Created
November 8, 2011 01:17
-
-
Save tensorpudding/1346742 to your computer and use it in GitHub Desktop.
more clojure webapp goodness
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(ns blog.models.posts | |
(:require blog.db [clj-time.core :as ctime] [clj-time.format :as format] | |
[clj-time.coerce :as coerce]) | |
(:use korma.core)) | |
(defentity posts | |
(pk :pid) | |
(database blog.db/db)) | |
(defn sql-current-time "Current timestamp in SQL Timestamp format" | |
[] (java.sql.Timestamp. ^long (coerce/to-long (ctime/now)))) | |
(defn parse-sql-time "Parse SQL timestamp to a string" | |
[sqltime] (.toString sqltime)) | |
(defn new-post "Store new post in database" | |
[user title body] (insert posts | |
(values {:uid user | |
:time (sql-current-time) | |
:title title | |
:body body}))) | |
(defn get-post "Get post by pid" | |
[pid] (let [result ((select posts | |
(where {:pid [= pid]}) | |
(limit 1)) 0)] | |
(assoc result :time (parse-sql-time (result :time))))) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(ns blog.routes | |
(:require [noir.core :as noir] | |
[blog.views.view :as view])) | |
(noir/defpage [:get ["/view/:pid" :pid #"\d+"]] {:keys [pid]} | |
(view/display pid)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(ns blog.views.view | |
(:require [blog.models.posts :as posts] | |
[blog.views.notfound :as notfound] | |
[net.cgrand.enlive-html :as enlive])) | |
(enlive/deftemplate view-template "blog/views/view.html" | |
[title date body] | |
[:title] (enlive/content title) | |
[:#title] (enlive/content title) | |
[:#date] (enlive/content date) | |
[:#body] (enlive/content body)) | |
(defn display "Display view" | |
[pid] (let [res (posts/get-post pid)] | |
(if (empty? res) | |
(notfound/display) | |
(let [title (res :title) | |
date (res :time) | |
body (res :body)] | |
(view-template title date body))))) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment