Set up the database tables and connection
This commit is contained in:
parent
b6ede2c2b0
commit
d2d60cf48d
6 changed files with 81 additions and 37 deletions
|
@ -18,7 +18,11 @@
|
||||||
[javax.mail/mail
|
[javax.mail/mail
|
||||||
javax.jms/jms
|
javax.jms/jms
|
||||||
com.sun.jdmk/jmxtools
|
com.sun.jdmk/jmxtools
|
||||||
com.sun.jmx/jmxri]]]
|
com.sun.jmx/jmxri]]
|
||||||
|
[clj-http "0.7.7"]
|
||||||
|
[image-resizer "0.1.6"]
|
||||||
|
[enlive "1.1.1"]
|
||||||
|
[me.raynes/fs "1.4.5"]]
|
||||||
:ring
|
:ring
|
||||||
{:handler ldview.handler/war-handler,
|
{:handler ldview.handler/war-handler,
|
||||||
:init ldview.handler/init,
|
:init ldview.handler/init,
|
||||||
|
|
|
@ -4,7 +4,8 @@
|
||||||
[noir.util.middleware :as middleware]
|
[noir.util.middleware :as middleware]
|
||||||
[compojure.route :as route]
|
[compojure.route :as route]
|
||||||
[taoensso.timbre :as timbre]
|
[taoensso.timbre :as timbre]
|
||||||
[com.postspectacular.rotor :as rotor]))
|
[com.postspectacular.rotor :as rotor]
|
||||||
|
[ldview.models.schema :as schema]))
|
||||||
|
|
||||||
(defroutes app-routes
|
(defroutes app-routes
|
||||||
(route/resources "/")
|
(route/resources "/")
|
||||||
|
@ -28,6 +29,8 @@
|
||||||
[:shared-appender-config :rotor]
|
[:shared-appender-config :rotor]
|
||||||
{:path "ldview.log" :max-size (* 512 1024) :backlog 10})
|
{:path "ldview.log" :max-size (* 512 1024) :backlog 10})
|
||||||
|
|
||||||
|
(if-not (schema/initialized?) (schema/create-tables))
|
||||||
|
|
||||||
(timbre/info "ldview started successfully"))
|
(timbre/info "ldview started successfully"))
|
||||||
|
|
||||||
(defn destroy
|
(defn destroy
|
||||||
|
|
|
@ -5,20 +5,37 @@
|
||||||
|
|
||||||
(defdb db schema/db-spec)
|
(defdb db schema/db-spec)
|
||||||
|
|
||||||
(defentity users)
|
(declare entities contests users sources)
|
||||||
|
|
||||||
(defn create-user [user]
|
(defentity entries
|
||||||
(insert users
|
(belongs-to contests)
|
||||||
(values user)))
|
(belongs-to users)
|
||||||
|
(has-many sources))
|
||||||
|
|
||||||
(defn update-user [id first-name last-name email]
|
(defentity contests
|
||||||
(update users
|
(has-many entries))
|
||||||
(set-fields {:first_name first-name
|
|
||||||
:last_name last-name
|
|
||||||
:email email})
|
|
||||||
(where {:id id})))
|
|
||||||
|
|
||||||
(defn get-user [id]
|
(defentity users
|
||||||
(first (select users
|
(has-many entities))
|
||||||
(where {:id id})
|
|
||||||
(limit 1))))
|
(defentity sources
|
||||||
|
(belongs-to entities))
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
; (defn create-entry [entry]
|
||||||
|
; (insert entries
|
||||||
|
; (values entry)))
|
||||||
|
;
|
||||||
|
; (defn update-entry [id id_uid title description user_id contest_id]
|
||||||
|
; (update entries
|
||||||
|
; (set-fields {:first_name first-name
|
||||||
|
; :last_name last-name
|
||||||
|
; :email email})
|
||||||
|
; (where {:id id})))
|
||||||
|
;
|
||||||
|
; (defn get-user [id]
|
||||||
|
; (first (select users
|
||||||
|
; (where {:id id})
|
||||||
|
; (limit 1))))
|
||||||
|
|
9
src/ldview/models/entity.clj
Normal file
9
src/ldview/models/entity.clj
Normal file
|
@ -0,0 +1,9 @@
|
||||||
|
(ns ldview.models.entity
|
||||||
|
(:use korma.core
|
||||||
|
[ldview.models.db]))
|
||||||
|
|
||||||
|
(defn all []
|
||||||
|
(select entries))
|
||||||
|
|
||||||
|
(defn insert [fields]
|
||||||
|
(insert entries (values fields)))
|
|
@ -11,26 +11,37 @@
|
||||||
:password ""
|
:password ""
|
||||||
:naming {:keys clojure.string/lower-case
|
:naming {:keys clojure.string/lower-case
|
||||||
:fields clojure.string/upper-case}})
|
:fields clojure.string/upper-case}})
|
||||||
(defn initialized?
|
|
||||||
"checks to see if the database schema is present"
|
(defn initialized? []
|
||||||
[]
|
|
||||||
(.exists (new java.io.File (str (io/resource-path) db-store ".h2.db"))))
|
(.exists (new java.io.File (str (io/resource-path) db-store ".h2.db"))))
|
||||||
|
|
||||||
(defn create-users-table
|
|
||||||
[]
|
|
||||||
(sql/with-connection db-spec
|
|
||||||
(sql/create-table
|
|
||||||
:users
|
|
||||||
[:id "varchar(20) PRIMARY KEY"]
|
|
||||||
[:first_name "varchar(30)"]
|
|
||||||
[:last_name "varchar(30)"]
|
|
||||||
[:email "varchar(30)"]
|
|
||||||
[:admin :boolean]
|
|
||||||
[:last_login :time]
|
|
||||||
[:is_active :boolean]
|
|
||||||
[:pass "varchar(100)"])))
|
|
||||||
|
|
||||||
(defn create-tables
|
(defn create-initial-tables []
|
||||||
"creates the database tables used by the application"
|
(sql/with-connection db-spec
|
||||||
[]
|
(sql/create-table :entries
|
||||||
(create-users-table))
|
[:id "INT PRIMARY KEY AUTO_INCREMENT"]
|
||||||
|
[:ld_uid "INT"]
|
||||||
|
[:title "VARCHAR(255)"]
|
||||||
|
[:description "TEXT"]
|
||||||
|
[:user_id "INT"]
|
||||||
|
[:contest_id "INT"])
|
||||||
|
|
||||||
|
(sql/create-table :users
|
||||||
|
[:id "INT PRIMARY KEY AUTO_INCREMENT"]
|
||||||
|
[:name "VARCHAR(255)"])
|
||||||
|
|
||||||
|
(sql/create-table :contests
|
||||||
|
[:id "INT PRIMARY KEY AUTO_INCREMENT"]
|
||||||
|
[:name "VARCHAR(255)"])
|
||||||
|
|
||||||
|
(sql/create-table :sources
|
||||||
|
[:id "INT PRIMARY KEY AUTO_INCREMENT"]
|
||||||
|
[:entry_id "INT"]
|
||||||
|
[:type "VARCHAR(100)"]
|
||||||
|
[:uri "VARCHAR(255)"])
|
||||||
|
|
||||||
|
(sql/do-commands "CREATE INDEX ld_uid_index ON entries (ld_uid)")
|
||||||
|
(sql/do-commands "CREATE INDEX entry_id_index ON sources (entry_id)")))
|
||||||
|
|
||||||
|
(defn create-tables []
|
||||||
|
(create-initial-tables))
|
||||||
|
|
|
@ -1,7 +1,7 @@
|
||||||
(ns ldview.tasks.scrape
|
(ns ldview.tasks.scrape
|
||||||
(:require [net.cgrand.enlive-html :as html]
|
(:require [net.cgrand.enlive-html :as html]
|
||||||
[clj-http.client :as http]
|
[clj-http.client :as http]
|
||||||
[clojure.java.io :as io]
|
[clojure.java.io :as io])
|
||||||
(:use [clojure.string :only (split)]
|
(:use [clojure.string :only (split)]
|
||||||
[ldview.util]))
|
[ldview.util]))
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue