aboutsummaryrefslogtreecommitdiff
path: root/src/clj
diff options
context:
space:
mode:
authorGabriel A. Giovanini <mail@gabrielgio.me>2018-02-22 00:38:26 -0300
committerGabriel A. Giovanini <mail@gabrielgio.me>2018-02-22 00:38:26 -0300
commitf55b9febe8974f861e2179baddaf9d48fa73552c (patch)
treef0d11523c5f986643a0b37cc067dcd962ecd25de /src/clj
parent0b8277f72cdc2ed2abeb566f30958bd8e0dc5167 (diff)
downloadqueue-api-f55b9febe8974f861e2179baddaf9d48fa73552c.tar.gz
queue-api-f55b9febe8974f861e2179baddaf9d48fa73552c.tar.bz2
queue-api-f55b9febe8974f861e2179baddaf9d48fa73552c.zip
A better swagger documentation and a better routing test
Diffstat (limited to 'src/clj')
-rw-r--r--src/clj/queue_api/db/core.clj11
-rw-r--r--src/clj/queue_api/routes/services.clj139
2 files changed, 101 insertions, 49 deletions
diff --git a/src/clj/queue_api/db/core.clj b/src/clj/queue_api/db/core.clj
index 0482feb..adbda25 100644
--- a/src/clj/queue_api/db/core.clj
+++ b/src/clj/queue_api/db/core.clj
@@ -16,12 +16,12 @@
(defn add-agent
"Add an agent into the database"
- [{:keys [id name primary-skillset secondary-skillset]}]
+ [{:keys [id name primary_skillset secondary_skillset]}]
(d/transact! conn
[{:agent/id id
:agent/name name
- :agent/primary-skillset primary-skillset
- :agent/secondary-skillset secondary-skillset}]))
+ :agent/primary-skillset primary_skillset
+ :agent/secondary-skillset secondary_skillset}]))
(defn add-job
"Add a job into the database"
@@ -107,6 +107,11 @@
[id]
(d/entity @conn [:agent/id id]))
+(defn job
+ "Get a job by id"
+ [id]
+ (d/entity @conn [:job/id id]))
+
(defn fittest-job
"Get the fittest job for a agent `id`."
[^String id]
diff --git a/src/clj/queue_api/routes/services.clj b/src/clj/queue_api/routes/services.clj
index 766f831..c3c83e9 100644
--- a/src/clj/queue_api/routes/services.clj
+++ b/src/clj/queue_api/routes/services.clj
@@ -2,7 +2,39 @@
(:require [ring.util.http-response :refer :all]
[compojure.api.sweet :refer :all]
[schema.core :as s]
- [queue-api.db.core :as db]))
+ [queue-api.db.core :as db]
+ [ring.util.http-status :as http-status]))
+
+
+
+(s/defschema new-agent
+ {:id s/Str
+ :name s/Str
+ :primary_skillset [s/Str]
+ (s/optional-key :secondary_skillset) [s/Str]})
+
+(s/defschema new-job
+ {:id s/Str
+ :type s/Str
+ :urgent s/Bool})
+
+(s/defschema agent-id
+ {:agent_id s/Str})
+
+(s/defschema agent-sum
+ [{:type s/Str
+ :jobs s/Int}])
+
+(s/defschema error-message
+ {:message s/Str})
+
+(s/defschema job-sum
+ {:completed [s/Str]
+ :processing [s/Str]
+ :unassigned [s/Str]})
+
+(s/defschema job-request
+ {:job_request {:job_id s/Str :agent_id s/Str}})
(defapi service-routes
{:swagger {:ui "/swagger-ui"
@@ -12,52 +44,67 @@
:description "Manages agent resources"}}}}
(context "/agent" []
- :tags ["agent"]
- (PUT "/" []
- :body-params [id :- String, name :- String, primary_skillset :- [String], secondary_skillset :- [String]]
- :summary "Add a new agent"
- (db/add-agent
- {:id id
- :name name
- :primary-skillset primary_skillset
- :secondary-skillset secondary_skillset})
- (ok))
- (POST "/" []
- :return [{:type String :jobs s/Int}]
- :body-params [agent_id :- String]
- :summary "Get summary of an agent"
- (let [jobs (db/sum-agent agent_id)]
- (map (fn [x]
- {:type (first x)
- :jobs (last x)}) jobs))))
+ (resource
+ {:tags ["agent"]
+ :put {:summary "Add a new agent"
+ :responses {http-status/created {:description "Agent Created"}
+ http-status/bad-request {:description "Agent id already exists"
+ :schema error-message}}
+ :parameters {:body-params new-agent}
+ :handler (fn [{body :body-params}]
+ (if (nil? (db/agent (:id body)))
+ (do (db/add-agent body)
+ {:status 201})
+ (bad-request {:message "Agent id already exists"})))}
+ :post {:summary "Get summary of an agent"
+ :parameters {:body-params agent-id}
+ :responses {http-status/ok {:schema agent-sum
+ :description "Fetched correctly"}
+ http-status/not-found {:schema error-message
+ :description "Agent not found"}}
+ :handler (fn [{body :body-params}]
+ (let [a (:agent_id body)]
+ (if-not (nil? (db/agent a))
+ (let [jobs (db/sum-agent a)]
+ (ok (map (fn [x]
+ {:type (first x)
+ :jobs (last x)}) jobs)))
+ (not-found {:message "Agent does not exist"}))))}}))
+
(context "/job" []
- :tags ["job"]
- (PUT "/" []
- :body-params [id :- String, type :- String, urgent :- Boolean]
- :summary "Add a new job"
- (db/add-job {:id id
- :type type
- :urgent urgent})
- (ok))
- (POST "/" []
- :return {:job_request {:job_id s/Any :agent_id String}}
- :body-params [agent_id :- String]
- :summary "Request a job to a given agent"
-
- (let [a (agent agent_id)]
- (if (nil? a)
- (not-found {:message "Agent does not exist"}))
- (let [j (db/dequeue-job agent_id)]
- (if (nil? j)
- (not-found {:message "There is no job available for given agent"})
- (ok {:job_request {:job_id j :agent_id agent_id}})))))
-
- (GET "/" []
- :return {:completed [String]
- :processing [String]
- :unassigned [String]}
- :summary "Get a summary of the queue"
- (ok (db/sum-queue)))))
+ (resource
+ {:tags ["job"]
+ :put {:summary "Add a new job"
+ :responses {http-status/created {:description "Job Created"}
+ http-status/bad-request {:description "Job id already exists"
+ :schema error-message}}
+ :parameters {:body-params new-job}
+ :handler (fn [{body :body-params}]
+ (if (nil? (db/job (:id body)))
+ (do
+ (db/add-job body)
+ {:status 201})
+ (bad-request {:message "job id already exist"})))}
+ :post {:summary "Request a job to a given agent"
+ :responses {http-status/ok {:description "Job assigned successfully"
+ :schema job-request}
+ http-status/not-found {:description "Agent not found or there is no job avaliable"
+ :schema error-message}}
+ :parameters {:body-params agent-id}
+ :handler (fn [{body :body-params}]
+ (let [id (:agent_id body)
+ a (db/agent id)]
+ (if (nil? a)
+ (not-found {:message "Agent does not exist"}))
+ (let [j (db/dequeue-job id)]
+ (if (nil? j)
+ (not-found {:message "There is no job available for given agent"})
+ (ok {:job_request {:job_id j :agent_id id}})))))}
+ :get {:summary "Get queue summary"
+ :responses {http-status/ok {:description "Jobs summary fetched correctly"
+ :schema job-sum}}
+ :handler (fn [_]
+ (ok (db/sum-queue)))}})))