add get by slug and tag

This commit is contained in:
Travis Shears 2025-06-16 15:42:52 +02:00
parent e11719b40f
commit 6bbdb9c348
5 changed files with 79 additions and 0 deletions

View file

@ -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"]
}
}

View file

@ -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"]
}
}

View file

@ -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

View file

@ -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))))))

View file

@ -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))