diff --git a/bruno/CodeSnippets/edit_snippet.bru b/bruno/CodeSnippets/edit_snippet.bru index fa6d897..dbfd39d 100644 --- a/bruno/CodeSnippets/edit_snippet.bru +++ b/bruno/CodeSnippets/edit_snippet.bru @@ -16,7 +16,7 @@ params:query { body:json { { - "title": "Updated from Bruno", - "tags": ["code", "mock"] + "title": "quick way to push last jj commit to git", + "tags": ["jj", "git"] } } diff --git a/bruno/CodeSnippets/get_snippets.bru b/bruno/CodeSnippets/get_snippets.bru index 8bf73c4..e2de66e 100644 --- a/bruno/CodeSnippets/get_snippets.bru +++ b/bruno/CodeSnippets/get_snippets.bru @@ -5,13 +5,13 @@ meta { } get { - url: {{host}}/api/snippets?limit=100&skip=0 + url: {{host}}/api/snippets?limit=25&skip=0 body: none auth: none } params:query { - limit: 100 + limit: 25 skip: 0 } diff --git a/bruno/CodeSnippets/get_tags.bru b/bruno/CodeSnippets/get_tags.bru new file mode 100644 index 0000000..a8a876b --- /dev/null +++ b/bruno/CodeSnippets/get_tags.bru @@ -0,0 +1,19 @@ +meta { + name: get_tags + type: http + seq: 8 +} + +get { + url: {{host}}/api/tags + body: none + auth: none +} + +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 1e96047..65e07f5 100644 --- a/src/snippets/infra/api.clj +++ b/src/snippets/infra/api.clj @@ -47,6 +47,11 @@ {:status 200 :body (format "Deleted snippet with id: %s if it existed" id)})) +(defn handle-view-tags [_args] + (let [tags (snippets.use-cases.view/view-tags)] + {:status 200 + :body tags})) + (defn wrap [handler id] (fn [request] (update (handler request) :wrap (fnil conj '()) id))) @@ -58,6 +63,7 @@ mm/wrap-format [wrap :api]]} ["/ping" {:get handle-ping}] + ["/tags" {:get handle-view-tags}] ["/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 1273cf5..8e52d91 100644 --- a/src/snippets/infra/db.clj +++ b/src/snippets/infra/db.clj @@ -39,3 +39,6 @@ (defn patch-snippet [id patch] (t/log! {:level :info, :data {:patch patch :id id}} "Patching snippet") (xt/execute-tx client [[:patch-docs :snippets (merge {:xt/id id} patch)]])) + +(defn list-tags [] + (xt/q client '(-> (from :snippets [{:xt/id id} tags]) (unnest {:tag tags}) (without :tags) (aggregate tag {:ids (array-agg id)})))) diff --git a/src/snippets/use_cases/view.clj b/src/snippets/use_cases/view.clj index ac6e83b..2eeba6e 100644 --- a/src/snippets/use_cases/view.clj +++ b/src/snippets/use_cases/view.clj @@ -9,3 +9,7 @@ (defn view-snippets [& args] (db/list-snippets args)) + +(defn view-tags [] + (t/log! {:level :info} "Viewing tags") + (map #(assoc % :count (count (:ids %))) (db/list-tags)))