Add posts #1
2
.gitignore
vendored
2
.gitignore
vendored
|
@ -14,3 +14,5 @@ profiles.clj
|
|||
|
||||
/node_modules
|
||||
/log
|
||||
*.swp
|
||||
resources/posts/
|
||||
|
|
18
src/clj/shapey_shifty/posts/core.clj
Normal file
18
src/clj/shapey_shifty/posts/core.clj
Normal file
|
@ -0,0 +1,18 @@
|
|||
(ns shapey-shifty.posts.core)
|
||||
|
||||
(defn create-empty-post [] {:type nil :properties {:name nil :author nil :published nil :content nil}})
|
||||
|
||||
(defn set-publish-date [post date]
|
||||
(assoc-in post [:properties :published] date))
|
||||
|
||||
(defn set-type [post post-type]
|
||||
(assoc post :type post-type))
|
||||
|
||||
(defn set-author [post author]
|
||||
(assoc-in post [:properties :author] author))
|
||||
|
||||
(defn set-name [post post-name]
|
||||
(assoc-in post [:properties :name] post-name))
|
||||
|
||||
(defn set-content [post post-content]
|
||||
(assoc-in post [:properties :content] post-content))
|
37
src/clj/shapey_shifty/posts/posts_io.clj
Normal file
37
src/clj/shapey_shifty/posts/posts_io.clj
Normal file
|
@ -0,0 +1,37 @@
|
|||
(ns shapey-shifty.posts.posts-io
|
||||
(:require
|
||||
[shapey-shifty.posts.core :as core]))
|
||||
|
||||
(def post-filename "post.json")
|
||||
(def base-path "resources/posts")
|
||||
|
||||
(defn create-path-by-date [year month day]
|
||||
{:year year :month month :day day})
|
||||
|
||||
(defn pathmap-to-path [{:keys [year month day]}]
|
||||
(format "%d/%d/%d" year month day))
|
||||
|
||||
(defn count-posts-in-date [dt-path]
|
||||
(let [path (pathmap-to-path dt-path)
|
||||
final-path (format "%s/%s" base-path path)]
|
||||
(->> final-path
|
||||
clojure.java.io/file
|
||||
file-seq
|
||||
(filter #(.isDirectory %))
|
||||
count
|
||||
dec)))
|
||||
|
||||
(defn write-post [post dt-path]
|
||||
(let [path (pathmap-to-path dt-path)
|
||||
increment (inc (count-posts-in-date dt-path))
|
||||
final-path (format "%s/%s/%d/%s" base-path path increment post-filename)]
|
||||
(clojure.java.io/make-parents final-path)
|
||||
(spit final-path post)))
|
||||
|
||||
(defn read-post [dt-path n]
|
||||
(let [path (format "%s/%s/%d/%s" base-path (pathmap-to-path dt-path) n post-filename)
|
||||
f (clojure.java.io/file path)]
|
||||
(when (.exists f)
|
||||
(-> f
|
||||
slurp
|
||||
read-string))))
|
Loading…
Reference in a new issue