From 1f3183d9f6e1e6bbf281c22a9d009c3e0939a471 Mon Sep 17 00:00:00 2001 From: Charlotte Allen Date: Wed, 22 Jul 2020 22:32:55 -0700 Subject: [PATCH] Refactoring for dependency injection based IO --- src/clj/shapey_shifty/index/index.clj | 5 ++-- src/clj/shapey_shifty/posts/core.clj | 20 ++++++++----- src/clj/shapey_shifty/posts/posts_io.clj | 36 +++++++++++++----------- src/clj/shapey_shifty/routes/home.clj | 3 +- 4 files changed, 35 insertions(+), 29 deletions(-) diff --git a/src/clj/shapey_shifty/index/index.clj b/src/clj/shapey_shifty/index/index.clj index 52b7025..2b6ae1e 100644 --- a/src/clj/shapey_shifty/index/index.clj +++ b/src/clj/shapey_shifty/index/index.clj @@ -4,9 +4,8 @@ [duratom.core :as dur] [shapey-shifty.posts.posts-io :as post-io])) -(def index-path (atom "resources/index")) - -(s/def ::index (s/keys :req [::filename ::key ::created-date ::stub])) +(s/def ::index-item (s/keys :req [::filename ::key ::created-date ::stub])) +(s/def ::index (s/coll-of ::index-item)) (defn create-index [index-path] (let [index (dur/duratom :local-file :file-path index-path :init [])] diff --git a/src/clj/shapey_shifty/posts/core.clj b/src/clj/shapey_shifty/posts/core.clj index 8d50e6a..3aa38bf 100644 --- a/src/clj/shapey_shifty/posts/core.clj +++ b/src/clj/shapey_shifty/posts/core.clj @@ -1,33 +1,39 @@ (ns shapey-shifty.posts.core (:require [clojure.spec.alpha :as s])) -(def posts (atom [])) - (defn create-empty-post [] {::type :note ::key (java.util.UUID/randomUUID) ::content "" ::properties {::name nil ::author nil ::published nil + ::created (java.time.LocalDateTime/now) ::stub nil ::filename nil ::status :preview}}) (s/def ::name (s/nilable string?)) -(s/def ::author string?) -(s/def ::published keyword?) -(s/def ::content string?) -(s/def ::stub string?) +(s/def ::author (s/nilable string?)) +(s/def ::published (s/nilable keyword?)) +(s/def ::content (s/nilable string?)) +(s/def ::stub (s/nilable string?)) (s/def ::filename (s/nilable string?)) (s/def ::type keyword?) (s/def ::key uuid?) (s/def ::status keyword?) (s/def ::properties - (s/keys :req [::name ::author ::published ::stub ::filename])) + (s/keys :req [::name ::author ::published ::stub ::filename ::created])) (s/def ::post (s/keys :req [::type ::properties ::content])) +(defprotocol PostKeeper + (create-post [this post]) + (search-posts [this post-filter index]) + (get-all-posts [this]) + (update-post [this post]) + (delete-post [this post])) + (defn set-publish-date [post date] (assoc-in post [:properties :published] date)) diff --git a/src/clj/shapey_shifty/posts/posts_io.clj b/src/clj/shapey_shifty/posts/posts_io.clj index 61c680c..0a020c2 100644 --- a/src/clj/shapey_shifty/posts/posts_io.clj +++ b/src/clj/shapey_shifty/posts/posts_io.clj @@ -3,13 +3,7 @@ [shapey-shifty.posts.core :as core] [shapey-shifty.authors.author-core :as author])) -(defprotocol PostKeeper - (create-post [this post]) - (get-posts [this]) - (update-post [this post]) - (delete-post [this post])) - -(def post-filename "post.json") +(def post-filename "post.edn") (def base-posts-path "resources/posts") (defn create-path-by-date [year month day] @@ -50,20 +44,28 @@ f (clojure.java.io/file path)] (read-post f)))) -(defn datetime-filename-resolver [base-path filename post] - (if (:filename post) (:filename post))) +(defn datetime-filename-resolver [post] + (if (:filename post) + (:filename post))) -(defrecord FileBasedPostKeeper [filename-resolver base-path filename] - PostKeeper +(defrecord FileBasedPostKeeper [filename-resolver base-path filename index] + core/PostKeeper (create-post [this post] (let [path (str base-path (filename-resolver post) filename)] (clojure.java.io/make-parents path) (spit path post))) - (get-posts [this] (->> base-path - clojure.java.io/file - file-seq - (filter #(.isDirectory %)) - (filter #(.exists %)) - read-post)) + (search-posts [this post-filter index] + (-> index + post-filter + (partial map #(:filename %)) + (partial map #(clojure.java.io/file)) + file-seq + (partial map read-post))) + (get-all-posts [this] + (->> base-path + clojure.java.io/file + file-seq + (filter #(.isFile %)) + (map read-post))) (update-post [this post] nil) (delete-post [this post] nil)) diff --git a/src/clj/shapey_shifty/routes/home.clj b/src/clj/shapey_shifty/routes/home.clj index fba1bbc..dcfe781 100644 --- a/src/clj/shapey_shifty/routes/home.clj +++ b/src/clj/shapey_shifty/routes/home.clj @@ -31,5 +31,4 @@ {:middleware @middleware} ["/" {:get home-page}] ["/about/:name" {:get about-page}] - ["/:year/:month/:day/:n" {:get post-view}]]) - + ["/blog/:year/:month/:day/:n" {:get post-view}]])