diff options
author | Gabriel A. Giovanini <mail@gabrielgio.me> | 2018-03-08 23:06:12 -0300 |
---|---|---|
committer | Gabriel A. Giovanini <mail@gabrielgio.me> | 2018-03-08 23:06:12 -0300 |
commit | de7cd8220c364dd816f4136a9d4c5a4d6618359d (patch) | |
tree | 8de2745e1d6d63d020330ad71f8bc6fe5e37db08 | |
parent | b9e1725e4f9bebce7e426431a1f8524efd026662 (diff) | |
download | queue-api-de7cd8220c364dd816f4136a9d4c5a4d6618359d.tar.gz queue-api-de7cd8220c364dd816f4136a9d4c5a4d6618359d.tar.bz2 queue-api-de7cd8220c364dd816f4136a9d4c5a4d6618359d.zip |
Better routing and mutex
-rw-r--r-- | README.md | 2 | ||||
-rw-r--r-- | src/clj/queue_api/db/core.clj | 9 | ||||
-rw-r--r-- | src/clj/queue_api/routes/services.clj | 31 | ||||
-rw-r--r-- | test/clj/queue_api/test/handler.clj | 4 |
4 files changed, 24 insertions, 22 deletions
@@ -74,7 +74,7 @@ For `services.clj` it holds all code for endpoint definition and input validatio Considering the exercise requirements there is a need for 5 endpoints: * Endpoint to add agent is a`:put` at `/agent` -* Endpoint to get how many jobs of each type this agent has performed is a `:post` at `/agent`. Note: usually since it is a method that doesn't modify anything I'd have used `:get` and pass the agent id via path (`/agent/:id`) but one of the requirement is "*All endpoints should accept and return JSON content type payloads*" I worked with POST PUT. +* Endpoint to get how many jobs of each type this agent has performed is a `:get` at `/agent/:id`. * Endpoint to add a job is `:put` at `/job` * Endpoint to request a job is `:post` at `/job` * Endpoint to get current queue state is `:get` at `/job` diff --git a/src/clj/queue_api/db/core.clj b/src/clj/queue_api/db/core.clj index adbda25..0ad9b81 100644 --- a/src/clj/queue_api/db/core.clj +++ b/src/clj/queue_api/db/core.clj @@ -156,7 +156,8 @@ [^String id] (let [a (d/entity @conn [:agent/id id])] (if-not (nil? a) - (let [jid (:job/id (fittest-job id))] - (end-job a) - (start-job id jid) - jid))))
\ No newline at end of file + (locking (Object.) + (let [jid (:job/id (fittest-job id))] + (end-job a) + (start-job id jid) + jid)))))
\ No newline at end of file diff --git a/src/clj/queue_api/routes/services.clj b/src/clj/queue_api/routes/services.clj index 9b8cc91..01937c1 100644 --- a/src/clj/queue_api/routes/services.clj +++ b/src/clj/queue_api/routes/services.clj @@ -8,9 +8,9 @@ (s/defschema new-agent - {:id s/Str - :name s/Str - :primary_skillset [s/Str] + {:id s/Str + :name s/Str + :primary_skillset [s/Str] :secondary_skillset [s/Str]}) (s/defschema new-job @@ -55,21 +55,24 @@ (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} + (bad-request {:message "Agent id already exists"})))}})) + + (context "/agent/:id" [] + (resource + {:tags ["agent"] + :get {:summary "Get summary of an agent" + :parameters {:path-params {:id s/Str}} :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"}))))}})) + :handler (fn [{{:keys [id]} :path-params}] + (if-not (nil? (db/agent id)) + (let [jobs (db/sum-agent id)] + (ok (map (fn [x] + {:type (first x) + :jobs (last x)}) jobs))) + (not-found {:message "Agent does not exist"})))}})) (context "/job" [] diff --git a/test/clj/queue_api/test/handler.clj b/test/clj/queue_api/test/handler.clj index 75ce8f3..0af1b9f 100644 --- a/test/clj/queue_api/test/handler.clj +++ b/test/clj/queue_api/test/handler.clj @@ -43,9 +43,7 @@ (json-body {:agent_id "51c6324b-a176-4663-8b5f-03fc2544dbe2"}) app)] (is (= 404 (:status response)))) - (let [response (-> (request :post "/agent") - (json-body {:agent_id "644be0ce-035d-48cb-867e-8e6de2714a8d"}) - app)] + (let [response (app (request :get "/agent/644be0ce-035d-48cb-867e-8e6de2714a8d"))] (is (= 200 (:status response)))))) (deftest job-test (testing "Job route PUT" |