get endpoint for listing snippets working
This commit is contained in:
parent
700710df56
commit
4acc4e2201
5 changed files with 57 additions and 10 deletions
|
|
@ -4,8 +4,17 @@ meta {
|
||||||
seq: 3
|
seq: 3
|
||||||
}
|
}
|
||||||
|
|
||||||
get {
|
post {
|
||||||
url:
|
url: {{host}}/api/snippet
|
||||||
body: none
|
body: json
|
||||||
auth: none
|
auth: none
|
||||||
}
|
}
|
||||||
|
|
||||||
|
body:json {
|
||||||
|
{
|
||||||
|
"title": "Test Snippet",
|
||||||
|
"slug": "test-snippet",
|
||||||
|
"markdown": "this is a test",
|
||||||
|
"tags": ["test"]
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
|
||||||
24
bruno/CodeSnippets/get_snippets.bru
Normal file
24
bruno/CodeSnippets/get_snippets.bru
Normal file
|
|
@ -0,0 +1,24 @@
|
||||||
|
meta {
|
||||||
|
name: get_snippets
|
||||||
|
type: http
|
||||||
|
seq: 4
|
||||||
|
}
|
||||||
|
|
||||||
|
get {
|
||||||
|
url: {{host}}/api/snippet?limit=10&skip=10
|
||||||
|
body: none
|
||||||
|
auth: none
|
||||||
|
}
|
||||||
|
|
||||||
|
params:query {
|
||||||
|
limit: 10
|
||||||
|
skip: 10
|
||||||
|
}
|
||||||
|
|
||||||
|
body:json {
|
||||||
|
{
|
||||||
|
"title": "Test Snippet",
|
||||||
|
"markdown": "## Cool Snippet\ndoes a cool thing",
|
||||||
|
"tags": ["git", "jj"]
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -2,7 +2,9 @@
|
||||||
(:require
|
(:require
|
||||||
[ring.adapter.jetty :as jetty]
|
[ring.adapter.jetty :as jetty]
|
||||||
[clojure.pprint :as pprint]
|
[clojure.pprint :as pprint]
|
||||||
|
[snippets.db :as db]
|
||||||
[muuntaja.middleware :as mm]
|
[muuntaja.middleware :as mm]
|
||||||
|
[ring.middleware.params]
|
||||||
[reitit.ring :as rr]))
|
[reitit.ring :as rr]))
|
||||||
|
|
||||||
(defn handle-ping [args]
|
(defn handle-ping [args]
|
||||||
|
|
@ -13,6 +15,12 @@
|
||||||
(pprint/pprint body)
|
(pprint/pprint body)
|
||||||
{:status 200, :body "snippet created"})
|
{: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})}))
|
||||||
|
|
||||||
(defn wrap [handler id]
|
(defn wrap [handler id]
|
||||||
(fn [request]
|
(fn [request]
|
||||||
(update (handler request) :wrap (fnil conj '()) id)))
|
(update (handler request) :wrap (fnil conj '()) id)))
|
||||||
|
|
@ -20,10 +28,12 @@
|
||||||
(def app
|
(def app
|
||||||
(rr/ring-handler
|
(rr/ring-handler
|
||||||
(rr/router
|
(rr/router
|
||||||
["/api" {:middleware [mm/wrap-format
|
["/api" {:middleware [ring.middleware.params/wrap-params
|
||||||
|
mm/wrap-format
|
||||||
[wrap :api]]}
|
[wrap :api]]}
|
||||||
["/ping" {:get handle-ping}]
|
["/ping" {:get handle-ping}]
|
||||||
["/snippet" {:post handle-create-snippet}]])
|
["/snippet" {:post handle-create-snippet
|
||||||
|
:get handle-view-snippet}]])
|
||||||
(rr/create-default-handler)))
|
(rr/create-default-handler)))
|
||||||
|
|
||||||
;; (defroutes app-routes
|
;; (defroutes app-routes
|
||||||
|
|
|
||||||
|
|
@ -21,5 +21,5 @@
|
||||||
:tags (:snippet_types frontmatter))))
|
:tags (:snippet_types frontmatter))))
|
||||||
(map #(dissoc % :full-path)))))
|
(map #(dissoc % :full-path)))))
|
||||||
|
|
||||||
;; create snippets from files
|
;; used repl to do backfill
|
||||||
;; save snippets in db
|
;; (doseq [s old-snippets] (xt/execute-tx db/client [[:put-docs :snippets (merge {:xt/id (:slug s)} s)]]))
|
||||||
|
|
|
||||||
|
|
@ -15,7 +15,11 @@
|
||||||
|
|
||||||
(defn uuid [] (str (java.util.UUID/randomUUID)))
|
(defn uuid [] (str (java.util.UUID/randomUUID)))
|
||||||
|
|
||||||
(defn save-snippet [snippet]
|
;; xtdb query docs: https://docs.xtdb.com/reference/main/xtql/queries.html#_limit
|
||||||
(xt/execute-tx client [[:put-docs :snippets (merge snippet {:xt/id (uuid)})]]))
|
(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)))))
|
||||||
|
|
||||||
;; (xt/q client ['(from :users [{:name "travis"}])])
|
;; (xt/execute-tx client [[:put-docs :snippets (merge snippet {:xt/id (uuid)})]]))
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue