diff --git a/bruno/CodeSnippets/create_snippet.bru b/bruno/CodeSnippets/create_snippet.bru index 638f2b0..5972683 100644 --- a/bruno/CodeSnippets/create_snippet.bru +++ b/bruno/CodeSnippets/create_snippet.bru @@ -12,8 +12,8 @@ post { body:json { { - "title": "Test Snippet", - "slug": "test-snippet", + "title": "TEST from Bruno", + "slug": "bruno-test", "markdown": "this is a test", "tags": ["test"] } diff --git a/bruno/CodeSnippets/delete_snippet.bru b/bruno/CodeSnippets/delete_snippet.bru new file mode 100644 index 0000000..823c7eb --- /dev/null +++ b/bruno/CodeSnippets/delete_snippet.bru @@ -0,0 +1,23 @@ +meta { + name: delete_snippet + type: http + seq: 5 +} + +delete { + url: {{host}}/api/snippet?id=952cdf1a-a2a8-4fe2-9354-953b240136ae + body: none + auth: none +} + +params:query { + id: 952cdf1a-a2a8-4fe2-9354-953b240136ae +} + +body:json { + { + "title": "Test Snippet", + "markdown": "## Cool Snippet\ndoes a cool thing", + "tags": ["git", "jj"] + } +} diff --git a/bruno/CodeSnippets/environments/local.bru b/bruno/CodeSnippets/environments/local.bru index 529e6ae..2e017ca 100644 --- a/bruno/CodeSnippets/environments/local.bru +++ b/bruno/CodeSnippets/environments/local.bru @@ -1,3 +1,3 @@ vars { - host: http://localhost:3000 + host: http://localhost:8080 } diff --git a/bruno/CodeSnippets/get_snippets.bru b/bruno/CodeSnippets/get_snippets.bru index 6ee7097..a61d3f4 100644 --- a/bruno/CodeSnippets/get_snippets.bru +++ b/bruno/CodeSnippets/get_snippets.bru @@ -5,13 +5,13 @@ meta { } get { - url: 192.168.1.157:29406/api/snippet?limit=4&skip=0 + url: {{host}}/api/snippet?limit=100&skip=0 body: none auth: none } params:query { - limit: 4 + limit: 100 skip: 0 } diff --git a/deps.edn b/deps.edn index a191441..6b12105 100644 --- a/deps.edn +++ b/deps.edn @@ -9,6 +9,8 @@ com.github.seancorfield/next.jdbc {:mvn/version "1.3.1002"} org.postgresql/postgresql {:mvn/version "42.7.6"} frontmatter/frontmatter {:mvn/version "0.0.1"} + ;; logging + com.taoensso/telemere {:mvn/version "1.0.0"} ;; environment variables environ/environ {:mvn/version "1.2.0"} diff --git a/src/snippets/api.clj b/src/snippets/api.clj index 6275199..3d59d97 100644 --- a/src/snippets/api.clj +++ b/src/snippets/api.clj @@ -2,7 +2,8 @@ (:require [ring.adapter.jetty :as jetty] [clojure.pprint :as pprint] - [snippets.db :as db] + [taoensso.telemere :as t] + [snippets.core :as core] [snippets.config :as config] [muuntaja.middleware :as mm] [ring.middleware.params] @@ -13,14 +14,21 @@ {:status 200, :body "ok"}) (defn handle-create-snippet [{body :body-params}] - (pprint/pprint body) + (t/log! {:level :info, :data {:body body}} "Received request to create snippet") + (core/create-snippet body) {:status 200, :body "snippet created"}) (defn handle-view-snippet [{params :query-params}] (let [limit (Integer/parseInt (get params "limit" "10")) skip (Integer/parseInt (get params "skip" "0"))] {:status 200 - :body (db/list-snippets {:limit limit :skip skip})})) + :body (core/list-snippets {:limit limit :skip skip})})) + +(defn handle-delete-snippet [{params :query-params}] + (let [id (get params "id")] + (core/delete-snippet id) + {:status 200 + :body (format "Deleted snippet with id: %s if it existed" id)})) (defn wrap [handler id] (fn [request] @@ -34,7 +42,8 @@ [wrap :api]]} ["/ping" {:get handle-ping}] ["/snippet" {:post handle-create-snippet - :get handle-view-snippet}]]) + :get handle-view-snippet + :delete handle-delete-snippet}]]) (rr/create-default-handler))) ;; (defroutes app-routes diff --git a/src/snippets/core.clj b/src/snippets/core.clj index 9ac9c0f..11a8ad6 100644 --- a/src/snippets/core.clj +++ b/src/snippets/core.clj @@ -1 +1,19 @@ -(ns snippets.core) +(ns snippets.core + (:require + [taoensso.telemere :as t] + [snippets.db :as db])) + +(defn- uuid [] (str (java.util.UUID/randomUUID))) + +(defn create-snippet [{:keys [title slug markdown tags]}] + (let [id (uuid) + pub-date (java.util.Date.)] + (t/log! {:level :info, :data {:title title :slug slug :id id}} "Creating snippet") + (db/put-snippet id {:title title :slug slug :markdown markdown :tags tags :pub-date pub-date}))) + +(defn list-snippets [& args] + (db/list-snippets args)) + +(defn delete-snippet [key] + (t/log! {:level :info, :data {:key key}} "Deleting snippet by id") + (db/delete-snippet key)) diff --git a/src/snippets/db.clj b/src/snippets/db.clj index a2df8b5..c5f419b 100644 --- a/src/snippets/db.clj +++ b/src/snippets/db.clj @@ -1,5 +1,6 @@ (ns snippets.db (:require + [taoensso.telemere :as t] [snippets.config :as config] [xtdb.api :as xt])) @@ -13,13 +14,21 @@ ;; :password "xtdb" :dbname "xtdb"}))) -(defn uuid [] (str (java.util.UUID/randomUUID))) - ;; xtdb query docs: https://docs.xtdb.com/reference/main/xtql/queries.html#_limit (defn list-snippets [{:keys [skip limit]}] (xt/q client (eval (read-string - (format "(quote (-> (from :snippets [* {:xt/id id}]) (offset %s) (limit %s)))" skip limit))))) + (format "(quote (-> (from :snippets [* {:xt/id id}]) (order-by {:val pub-date, :dir :desc, :nulls :last}) (offset %s) (limit %s)))" skip limit))))) - ;; (xt/execute-tx client [[:put-docs :snippets (merge snippet {:xt/id (uuid)})]])) +(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})]])) + +(defn delete-snippet [id] + (t/log! {:level :info, :data {:id id}} "Deleting snippet") + (xt/execute-tx client [[:delete-docs :snippets id]])) + +(defn erase-snippet [id] + (t/log! {:level :info, :data {:id id}} "Erasing snippet, aka removing it completely through out time") + (xt/execute-tx client [[:erase-docs :snippets id]]))