Skip to content

Commit

Permalink
feat: adds delete notes, see-also & better return on definition
Browse files Browse the repository at this point in the history
  • Loading branch information
rafaeldelboni committed Mar 14, 2024
1 parent 57544a7 commit a6453da
Show file tree
Hide file tree
Showing 10 changed files with 119 additions and 27 deletions.
28 changes: 15 additions & 13 deletions src/codes/clj/docs/backend/adapters/db/postgres.clj
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,14 @@
:author/created-at created-at})

(defn db->note
{:malli/schema [:=> [:cat schemas.db/Row] schemas.model.social/Note]}
{:malli/schema [:=> [:cat [:maybe schemas.db/Row]] [:maybe schemas.model.social/Note]]}
[{:keys [id definition-id body created author-id] :as note}]
(enc/assoc-some {:note/note-id id
:note/definition-id definition-id
:note/body body
:note/created-at created}
:note/author (when author-id (db->author note))))
(when note
(enc/assoc-some {:note/note-id id
:note/definition-id definition-id
:note/body body
:note/created-at created}
:note/author (when author-id (db->author note)))))

(defn db->notes
{:malli/schema [:=> [:cat [:sequential schemas.db/Row]]
Expand Down Expand Up @@ -55,14 +56,15 @@
(db->example example editors))))))

(defn db->see-also
{:malli/schema [:=> [:cat schemas.db/Row] schemas.model.social/SeeAlso]}
{:malli/schema [:=> [:cat [:maybe schemas.db/Row]] [:maybe schemas.model.social/SeeAlso]]}
[{:keys [id definition-id body created author-id] :as see-also}]
(enc/assoc-some {:see-also/see-also-id id
:see-also/definition-id definition-id
:see-also/definition-id-to body
:see-also/created-at created}
:see-also/author (when author-id
(db->author see-also))))
(when see-also
(enc/assoc-some {:see-also/see-also-id id
:see-also/definition-id definition-id
:see-also/definition-id-to body
:see-also/created-at created}
:see-also/author (when author-id
(db->author see-also)))))

(defn db->see-alsos
{:malli/schema [:=> [:cat [:sequential schemas.db/Row]]
Expand Down
3 changes: 1 addition & 2 deletions src/codes/clj/docs/backend/adapters/social.clj
Original file line number Diff line number Diff line change
Expand Up @@ -90,10 +90,9 @@
(defn update-note-wire->model
{:malli/schema [:=> [:cat schemas.wire.in.social/UpdateNote :uuid]
schemas.model.social/UpdateNote]}
[{:keys [note-id definition-id body]} author-id]
[{:keys [note-id body]} author-id]
#:note{:note-id note-id
:author-id author-id
:definition-id definition-id
:body body})

(defn note->model->wire
Expand Down
12 changes: 12 additions & 0 deletions src/codes/clj/docs/backend/controllers/social.clj
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,12 @@
[new-see-also {:keys [database]}]
(db.postgres/insert-see-also new-see-also database))

(defn delete-see-also
{:malli/schema [:=> [:cat :uuid schemas.types/Components]
:uuid]}
[see-also-id {:keys [database]}]
(db.postgres/delete-see-also see-also-id database))

(defn get-see-also
{:malli/schema [:=> [:cat :uuid schemas.types/Components]
[:maybe schemas.model.social/SeeAlso]]}
Expand Down Expand Up @@ -57,6 +63,12 @@
[update-note {:keys [database]}]
(db.postgres/update-note update-note database))

(defn delete-note
{:malli/schema [:=> [:cat :uuid schemas.types/Components]
:uuid]}
[note-id {:keys [database]}]
(db.postgres/delete-note note-id database))

(defn get-note
{:malli/schema [:=> [:cat :uuid schemas.types/Components]
[:maybe schemas.model.social/Note]]}
Expand Down
24 changes: 24 additions & 0 deletions src/codes/clj/docs/backend/db/postgres.clj
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,18 @@
first
adapters/db->see-also))

(defn delete-see-also
{:malli/schema [:=> [:cat :uuid schemas.types/DatabaseComponent]
:uuid]}
[see-also-id db]
(->> (-> (sql.helpers/delete-from :see-also)
(sql.helpers/where [:= :see-also-id see-also-id])
(sql.helpers/returning [:see-also-id :id])
sql/format)
(execute! db)
first
:id))

(def get-see-also-query
(-> (sql.helpers/select
[:see-also/see-also-id :id]
Expand Down Expand Up @@ -178,6 +190,18 @@
first
adapters/db->note))

(defn delete-note
{:malli/schema [:=> [:cat :uuid schemas.types/DatabaseComponent]
:uuid]}
[note-id db]
(->> (-> (sql.helpers/delete-from :note)
(sql.helpers/where [:= :note-id note-id])
(sql.helpers/returning [:note-id :id])
sql/format)
(execute! db)
first
:id))

(def get-note-query
(-> (sql.helpers/select
[:note/note-id :id]
Expand Down
36 changes: 30 additions & 6 deletions src/codes/clj/docs/backend/ports/http_in/social.clj
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,17 @@
(controllers.social/insert-see-also components)
adapters.social/see-also->model->wire)})

; TODO update-see-also
;; TODO delete-see-also, routes & integration tests
(defn delete-see-also
[{{{:keys [see-also-id]} :path} :parameters
components :components
auth :auth}]
(let [{author :note/author} (controllers.social/get-see-also see-also-id components)]
(if (= (:author/author-id author) (:author-id auth))
{:status 202
:body {:see-also-id (controllers.social/delete-see-also see-also-id components)}}
{:status 403
:body "You not allowed to delete this see also."})))

(defn get-see-also
[{{{:keys [see-also-id]} :path} :parameters
Expand Down Expand Up @@ -99,6 +109,18 @@
{:status 403
:body "You not allowed to update this note."})))

;; TODO delete-note, routes & integration tests
(defn delete-note
[{{{:keys [note-id]} :path} :parameters
components :components
auth :auth}]
(let [{author :note/author} (controllers.social/get-note note-id components)]
(if (= (:author/author-id author) (:author-id auth))
{:status 202
:body {:note-id (controllers.social/delete-note note-id components)}}
{:status 403
:body "You not allowed to delete this see also."})))

(defn get-note
[{{{:keys [note-id]} :path} :parameters
components :components}]
Expand All @@ -111,8 +133,10 @@
(defn get-by-definition
[{{{:keys [definition-id]} :path} :parameters
components :components}]
(if-let [definition (controllers.social/get-by-definition definition-id components)]
{:status 200
:body (adapters.social/social->model->wire definition)}
{:status 404
:body "not found"}))
{:status 200
:body (if-let [definition (controllers.social/get-by-definition definition-id components)]
(adapters.social/social->model->wire definition)
{:definition-id definition-id
:notes []
:examples []
:see-alsos []})})
1 change: 0 additions & 1 deletion src/codes/clj/docs/backend/schemas/model/social.clj
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,6 @@
(def UpdateNote
(mu/select-keys note [:note/note-id
:note/author-id
:note/definition-id
:note/body]))

(def Note
Expand Down
1 change: 0 additions & 1 deletion src/codes/clj/docs/backend/schemas/wire/in/social.clj
Original file line number Diff line number Diff line change
Expand Up @@ -33,5 +33,4 @@
:body]))
(def UpdateNote
(mu/select-keys note [:note-id
:definition-id
:body]))
16 changes: 16 additions & 0 deletions test/integration/codes/clj/docs/backend/db/postgres_test.clj
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,14 @@
:see-also/definition-id "clojure.core/disj"
:see-also/definition-id-to "clojure.core/dissoc"
:see-also/created-at inst?}
(util.db.postgres/get-see-also (:see-also/see-also-id see-also))))

(flow "delete see-also in db"
(match? (:see-also/see-also-id see-also)
(util.db.postgres/delete-see-also (:see-also/see-also-id see-also))))

(flow "check see-also using get-see-also fn"
(match? nil
(util.db.postgres/get-see-also (:see-also/see-also-id see-also)))))

(defflow note-db-test
Expand Down Expand Up @@ -110,6 +118,14 @@
:note/definition-id "clojure.core/disj"
:note/body "edited my note about this function."
:note/created-at inst?}
(util.db.postgres/get-note (:note/note-id note))))

(flow "delete note in db"
(match? (:note/note-id note)
(util.db.postgres/delete-note (:note/note-id note))))

(flow "check note using get-note fn"
(match? nil
(util.db.postgres/get-note (:note/note-id note)))))

(defflow example-db-test
Expand Down
9 changes: 5 additions & 4 deletions test/integration/codes/clj/docs/backend/social_test.clj
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,6 @@
:headers {"authorization" (str "Bearer " fake-token)}
:uri "/api/social/note/"
:body {:note-id note-id
:definition-id "clojure.core/disj"
:body "my edited note about this function."}})))

(flow "check update note response"
Expand All @@ -121,7 +120,6 @@
:headers {"authorization" (str "Bearer " token)}
:uri "/api/social/note/"
:body {:note-id note-id
:definition-id "clojure.core/disj"
:body "my edited note about this function."}}))

(flow "checks db for updated note"
Expand Down Expand Up @@ -268,7 +266,10 @@
(flow "should interact with system"

(flow "should not return definition"
(match? {:status 404
:body "not found"}
(match? {:status 200
:body {:definition-id "golang/go/math/abs/0"
:notes []
:examples []
:see-alsos []}}
(state-flow.server/request! {:method :get
:uri "/api/social/definition/golang/go/math/abs/0"})))))
16 changes: 16 additions & 0 deletions test/integration/codes/clj/docs/backend/util/db/postgres.clj
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,14 @@
(db/update-note note)
state-flow.api/return)))

(defn delete-note
[note-id]
(flow "delete note"
[database (state-flow.api/get-state :database)]
(->> database
(db/delete-note note-id)
state-flow.api/return)))

(defn create-see-also
[see-also]
(flow "insert new see-also"
Expand All @@ -53,6 +61,14 @@
(db/insert-see-also see-also)
state-flow.api/return)))

(defn delete-see-also
[see-also-id]
(flow "delete see-also"
[database (state-flow.api/get-state :database)]
(->> database
(db/delete-see-also see-also-id)
state-flow.api/return)))

(defn get-note
[note-id]
(flow "get note by id"
Expand Down

0 comments on commit a6453da

Please sign in to comment.