diff --git a/bruno/CodeSnippets/get_snippet_by_slug.bru b/bruno/CodeSnippets/get_snippet_by_slug.bru new file mode 100644 index 0000000..3c17768 --- /dev/null +++ b/bruno/CodeSnippets/get_snippet_by_slug.bru @@ -0,0 +1,23 @@ +meta { + name: get_snippet_by_slug + type: http + seq: 10 +} + +get { + url: {{host}}/api/snippet-by-slug?slug=netcat-over-ping + body: none + auth: none +} + +params:query { + slug: netcat-over-ping +} + +body:json { + { + "title": "Test Snippet", + "markdown": "## Cool Snippet\ndoes a cool thing", + "tags": ["git", "jj"] + } +} diff --git a/bruno/CodeSnippets/get_tag.bru b/bruno/CodeSnippets/get_tag.bru new file mode 100644 index 0000000..c040ed9 --- /dev/null +++ b/bruno/CodeSnippets/get_tag.bru @@ -0,0 +1,23 @@ +meta { + name: get_tag + type: http + seq: 9 +} + +get { + url: {{host}}/api/tag?tag=git + body: none + auth: none +} + +params:query { + tag: git +} + +body:json { + { + "title": "Test Snippet", + "markdown": "## Cool Snippet\ndoes a cool thing", + "tags": ["git", "jj"] + } +} diff --git a/src/snippets/infra/api.clj b/src/snippets/infra/api.clj index 65e07f5..5f2c6fe 100644 --- a/src/snippets/infra/api.clj +++ b/src/snippets/infra/api.clj @@ -52,6 +52,21 @@ {:status 200 :body tags})) +(defn handle-view-snippets-by-tag [{params :query-params}] + (let [tag (get params "tag")] + {:status 200 + :body (snippets.use-cases.view/view-snippets-by-tag tag)})) + +(defn handle-view-tags [_] + (let [tags (snippets.use-cases.view/view-tags)] + {:status 200 + :body tags})) + +(defn handle-view-snippet-by-slug [{params :query-params}] + (let [slug (get params "slug")] + {:status 200 + :body (snippets.use-cases.view/view-snippet-by-slug slug)})) + (defn wrap [handler id] (fn [request] (update (handler request) :wrap (fnil conj '()) id))) @@ -64,6 +79,8 @@ [wrap :api]]} ["/ping" {:get handle-ping}] ["/tags" {:get handle-view-tags}] + ["/tag" {:get handle-view-snippets-by-tag}] + ["/snippet-by-slug" {:get handle-view-snippet-by-slug}] ["/snippets" {:get handle-view-snippets}] ["/snippet" {:post handle-create-snippet :get handle-view-snippet diff --git a/src/snippets/infra/db.clj b/src/snippets/infra/db.clj index 537ce1b..8346ef5 100644 --- a/src/snippets/infra/db.clj +++ b/src/snippets/infra/db.clj @@ -24,6 +24,9 @@ (defn get-snippet-by-id [snippet-id] (first (xt/q client ['#(from :snippets [{:xt/id %} slug title tags {:xt/id id} markdown pub-date]) snippet-id]))) +(defn get-snippet-by-slug [slug] + (first (xt/q client ['#(from :snippets [{:xt/id id} {:slug %} slug title tags markdown pub-date]) slug]))) + (defn put-snippet [id snippet] (t/log! {:level :info, :data {:snippet snippet :id id}} "Saving new snippet to db") (xt/execute-tx client [[:put-docs :snippets (merge snippet {:xt/id id})]])) @@ -43,3 +46,8 @@ (defn list-tags [] ;; (xt/q client '(-> (from :snippets [{:xt/id id} tags]) (unnest {:tag tags}) (without :tags) (aggregate tag {:ids (array-agg id)})))) (xt/q client '(-> (from :snippets [{:xt/id id} tags]) (unnest {:tag tags}) (without :tags) (aggregate tag {:count (count id)})))) + +(defn get-snippets-by-tag [tag] + (map #(dissoc % :tag) + (xt/q client (eval (read-string + (format "(quote (-> (from :snippets [title slug tags pub-date]) (unnest {:tag tags}) (without :tags) (where (= tag \"%s\"))))" tag)))))) diff --git a/src/snippets/use_cases/view.clj b/src/snippets/use_cases/view.clj index 98b66a6..4edbacd 100644 --- a/src/snippets/use_cases/view.clj +++ b/src/snippets/use_cases/view.clj @@ -13,3 +13,11 @@ (defn view-tags [] (t/log! {:level :info} "Viewing tags") (db/list-tags)) + +(defn view-snippets-by-tag [tag] + (t/log! {:level :info :data {:tag tag}} "Viewing snippet by tag") + (db/get-snippets-by-tag tag)) + +(defn view-snippet-by-slug [slug] + (t/log! {:level :info :data {:slug slug}} "Viewing snippet by slug") + (db/get-snippet-by-slug slug))