aboutsummaryrefslogtreecommitdiff
path: root/src/dionysus/web/admin/home/blacklist.clj
diff options
context:
space:
mode:
Diffstat (limited to 'src/dionysus/web/admin/home/blacklist.clj')
-rw-r--r--src/dionysus/web/admin/home/blacklist.clj106
1 files changed, 106 insertions, 0 deletions
diff --git a/src/dionysus/web/admin/home/blacklist.clj b/src/dionysus/web/admin/home/blacklist.clj
new file mode 100644
index 0000000..81ac39d
--- /dev/null
+++ b/src/dionysus/web/admin/home/blacklist.clj
@@ -0,0 +1,106 @@
+(ns dionysus.web.admin.home.blacklist
+ (:require [clojure.string :as cstr]
+ [dionysus.blacklist :as dblacklist]
+ [dionysus.spotify :as dspotify]
+ [dionysus.web.utils :as dwutils]
+ [ring.util.response :as ruresp]))
+
+(defn- blacklist-table-item [index edited-item]
+ (let [item (nth @dblacklist/blacklist index)
+ editor? (= (str index) edited-item)]
+ [:tr
+ [:td (when-not editor? (cstr/upper-case (:type item)))]
+ [:td (if editor?
+ [:input {:type :text :name "link" :value (:url item)}]
+ [:a {:href (:url item) :target "_blank"}
+ (condp = (:type item)
+ "track"
+ (-> (:id item)
+ dspotify/get-track!
+ :name)
+
+ "artist"
+ (-> (:id item)
+ dspotify/get-artist!
+ :name)
+
+ nil)])]
+ (if editor?
+ [:td
+ [:img {:src "/assets/icons/check.svg"
+ :class "icon"
+ :hx-trigger "click"
+ :hx-post (str "/blacklist/change/" index)
+ :hx-target "#blacklist"
+ :hx-swap "outerHTML"
+ :hx-include "previous input"}]
+ [:img {:src "/assets/icons/trash.svg"
+ :class "icon"
+ :hx-trigger "click"
+ :hx-delete (str "/blacklist/delete/" index)
+ :hx-target "#blacklist"
+ :hx-swap "outerHTML"}]]
+ [:td
+ [:img {:src "/assets/icons/edit.svg"
+ :class "icon"
+ :hx-trigger "click"
+ :hx-get (str "/blacklist?editing=" index)
+ :hx-target "#blacklist"
+ :hx-swap "outerHTML"}]])]))
+
+(defn render-blacklist-table [edited-item]
+ [:table {:id "blacklist"}
+ [:thead
+ [:tr
+ [:th "Typ"]
+ [:th "Link"]
+ [:th "Aktionen"]]]
+ [:tbody
+ (for [index (-> @dblacklist/blacklist
+ .length
+ range)]
+ (blacklist-table-item index edited-item))
+ [:tr
+ [:td]
+ [:td [:input {:type :text
+ :placeholder "Link to track or artist"
+ :name "link"}]]
+ [:td [:img {:src "/assets/icons/plus.svg"
+ :class "icon"
+ :hx-trigger "click"
+ :hx-post "/blacklist/add"
+ :hx-target "#blacklist"
+ :hx-swap "outerHTML"
+ :hx-include "previous input"}]]]]])
+
+(defn handle [req]
+ (-> (get-in req [:query-params "editing"])
+ render-blacklist-table
+ dwutils/render-html
+ ruresp/response))
+
+(defn handle-add [req]
+ (-> req
+ (get-in [:form-params "link"])
+ dblacklist/add)
+ (handle {}))
+
+(defn- valid-index? [s]
+ (and (string? s) (string? (re-matches #"[0-9][0-9]*" s))))
+
+(defn handle-change [req]
+ (let [index (get-in req [:path-params :index])
+ link (get-in req [:form-params "link"])]
+ (if (and (valid-index? index) (string? link))
+ (do (-> index
+ Integer/parseInt
+ (dblacklist/change link))
+ (handle {}))
+ (ruresp/bad-request "Bad request."))))
+
+(defn handle-delete [req]
+ (let [index (get-in req [:path-params :index])]
+ (if (valid-index? index)
+ (do (dblacklist/delete (Integer/parseInt index))
+ (handle {}))
+ (ruresp/bad-request "Bad request."))))