Skip to content

Instantly share code, notes, and snippets.

@klgraham
Created August 19, 2013 19:01
Show Gist options
  • Save klgraham/6272748 to your computer and use it in GitHub Desktop.
Save klgraham/6272748 to your computer and use it in GitHub Desktop.
(ns testsite.views.welcome
(:require [monger.core :as mg]
[testsite.login :as auth]
[testsite.views.common :as common])
(:use [monger.result :only [ok?]]
[monger.collection :only [insert find-maps find-one-as-map]])
(:import [org.bson.types ObjectId]))
;;;; MongoDB interaction functions
(def ^:const record-types #{"book"})
(defn get-record
"Gets a record from the database of given type and name."
[record-type record-name]
(if (contains? record-types record-type)
(find-one-as-map "inventory" {:type record-type :name record-name})
"Sorry, but that record-type does not exist."))
(defn get-records-by-type
"Gets all records of given type"
[record-type]
(if (contains? record-types record-type)
(find-maps "inventory" {:type record-type})
"Sorry, but that record-type does not exist."))
; (defn welcome []
; (common/layout [:p "Hi"]))
; ;;;; The actual webpage
(defn welcome []
; (mg/connect-via-uri! "mongodb://127.0.0.1/mongo-test") ; local
(mg/connect-via-uri! (System/genenv "MONGOHQ_URL")) ; cloud
(insert "inventory" {:_id (ObjectId.) :type "book" :name "Dune" :author "Frank Herbert" :genre "Science Fiction"})
(let [books (get-records-by-type "book")
book-string ((comp :name first) books)]
; add stuff to the db then write to screen
(common/layout
[:p (str "Welcome to testsite:" book-string) ])))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment