From c787cacc522f9eb5b4277e4f1890d4d9d6bc3f14 Mon Sep 17 00:00:00 2001 From: Charlotte Allen Date: Mon, 27 Jan 2020 18:54:24 -0800 Subject: [PATCH] Implement basic post recording --- .gitignore | 1 + src/clj/shapey_shifty/posts/posts_io.clj | 35 ++++++++++++++++++++++++ 2 files changed, 36 insertions(+) create mode 100644 src/clj/shapey_shifty/posts/posts_io.clj diff --git a/.gitignore b/.gitignore index e4c8262..8885ace 100644 --- a/.gitignore +++ b/.gitignore @@ -15,3 +15,4 @@ profiles.clj /node_modules /log *.swp +resources/posts/ diff --git a/src/clj/shapey_shifty/posts/posts_io.clj b/src/clj/shapey_shifty/posts/posts_io.clj new file mode 100644 index 0000000..fee6cd2 --- /dev/null +++ b/src/clj/shapey_shifty/posts/posts_io.clj @@ -0,0 +1,35 @@ +(ns shapey-shifty.posts.posts-io + (:require + [clojure.data.json :as json] + [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 jsonify-post [post] + (json/write-str post)) + +(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))) +