54 lines
2.1 KiB
Clojure
54 lines
2.1 KiB
Clojure
(ns cli-cms.edit
|
|
(:require
|
|
[babashka.http-client :as http]
|
|
[clojure.pprint :refer [pprint]]
|
|
[cli-cms.config :refer [config]]
|
|
[cli-cms.view :as view]
|
|
[cheshire.core :as json]
|
|
[cli-cms.cli-utils :as utils]))
|
|
|
|
(defn fetch-snippets []
|
|
(let [res (http/get (str (:backend-host config) "/api/snippets?limit=25"))]
|
|
(json/parse-string (:body res) true)))
|
|
|
|
(defn prompt-for-edit-type []
|
|
(utils/select (map #(hash-map :value (name %) :item %)'(
|
|
:title
|
|
:markdown
|
|
:slug
|
|
:remove-tag
|
|
:add-tags
|
|
:done
|
|
))))
|
|
|
|
(defn send-patch [id patch]
|
|
(http/patch (str (:backend-host config) "/api/snippet")
|
|
{:query-params {:id id}
|
|
:headers {:content-type "application/json"}
|
|
:body (json/encode patch)}))
|
|
|
|
(defn edit
|
|
([snippet]
|
|
(edit snippet {}))
|
|
([snippet changes]
|
|
(case (prompt-for-edit-type)
|
|
:title (let [new-title (utils/prompt-for "title" :prefill (:title snippet))]
|
|
(edit (assoc snippet :title new-title) (assoc changes :title new-title)))
|
|
:slug (let [new-slug (utils/prompt-for "slug" :prefill (:slug snippet))]
|
|
(edit (assoc snippet :slug new-slug) (assoc changes :slug new-slug)))
|
|
:remove-tag (let [tag-to-remove (utils/select (map #(hash-map :value % :item %) (:tags snippet)))
|
|
tags (remove #(= % tag-to-remove) (:tags snippet))]
|
|
(edit (assoc snippet :tags tags) (assoc changes :tags tags)))
|
|
:add-tags (let [
|
|
new-tags (utils/prompt-for-many "tags")
|
|
tags (concat new-tags (:tags snippet))]
|
|
(edit (assoc snippet :tags tags) (assoc changes :tags tags)))
|
|
:markdown (let [new-markdown (utils/prompt-for-long-form "markdown" :prefill (:markdown snippet))]
|
|
(edit (assoc snippet :markdown new-markdown) (assoc changes :markdown new-markdown)))
|
|
:done changes)))
|
|
|
|
(defn run []
|
|
(let [snippet-to-edit (utils/select (map #(hash-map :value (:slug %) :item %) (fetch-snippets)))]
|
|
(send-patch (:id snippet-to-edit) (edit snippet-to-edit))
|
|
(println "Snippet updated successfully")
|
|
(view/view (view/fetch-snippet (:id snippet-to-edit)))))
|