-
Notifications
You must be signed in to change notification settings - Fork 18
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
SQL-196 Reaction Management API (#321)
* SQL-196 Reaction Mgmt API * format * SQL-196 minor test refactor * SQL-196 additional tests * SQL-196 new json coercion fns for reaction * SQL-196 deal in camel reaction ids * SQL-196 API accepts camel everything
- Loading branch information
Showing
7 changed files
with
361 additions
and
16 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,161 @@ | ||
(ns lrsql.admin.interceptors.reaction | ||
(:require [clojure.spec.alpha :as s] | ||
[io.pedestal.interceptor :refer [interceptor]] | ||
[io.pedestal.interceptor.chain :as chain] | ||
[lrsql.admin.protocol :as adp] | ||
[lrsql.spec.reaction :as rs] | ||
[lrsql.util.reaction :as ru] | ||
[lrsql.util :as u])) | ||
|
||
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; | ||
;; Validation Interceptors | ||
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; | ||
|
||
(def validate-create-reaction-params | ||
"Validate valid params for reaction creation." | ||
(interceptor | ||
{:name ::validate-create-reaction-params | ||
:enter | ||
(fn validate-params [ctx] | ||
(let [{:keys [ruleset] :as raw-params} | ||
(get-in ctx [:request :json-params]) | ||
params (cond-> raw-params | ||
ruleset | ||
(update :ruleset ru/json->ruleset))] | ||
(if-some [err (s/explain-data rs/create-reaction-params-spec params)] | ||
;; Invalid parameters - Bad Request | ||
(assoc (chain/terminate ctx) | ||
:response | ||
{:status 400 | ||
:body {:error (format "Invalid parameters:\n%s" | ||
(-> err s/explain-out with-out-str))}}) | ||
;; Valid params - continue | ||
(assoc ctx ::data params))))})) | ||
|
||
(def validate-update-reaction-params | ||
"Validate valid params for reaction update." | ||
(interceptor | ||
{:name ::validate-update-reaction-params | ||
:enter | ||
(fn validate-params [ctx] | ||
(let [{:keys [ruleset] :as raw-params} | ||
(get-in ctx [:request :json-params]) | ||
params (-> raw-params | ||
ru/json->input | ||
(update :reaction-id u/str->uuid) | ||
(cond-> | ||
ruleset | ||
(update :ruleset ru/json->ruleset)))] | ||
(if-some [err (s/explain-data rs/update-reaction-params-spec params)] | ||
;; Invalid parameters - Bad Request | ||
(assoc (chain/terminate ctx) | ||
:response | ||
{:status 400 | ||
:body {:error (format "Invalid parameters:\n%s" | ||
(-> err s/explain-out with-out-str))}}) | ||
;; Valid params - continue | ||
(assoc ctx ::data params))))})) | ||
|
||
(def validate-delete-reaction-params | ||
"Validate valid params for reaction delete." | ||
(interceptor | ||
{:name ::validate-delete-reaction-params | ||
:enter | ||
(fn validate-params [ctx] | ||
(let [params (-> (get-in ctx [:request :json-params]) | ||
ru/json->input | ||
(update :reaction-id u/str->uuid))] | ||
(if-some [err (s/explain-data rs/delete-reaction-params-spec params)] | ||
;; Invalid parameters - Bad Request | ||
(assoc (chain/terminate ctx) | ||
:response | ||
{:status 400 | ||
:body {:error (format "Invalid parameters:\n%s" | ||
(-> err s/explain-out with-out-str))}}) | ||
;; Valid params - continue | ||
(assoc ctx ::data params))))})) | ||
|
||
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; | ||
;; Terminal Interceptors | ||
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; | ||
|
||
(def create-reaction | ||
"Create a new reaction and store it." | ||
(interceptor | ||
{:name ::create-reaction | ||
:enter | ||
(fn create-reaction [ctx] | ||
(let [{lrs :com.yetanalytics/lrs | ||
{:keys [ruleset active]} ::data} | ||
ctx | ||
{:keys [result]} | ||
(adp/-create-reaction lrs ruleset active)] | ||
(assoc ctx | ||
:response | ||
{:status 200 :body {:reactionId result}})))})) | ||
|
||
(def get-all-reactions | ||
"List all reactions." | ||
(interceptor | ||
{:name ::get-all-reactions | ||
:enter | ||
(fn get-all-reactions [ctx] | ||
(let [{lrs :com.yetanalytics/lrs} ctx | ||
result | ||
(adp/-get-all-reactions lrs)] | ||
(assoc ctx | ||
:response | ||
{:status 200 :body {:reactions | ||
(map | ||
(fn [reaction-record] | ||
(-> reaction-record | ||
(update :created u/time->str) | ||
(update :modified u/time->str) | ||
(update :ruleset ru/ruleset->json))) | ||
result)}})))})) | ||
|
||
(def update-reaction | ||
"Update an existing reaction." | ||
(interceptor | ||
{:name ::update-reaction | ||
:enter | ||
(fn create-reaction [ctx] | ||
(let [{lrs :com.yetanalytics/lrs | ||
{:keys [reaction-id ruleset active]} ::data} | ||
ctx | ||
{:keys [result]} | ||
(adp/-update-reaction lrs reaction-id ruleset active)] | ||
(cond | ||
(uuid? result) | ||
(assoc ctx | ||
:response | ||
{:status 200 :body {:reactionId result}}) | ||
(= :lrsql.reaction/reaction-not-found-error result) | ||
(assoc (chain/terminate ctx) | ||
:response | ||
{:status 404 | ||
:body {:error (format "The reaction \"%s\" does not exist!" | ||
(u/uuid->str reaction-id))}}))))})) | ||
|
||
(def delete-reaction | ||
"Delete a reaction." | ||
(interceptor | ||
{:name ::delete-reaction | ||
:enter | ||
(fn delete-reaction [ctx] | ||
(let [{lrs :com.yetanalytics/lrs | ||
{:keys [reaction-id]} ::data} | ||
ctx | ||
{:keys [result]} | ||
(adp/-delete-reaction lrs reaction-id)] | ||
(cond | ||
(uuid? result) | ||
(assoc ctx | ||
:response | ||
{:status 200 :body {:reactionId result}}) | ||
(= :lrsql.reaction/reaction-not-found-error result) | ||
(assoc (chain/terminate ctx) | ||
:response | ||
{:status 404 | ||
:body {:error (format "The reaction \"%s\" does not exist!" | ||
(u/uuid->str reaction-id))}}))))})) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.