Skip to content

Adds Social Info on Authors API #37

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jul 10, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 9 additions & 4 deletions src/codes/clj/docs/backend/adapters/db/postgres.clj
Original file line number Diff line number Diff line change
Expand Up @@ -77,9 +77,9 @@
(filter #(= (:type %) "see-also"))
(map db->see-also)))

(defn db->social
(defn db->socials
{:malli/schema [:=> [:cat [:sequential schemas.db/Row]]
[:maybe schemas.model.social/Social]]}
[:maybe [:sequential schemas.model.social/Social]]]}
[db-rows]
(->> db-rows
(group-by :definition-id)
Expand All @@ -90,5 +90,10 @@
{:social/definition-id definition-id
:social/notes notes
:social/examples examples
:social/see-alsos see-alsos})))
first))
:social/see-alsos see-alsos})))))

(defn db->social-definition
{:malli/schema [:=> [:cat [:sequential schemas.db/Row]]
[:maybe schemas.model.social/Social]]}
[db-rows]
(first (db->socials db-rows)))
8 changes: 8 additions & 0 deletions src/codes/clj/docs/backend/adapters/social.clj
Original file line number Diff line number Diff line change
Expand Up @@ -121,3 +121,11 @@
:notes (map note->model->wire notes)
:examples (map example->model->wire examples)
:see-alsos (map see-also->model->wire see-alsos)})

(defn author+socials->model->wire
{:malli/schema [:=> [:cat schemas.model.social/Author+Socials]
schemas.wire.out.social/Author+Socials]}
[{:author/keys [socials] :as author}]
(enc/assoc-some
(author->model->wire author)
:socials (map social->model->wire socials)))
6 changes: 3 additions & 3 deletions src/codes/clj/docs/backend/controllers/social.clj
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,11 @@
[new-author {:keys [database]}]
(db.postgres/upsert-author new-author database))

(defn get-author
(defn get-author+socials
{:malli/schema [:=> [:cat :string schemas.model.social/account-source schemas.types/Components]
[:maybe schemas.model.social/Author]]}
[:maybe schemas.model.social/Author+Socials]]}
[login source {:keys [database]}]
(db.postgres/get-author login source database))
(db.postgres/get-author+socials login source database))

(defn insert-see-also
{:malli/schema [:=> [:cat schemas.model.social/NewSeeAlso schemas.types/Components]
Expand Down
78 changes: 48 additions & 30 deletions src/codes/clj/docs/backend/db/postgres.clj
Original file line number Diff line number Diff line change
Expand Up @@ -5,41 +5,14 @@
[honey.sql :as sql]
[honey.sql.helpers :as sql.helpers]
[next.jdbc :as jdbc]
[parenthesin.components.db.jdbc-hikari :as components.database]))
[parenthesin.components.db.jdbc-hikari :as components.database]
[taoensso.encore :as enc]))

(defn ^:private execute!
{:malli/schema [:=> [:cat schemas.types/DatabaseComponent :any] :any]}
[db sql-params]
(components.database/execute db sql-params jdbc/unqualified-snake-kebab-opts))

(defn upsert-author
{:malli/schema [:=> [:cat schemas.model.social/NewAuthor schemas.types/DatabaseComponent]
schemas.model.social/Author]}
[transaction db]
(->> (-> (sql.helpers/insert-into :author)
(sql.helpers/values [transaction])
(sql.helpers/upsert (-> (sql.helpers/on-conflict :login :account_source)
(sql.helpers/do-update-set :avatar_url)))
(sql.helpers/returning :*)
sql/format)
(execute! db)
first
adapters/db->author))

(defn get-author
{:malli/schema [:=> [:cat :string schemas.model.social/account-source schemas.types/DatabaseComponent]
[:maybe schemas.model.social/Author]]}
[login source db]
(when-let [author (->> (-> (sql.helpers/select :*)
(sql.helpers/from :author)
(sql.helpers/where :and
[:= :login login]
[:= :account_source source])
sql/format)
(execute! db)
first)]
(adapters/db->author author)))

(defn insert-see-also
{:malli/schema [:=> [:cat schemas.model.social/NewSeeAlso schemas.types/DatabaseComponent]
schemas.model.social/SeeAlso]}
Expand Down Expand Up @@ -259,6 +232,51 @@
first
adapters/db->note))

(defn upsert-author
{:malli/schema [:=> [:cat schemas.model.social/NewAuthor schemas.types/DatabaseComponent]
schemas.model.social/Author]}
[transaction db]
(->> (-> (sql.helpers/insert-into :author)
(sql.helpers/values [transaction])
(sql.helpers/upsert (-> (sql.helpers/on-conflict :login :account_source)
(sql.helpers/do-update-set :avatar_url)))
(sql.helpers/returning :*)
sql/format)
(execute! db)
first
adapters/db->author))

(defn get-author+socials
{:malli/schema [:=> [:cat :string schemas.model.social/account-source schemas.types/DatabaseComponent]
[:maybe schemas.model.social/Author+Socials]]}
[login source db]
(when-let [author (->> (-> (sql.helpers/select :*)
(sql.helpers/from :author)
(sql.helpers/where :and
[:= :login login]
[:= :account_source source])
sql/format)
(execute! db)
first)]
(let [author-id (:author-id author)
socials (->> (-> (sql.helpers/union-all
(-> get-note-query
(sql.helpers/where [:= :note/author-id author-id]))

(-> get-example-query
(sql.helpers/where [:= :example-edit/author-id author-id]))

(-> get-see-also-query
(sql.helpers/where [:= :see-also/author-id author-id])))
sql/format)
(execute! db)
adapters/db->socials
seq)]

(enc/assoc-some
(adapters/db->author author)
:author/socials socials))))

(defn get-by-definition
{:malli/schema [:=> [:cat :string schemas.types/DatabaseComponent]
[:maybe schemas.model.social/Social]]}
Expand All @@ -274,4 +292,4 @@
(sql.helpers/where [:= :see-also/definition-id definition-id])))
sql/format)
(execute! db)
adapters/db->social))
adapters/db->social-definition))
6 changes: 3 additions & 3 deletions src/codes/clj/docs/backend/ports/http_in/social.clj
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,12 @@
:body {:author author
:access-token access-token}}))

(defn get-author
(defn get-author+socials
[{{{:keys [login source]} :path} :parameters
components :components}]
(if-let [author (controllers.social/get-author login source components)]
(if-let [author (controllers.social/get-author+socials login source components)]
{:status 200
:body (adapters.social/author->model->wire author)}
:body (adapters.social/author+socials->model->wire author)}
{:status 404
:body "not found"}))

Expand Down
6 changes: 3 additions & 3 deletions src/codes/clj/docs/backend/routes.clj
Original file line number Diff line number Diff line change
Expand Up @@ -45,14 +45,14 @@

["/author"
["/:login/:source"
{:get {:summary "get author by login and source"
{:get {:summary "get author and social interactions (if any) by login and source"
:parameters {:path {:login :string
:source :string}}
:responses {200 {:body schemas.wire.social/Author}
:responses {200 {:body schemas.wire.out.social/Author+Socials}
400 {:body :string}
404 {:body :string}
500 {:body :string}}
:handler ports.http-in.social/get-author}}]]
:handler ports.http-in.social/get-author+socials}}]]

["/example"
["/:example-id"
Expand Down
3 changes: 3 additions & 0 deletions src/codes/clj/docs/backend/schemas/model/social.clj
Original file line number Diff line number Diff line change
Expand Up @@ -110,3 +110,6 @@
[:social/notes [:sequential Note]]
[:social/examples [:sequential Example]]
[:social/see-alsos [:sequential SeeAlso]]])

(def Author+Socials
(mu/assoc Author [:author/socials {:optional true}] [:sequential Social]))
7 changes: 5 additions & 2 deletions src/codes/clj/docs/backend/schemas/wire/out/social.clj
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
(ns codes.clj.docs.backend.schemas.wire.out.social
(:require [codes.clj.docs.backend.schemas.wire.social :refer [example note
see-also]]
(:require [codes.clj.docs.backend.schemas.wire.social :refer [Author example
note see-also]]
[malli.util :as mu]))

(def SeeAlso
Expand Down Expand Up @@ -32,3 +32,6 @@
[:notes [:sequential Note]]
[:examples [:sequential Example]]
[:see-alsos [:sequential SeeAlso]]])

(def Author+Socials
(mu/assoc Author [:socials {:optional true}] [:sequential Social]))
38 changes: 33 additions & 5 deletions test/integration/codes/clj/docs/backend/db/postgres_test.clj
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,31 @@
:cleanup util/stop-system!
:fail-fast? true}

[database (state-flow.api/get-state :database)]

(util.db.postgres/upsert-author "delboni" "github")
[database (state-flow.api/get-state :database)

; prepare db authors
author-1 (util.db.postgres/upsert-author "delboni" "github")
author-2 (util.db.postgres/upsert-author "not-delboni" "github")

; prepare socials
see-also-1 (util.db.postgres/create-see-also {:see-also/author-id (:author/author-id author-1)
:see-also/definition-id "clojure.core/disj"
:see-also/definition-id-to "clojure.core/dissoc"})
_see-also-2 (util.db.postgres/create-see-also {:see-also/author-id (:author/author-id author-2)
:see-also/definition-id "clojure.core/disj"
:see-also/definition-id-to "clojure.core/dissoc2"})
note-1 (util.db.postgres/create-note {:note/author-id (:author/author-id author-1)
:note/definition-id "clojure.core/disj"
:note/body "author 1 note about this function."})
_note-2 (util.db.postgres/create-note {:note/author-id (:author/author-id author-2)
:note/definition-id "clojure.core/disj"
:note/body "author 2 note about this function."})
example-1 (util.db.postgres/create-example {:example/author-id (:author/author-id author-1)
:example/definition-id "clojure.core/disj"
:example/body "author 1 example about this function."})
_example-2 (util.db.postgres/create-example {:example/author-id (:author/author-id author-2)
:example/definition-id "clojure.core/disj"
:example/body "author 2 example about this function."})]

(flow "upsert author with new url"
(state/invoke
Expand All @@ -42,8 +64,14 @@
:author/login "delboni"
:author/account-source "github"
:author/avatar-url "https://my.pic.com/me2.jpg"
:author/created-at inst?}
(db/get-author "delboni" "github" database))))
:author/created-at inst?
:author/socials [{:social/definition-id "clojure.core/disj"
:social/notes [note-1]
:social/examples [(dissoc example-1
:example/author-id)]
:social/see-alsos [see-also-1]}]}

(db/get-author+socials "delboni" "github" database))))

(defflow see-also-db-test
{:init (util/start-system! create-and-start-components!)
Expand Down
62 changes: 62 additions & 0 deletions test/integration/codes/clj/docs/backend/social_test.clj
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,23 @@
(state-flow.server/request! {:method :get
:uri (str "/api/social/note/" note-id)})))

(flow "should return author + socials"
(match? {:status 200
:body {:author-id string?
:login "delboni",
:account-source "github",
:avatar-url "https://my.pic/me.jpg",
:created-at string?
:socials [{:definition-id "clojure.core/disj"
:notes [{:note-id note-id
:definition-id "clojure.core/disj"
:body "my edited note about this function."
:created-at string?}]
:examples []
:see-alsos []}]}}
(state-flow.server/request! {:method :get
:uri "/api/social/author/delboni/github"})))

(flow "should not be able to delete if not allowed"
(match? {:status 403
:body "You not allowed to delete this note."}
Expand Down Expand Up @@ -212,6 +229,22 @@
:created-at string?}}
(state-flow.server/request! {:method :get
:uri (str "/api/social/see-also/" see-also-id)})))
(flow "should return author + socials"
(match? {:status 200
:body {:author-id string?
:login "delboni",
:account-source "github",
:avatar-url "https://my.pic/me.jpg",
:created-at string?
:socials [{:definition-id "clojure.core/disj"
:notes []
:examples []
:see-alsos [{:see-also-id see-also-id
:definition-id "clojure.core/disj"
:definition-id-to "clojure.core/dissoc"
:created-at string?}]}]}}
(state-flow.server/request! {:method :get
:uri "/api/social/author/delboni/github"})))

(flow "should not be able to delete if not allowed"
(match? {:status 403
Expand Down Expand Up @@ -324,6 +357,35 @@
(state-flow.server/request! {:method :get
:uri (str "/api/social/example/" example-id)})))

(flow "should return author"
(match? {:status 200
:body {:author-id string?
:login "delboni",
:account-source "github",
:avatar-url "https://my.pic/me.jpg",
:created-at string?
:socials [{:definition-id "clojure.core/disj"
:notes []
:see-alsos []
:examples [{:example-id example-id
:definition-id "clojure.core/disj"
:body "my edited example about this function."
:created-at string?
:editors [{:author-id string?
:login "delboni"
:account-source "github"
:avatar-url "https://my.pic/me.jpg"
:created-at string?
:edited-at string?}
{:author-id string?
:login "delboni"
:account-source "github"
:avatar-url "https://my.pic/me.jpg"
:created-at string?
:edited-at string?}]}]}]}}
(state-flow.server/request! {:method :get
:uri "/api/social/author/delboni/github"})))

(flow "delete example revision part 1"
(state-flow.server/request! {:method :delete
:headers {"authorization" (str "Bearer " token)}
Expand Down
Loading