Created
April 16, 2012 02:32
-
-
Save yogthos/2396073 to your computer and use it in GitHub Desktop.
dead simple web app
This file contains hidden or 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 model | |
(:require [clojure.java.jdbc :as sql])) | |
(def db {:classname "org.hsqldb.jdbcDriver" | |
:subprotocol "hsqldb" | |
:subname "user.db" | |
:create true}) | |
(defn db-read [query] | |
(sql/with-connection db | |
(sql/with-query-results res query (doall res)))) | |
(defn add-user [id first-name last-name] | |
(sql/with-connection db | |
(sql/insert-rows :users [id first-name last-name]))) | |
(defn get-users [] | |
(db-read ["select id, first, last from users"])) |
This file contains hidden or 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 my-website.views.users | |
(:use noir.core hiccup.core hiccup.page hiccup.form) | |
(:require model)) | |
(defn display-users [] | |
[:table | |
[:thead [:tr [:th "ID"] [:th "First Name"] [:th "Last Name"]]] | |
(into [:tbody] | |
(for [user (model/get-users)] | |
[:tr [:td (:id user)] [:td (:first user)] [:td (:last user)]]))]) | |
(defpage "/users" {id :id, first-name :first last-name :last error :error} | |
(html5 | |
[:head (include-css "css/index.css")] | |
[:body | |
(when error [:p "Error has occured while processing the request: " error]) | |
(form-to [:post "/users"] | |
(label "id" "id") (text-field "id" id) | |
[:br] | |
(label "first" "first") (text-field "first-name" first-name) | |
[:br] | |
(label "last" "last") (text-field "last" last-name) | |
[:br] | |
(submit-button "add user")) | |
[:hr] | |
(display-users)])) | |
(defpage [:post "/users"] {id :id, first-name :first last-name :last} | |
(try | |
(model/add-user id first-name last-name) | |
(render "/users") | |
(catch Exception ex | |
(render "/users" {:id id :first first-name :last last-name :error (.getMessage ex)})))) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment