From a268607e980cbf6c9e912b18dda063259c128949 Mon Sep 17 00:00:00 2001 From: Charlotte Allen Date: Mon, 27 Jan 2020 23:14:01 -0800 Subject: [PATCH] Playing around with routing --- resources/html/post.html | 9 +++++++ src/clj/shapey_shifty/posts/posts_io.clj | 4 ++-- src/clj/shapey_shifty/routes/home.clj | 25 ++++++++++++++++---- src/clj/shapey_shifty/routes/post_router.clj | 7 ++++++ 4 files changed, 38 insertions(+), 7 deletions(-) create mode 100644 resources/html/post.html create mode 100644 src/clj/shapey_shifty/routes/post_router.clj diff --git a/resources/html/post.html b/resources/html/post.html new file mode 100644 index 0000000..eed501c --- /dev/null +++ b/resources/html/post.html @@ -0,0 +1,9 @@ +{% extends "base.html" %} +{% block content %} +
+

{{post.name}}

+
+

{{post.content}}

+
+
+{% endblock %} diff --git a/src/clj/shapey_shifty/posts/posts_io.clj b/src/clj/shapey_shifty/posts/posts_io.clj index 7ff20c6..fd0112a 100644 --- a/src/clj/shapey_shifty/posts/posts_io.clj +++ b/src/clj/shapey_shifty/posts/posts_io.clj @@ -9,7 +9,7 @@ {:year year :month month :day day}) (defn pathmap-to-path [{:keys [year month day]}] - (format "%d/%d/%d" year month day)) + (format "%s/%s/%s" year month day)) (defn count-posts-in-date [dt-path] (let [path (pathmap-to-path dt-path) @@ -29,7 +29,7 @@ (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) + (let [path (format "%s/%s/%s/%s" base-path (pathmap-to-path dt-path) n post-filename) f (clojure.java.io/file path)] (when (.exists f) (-> f diff --git a/src/clj/shapey_shifty/routes/home.clj b/src/clj/shapey_shifty/routes/home.clj index feb8865..9334e72 100644 --- a/src/clj/shapey_shifty/routes/home.clj +++ b/src/clj/shapey_shifty/routes/home.clj @@ -1,14 +1,28 @@ (ns shapey-shifty.routes.home (:require - [shapey-shifty.layout :as layout] - [clojure.java.io :as io] - [shapey-shifty.middleware :as middleware] - [ring.util.response] - [ring.util.http-response :as response])) + [shapey-shifty.layout :as layout] + [clojure.java.io :as io] + [shapey-shifty.middleware :as middleware] + [ring.util.response] + [shapey-shifty.posts.core :as posts] + [shapey-shifty.posts.posts-io :as post-io] + [shapey-shifty.routes.post-router :as post-router] + [ring.util.http-response :as response])) + +(def p (atom 0)) (defn home-page [request] (layout/render request "home.html" {:docs (-> "docs/docs.md" io/resource slurp)})) +(defn test-view [request] + (layout/render request "post.html" {:post (-> (posts/create-empty-post) (posts/set-content "Hey there everyone!") (posts/set-name "Yolo") :properties)})) + +(defn post-view [request] + (let [{:keys [path-params query-params body-params]} request + {:keys [year month day n]} path-params] + (do (reset! p request) + (layout/render request "post.html" {:post (:properties (post-router/get-post year month day n))})))) + (defn about-page [request] (layout/render request "about.html")) @@ -17,5 +31,6 @@ {:middleware [middleware/wrap-csrf middleware/wrap-formats]} ["/" {:get home-page}] + ["/blog/:year/:month/:day/:n" {:get post-view}] ["/about" {:get about-page}]]) diff --git a/src/clj/shapey_shifty/routes/post_router.clj b/src/clj/shapey_shifty/routes/post_router.clj new file mode 100644 index 0000000..140b51b --- /dev/null +++ b/src/clj/shapey_shifty/routes/post_router.clj @@ -0,0 +1,7 @@ +(ns shapey-shifty.routes.post-router + (:require [shapey-shifty.posts.core :as po] + [shapey-shifty.posts.posts-io :as io])) + +(defn get-post + ([dt-path n] (io/read-post dt-path n)) + ([year month day n] (#(io/read-post % n) (io/create-path-by-date year month day))))