-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Refactor invitation prst layer to use with plastic strategy
[Re #1611] * Also refactor it to follow new conventions on the persistence layer side, handling errors at that level.
- Loading branch information
1 parent
a187b0b
commit ed3e40c
Showing
4 changed files
with
151 additions
and
32 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 |
---|---|---|
@@ -1,9 +1,113 @@ | ||
(ns gpml.db.invitation | ||
{:ns-tracker/resource-deps ["invitation.sql"]} | ||
(:require [hugsql.core :as hugsql])) | ||
(:require [gpml.db.jdbc-util :as jdbc-util] | ||
[gpml.util :as util] | ||
[gpml.util.postgresql :as util.pgsql] | ||
[gpml.util.sql :as util.sql] | ||
[hugsql.core :as hugsql])) | ||
|
||
(declare create-invitations | ||
get-invitations | ||
accept-invitation) | ||
(declare create-invitations* | ||
get-invitations* | ||
accept-invitation* | ||
delete-invitation*) | ||
|
||
(hugsql/def-db-fns "gpml/db/invitation.sql" {:quoting :ansi}) | ||
|
||
(defn- p-invitation->invitation | ||
[p-invitation] | ||
(util/update-if-not-nil p-invitation :type keyword)) | ||
|
||
(defn- invitation->p-invitation | ||
[invitation] | ||
(util/update-if-not-nil invitation :type util.pgsql/->PGEnum "invitation_type")) | ||
|
||
(defn get-invitations | ||
[conn opts] | ||
(try | ||
{:success? true | ||
:invitations (->> (get-invitations* conn opts) | ||
(jdbc-util/db-result-snake-kw->db-result-kebab-kw) | ||
(map p-invitation->invitation))} | ||
(catch Throwable t | ||
{:success? false | ||
:reason :exception | ||
:error-details {:msg (ex-message t)}}))) | ||
|
||
(defn get-invitation | ||
[conn opts] | ||
(try | ||
(let [invitations (get-invitations conn opts)] | ||
(if (and (seq invitations) | ||
(= (count invitations) 1)) | ||
{:success? true | ||
:invitation (first invitations)} | ||
{:success? false | ||
:reason :not-found})) | ||
(catch Throwable t | ||
{:success? false | ||
:reason :exception | ||
:error-details {:msg (ex-message t)}}))) | ||
|
||
(defn create-invitations | ||
[conn invitations] | ||
(try | ||
(let [p-invitations (jdbc-util/db-params-kebab-kw->db-params-snake-kw | ||
(map invitation->p-invitation invitations)) | ||
cols (util.sql/get-insert-columns-from-entity-col p-invitations) | ||
values (util.sql/entity-col->persistence-entity-col p-invitations) | ||
created-invitations (create-invitations* conn {:cols cols | ||
:values values})] | ||
(if (= (count created-invitations) (count invitations)) | ||
{:success? true | ||
:invitations (map p-invitation->invitation created-invitations)} | ||
{:success? false | ||
:reason :unexpected-number-of-affected-rows | ||
:error-details {:expected-affected-rows (count invitations) | ||
:actual-affected-rows (count created-invitations)}})) | ||
(catch Throwable t | ||
{:success? false | ||
:reason :exception | ||
:error-details {:msg (ex-message t)}}))) | ||
|
||
(defn create-invitation | ||
[conn invitation] | ||
(try | ||
(let [result (create-invitations conn [invitation])] | ||
(if (:success? result) | ||
{:success? true | ||
:invitation (first (:invitations result))} | ||
result)) | ||
(catch Throwable t | ||
{:success? false | ||
:reason :exception | ||
:error-details {:msg (ex-message t)}}))) | ||
|
||
(defn accept-invitation | ||
[conn invitation-id] | ||
(try | ||
(let [affected (accept-invitation* conn {:id invitation-id})] | ||
(if (= affected 1) | ||
{:success? true} | ||
{:success? false | ||
:reason :unexpected-number-of-affected-rows | ||
:error-details {:expected-affected-rows 1 | ||
:actual-affected-rows affected}})) | ||
(catch Throwable t | ||
{:success? false | ||
:reason :exception | ||
:error-details {:msg (ex-message t)}}))) | ||
|
||
(defn delete-invitation | ||
[conn invitation-id] | ||
(try | ||
(let [affected (delete-invitation* conn {:id invitation-id})] | ||
(if (= affected 1) | ||
{:success? true} | ||
{:success? false | ||
:reason :unexpected-number-of-affected-rows | ||
:error-details {:expected-affected-rows 1 | ||
:actual-affected-rows affected}})) | ||
(catch Throwable t | ||
{:success? false | ||
:reason :exception | ||
:error-details {:msg (ex-message t)}}))) |
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