Refactoring for dependency injection based IO

This commit is contained in:
Charlotte Allen 2020-07-22 22:32:55 -07:00
parent 462fef7cf6
commit 1f3183d9f6
4 changed files with 35 additions and 29 deletions

View File

@ -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 [])]

View File

@ -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))

View File

@ -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))

View File

@ -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}]])