-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
use postgresql instead of datomic for porteiro
- Loading branch information
Showing
11 changed files
with
218 additions
and
28 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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
CREATE TABLE customers ( | ||
id UUID PRIMARY KEY, | ||
username VARCHAR(255) NOT NULL, | ||
name VARCHAR(255), | ||
roles TEXT[], | ||
hashed_password VARCHAR(255) NOT NULL | ||
); |
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,24 @@ | ||
(ns common-clj.io.interceptors.postgresql | ||
(:require [common-clj.error.core :as common-error] | ||
[io.pedestal.interceptor :as pedestal.interceptor] | ||
[pg.core :as pg] | ||
[pg.pool :as pool] | ||
[schema.core :as s])) | ||
|
||
(s/defn resource-existence-check-interceptor | ||
"resource-identifier-fn -> function used to extract param used to query the resource, must receive a context as argument. | ||
sql-query -> datomic query that will try to find the resource using the resource identifier" | ||
[resource-identifier-fn | ||
sql-query] | ||
(pedestal.interceptor/interceptor {:name ::resource-existence-check-interceptor | ||
:enter (fn [{{:keys [components]} :request :as context}] | ||
(let [pool (:postgresql components) | ||
resource-identifier (resource-identifier-fn context) | ||
resource (-> (pool/with-connection [database-conn pool] | ||
(pg/execute database-conn sql-query {:params [resource-identifier]})) first)] | ||
(when-not resource | ||
(common-error/http-friendly-exception 404 | ||
"resource-not-found" | ||
"Resource could not be found" | ||
"Not Found"))) | ||
context)})) |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
(ns common-clj.porteiro.db.postgresql.customer | ||
(:require [common-clj.porteiro.adapters.customer :as adapters.customer] | ||
[common-clj.porteiro.models.customer :as models.customer] | ||
[pg.core :as pg] | ||
[schema.core :as s])) | ||
|
||
(s/defn insert! :- models.customer/Customer | ||
[{:customer/keys [id username name roles hashed-password]} :- models.customer/Customer | ||
database-conn] | ||
(-> (pg/execute database-conn | ||
"INSERT INTO customers (id, username, name, roles, hashed_password) VALUES ($1, $2, $3, $4, $5) | ||
returning *" | ||
{:params [id username name (or roles []) hashed-password]}) | ||
first | ||
adapters.customer/postgresql->internal)) | ||
|
||
(s/defn by-username :- (s/maybe models.customer/Customer) | ||
[username :- s/Str | ||
database-conn] | ||
(some-> (pg/execute database-conn | ||
"SELECT * FROM customers WHERE username = $1" | ||
{:params [username]}) | ||
first | ||
adapters.customer/postgresql->internal)) | ||
|
||
(s/defn lookup :- (s/maybe models.customer/Customer) | ||
[customer-id :- s/Uuid | ||
database-conn] | ||
(some-> (pg/execute database-conn | ||
"SELECT * FROM customers WHERE id = $1" | ||
{:params [customer-id]}) | ||
first | ||
adapters.customer/postgresql->internal)) | ||
|
||
(s/defn add-role! :- (s/maybe models.customer/Customer) | ||
[customer-id :- s/Uuid | ||
role :- s/Keyword | ||
database-conn] | ||
(some-> (pg/execute database-conn | ||
"UPDATE customers SET roles = array_append(roles, $1) WHERE id = $2 returning *" | ||
{:params [(adapters.customer/internal-role->wire-role role) customer-id]}) | ||
first | ||
adapters.customer/postgresql->internal)) |
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,31 +1,30 @@ | ||
(ns common-clj.porteiro.diplomat.http-server.customer | ||
(:require [common-clj.porteiro.adapters.customer :as adapters.customer] | ||
[common-clj.porteiro.controllers.customer :as controllers.customer] | ||
[datomic.api :as d] | ||
[schema.core :as s]) | ||
(:import (java.util UUID))) | ||
|
||
(s/defn create-customer! | ||
[{{:keys [customer]} :json-params | ||
{:keys [datomic]} :components}] | ||
[{{:keys [customer]} :json-params | ||
{:keys [datomic postgresql]} :components}] | ||
{:status 201 | ||
:body {:customer (-> (adapters.customer/wire->internal customer) | ||
(controllers.customer/create-customer! datomic) | ||
(controllers.customer/create-customer! datomic postgresql) | ||
adapters.customer/internal->wire)}}) | ||
|
||
(s/defn authenticate-customer! | ||
[{{:keys [customer]} :json-params | ||
{:keys [datomic config]} :components}] | ||
[{{:keys [customer]} :json-params | ||
{:keys [datomic postgresql config]} :components}] | ||
{:status 200 | ||
:body (-> (adapters.customer/wire->internal-customer-authentication customer) | ||
(controllers.customer/authenticate-customer! config (d/db datomic)) | ||
(controllers.customer/authenticate-customer! config datomic postgresql) | ||
adapters.customer/customer-token->wire)}) | ||
|
||
(s/defn add-role! | ||
[{{wire-customer-id :customer-id | ||
wire-role :role} :query-params | ||
{:keys [datomic]} :components}] | ||
{:keys [datomic postgresql]} :components}] | ||
{:status 200 | ||
:body (-> (UUID/fromString wire-customer-id) | ||
(controllers.customer/add-role! (adapters.customer/wire->internal-role wire-role) datomic) | ||
(controllers.customer/add-role! (adapters.customer/wire->internal-role wire-role) datomic postgresql) | ||
adapters.customer/internal->wire)}) |
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
59 changes: 59 additions & 0 deletions
59
test/unit/common_clj/porteiro/db/postgresql/customer_test.clj
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,59 @@ | ||
(ns common-clj.porteiro.db.postgresql.customer-test | ||
(:require [clojure.test :refer :all] | ||
[common-clj.integrant-components.postgresql :as postgresql] | ||
[common-clj.porteiro.db.postgresql.customer :as database.customer] | ||
[common-clj.porteiro.models.customer :as models.customer] | ||
[matcher-combinators.test :refer [match?]] | ||
[common-clj.test.helper.schema :as test.helper.schema] | ||
[schema.test :as s])) | ||
|
||
(def customer-id (random-uuid)) | ||
(def customer | ||
(test.helper.schema/generate models.customer/Customer | ||
{:customer/id customer-id | ||
:customer/username "magal"})) | ||
|
||
(s/deftest insert-test | ||
(testing "Should insert a customer" | ||
(let [conn (postgresql/mocked-postgresql-conn)] | ||
(is (match? {:customer/hashed-password string? | ||
:customer/id uuid? | ||
:customer/roles () | ||
:customer/username string?} | ||
(database.customer/insert! customer conn)))))) | ||
|
||
(s/deftest by-username-test | ||
(testing "Should be able to query a customer by username" | ||
(let [conn (postgresql/mocked-postgresql-conn)] | ||
(database.customer/insert! customer conn) | ||
(is (match? {:customer/id uuid? | ||
:customer/username "magal" | ||
:customer/hashed-password string? | ||
:customer/roles []} | ||
(database.customer/by-username "magal" conn))) | ||
|
||
(is (nil? (database.customer/by-username "random-username" conn)))))) | ||
|
||
(s/deftest lookup-test | ||
(testing "Should be able to query a customer by id" | ||
(let [conn (postgresql/mocked-postgresql-conn)] | ||
(database.customer/insert! customer conn) | ||
(is (match? {:customer/id uuid? | ||
:customer/username "magal" | ||
:customer/hashed-password string? | ||
:customer/roles []} | ||
(database.customer/lookup customer-id conn))) | ||
|
||
(is (nil? (database.customer/lookup (random-uuid) conn)))))) | ||
|
||
(s/deftest add-role-test | ||
(testing "Should be able to query a customer by id" | ||
(let [conn (postgresql/mocked-postgresql-conn)] | ||
(database.customer/insert! customer conn) | ||
(is (match? {:customer/id uuid? | ||
:customer/username "magal" | ||
:customer/hashed-password string? | ||
:customer/roles [:test]} | ||
(database.customer/add-role! customer-id :test conn))) | ||
|
||
(is (nil? (database.customer/lookup (random-uuid) conn)))))) |