From d2d60cf48d9423cca195ff6bfde35ecb40705d6f Mon Sep 17 00:00:00 2001 From: Aaron Mueller Date: Sat, 28 Sep 2013 23:41:28 +0200 Subject: [PATCH] Set up the database tables and connection --- project.clj | 6 ++++- src/ldview/handler.clj | 5 +++- src/ldview/models/db.clj | 45 +++++++++++++++++++++---------- src/ldview/models/entity.clj | 9 +++++++ src/ldview/models/schema.clj | 51 ++++++++++++++++++++++-------------- src/ldview/tasks/scrape.clj | 2 +- 6 files changed, 81 insertions(+), 37 deletions(-) create mode 100644 src/ldview/models/entity.clj diff --git a/project.clj b/project.clj index 8426881..79ccf88 100644 --- a/project.clj +++ b/project.clj @@ -18,7 +18,11 @@ [javax.mail/mail javax.jms/jms 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 {:handler ldview.handler/war-handler, :init ldview.handler/init, diff --git a/src/ldview/handler.clj b/src/ldview/handler.clj index b70e46e..5157ccd 100644 --- a/src/ldview/handler.clj +++ b/src/ldview/handler.clj @@ -4,7 +4,8 @@ [noir.util.middleware :as middleware] [compojure.route :as route] [taoensso.timbre :as timbre] - [com.postspectacular.rotor :as rotor])) + [com.postspectacular.rotor :as rotor] + [ldview.models.schema :as schema])) (defroutes app-routes (route/resources "/") @@ -27,6 +28,8 @@ (timbre/set-config! [:shared-appender-config :rotor] {:path "ldview.log" :max-size (* 512 1024) :backlog 10}) + + (if-not (schema/initialized?) (schema/create-tables)) (timbre/info "ldview started successfully")) diff --git a/src/ldview/models/db.clj b/src/ldview/models/db.clj index bb68237..7803723 100644 --- a/src/ldview/models/db.clj +++ b/src/ldview/models/db.clj @@ -5,20 +5,37 @@ (defdb db schema/db-spec) -(defentity users) +(declare entities contests users sources) -(defn create-user [user] - (insert users - (values user))) +(defentity entries + (belongs-to contests) + (belongs-to users) + (has-many sources)) -(defn update-user [id first-name last-name email] - (update users - (set-fields {:first_name first-name - :last_name last-name - :email email}) - (where {:id id}))) +(defentity contests + (has-many entries)) -(defn get-user [id] - (first (select users - (where {:id id}) - (limit 1)))) +(defentity users + (has-many entities)) + +(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)))) diff --git a/src/ldview/models/entity.clj b/src/ldview/models/entity.clj new file mode 100644 index 0000000..bf41d25 --- /dev/null +++ b/src/ldview/models/entity.clj @@ -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))) diff --git a/src/ldview/models/schema.clj b/src/ldview/models/schema.clj index 8df984f..b63ed9f 100644 --- a/src/ldview/models/schema.clj +++ b/src/ldview/models/schema.clj @@ -11,26 +11,37 @@ :password "" :naming {:keys clojure.string/lower-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")))) -(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 - "creates the database tables used by the application" - [] - (create-users-table)) +(defn create-initial-tables [] + (sql/with-connection db-spec + (sql/create-table :entries + [: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)) diff --git a/src/ldview/tasks/scrape.clj b/src/ldview/tasks/scrape.clj index 317cbdd..ca24b60 100644 --- a/src/ldview/tasks/scrape.clj +++ b/src/ldview/tasks/scrape.clj @@ -1,7 +1,7 @@ (ns ldview.tasks.scrape (:require [net.cgrand.enlive-html :as html] [clj-http.client :as http] - [clojure.java.io :as io] + [clojure.java.io :as io]) (:use [clojure.string :only (split)] [ldview.util]))