diff --git a/project.clj b/project.clj index 9788f3c..2478067 100644 --- a/project.clj +++ b/project.clj @@ -10,6 +10,7 @@ [enlive "1.1.5"] [image-resizer "0.1.6"] [clj-http "1.0.0"] + [korma "0.3.3"] [me.raynes/fs "1.4.6"]] :main luduverse.core :plugins [[lein-ring "0.8.10"]] diff --git a/src/luduverse/db-models/competition.clj b/src/luduverse/db-models/competition.clj new file mode 100644 index 0000000..d9ba17e --- /dev/null +++ b/src/luduverse/db-models/competition.clj @@ -0,0 +1,18 @@ +(ns luduverse.db-models.competition + (:require [korma.core :refer :all] + [luduverse.db :refer :all])) + +(defn latest [] + (first (select competitions + (order :number :DESC) + (limit 1)))) + +(defn exists? [number] + (not (empty? (select competitions + (fields :id) + (where {:number number}))))) + +(defn create! [number theme] + (if-not (exists? number) + (insert competitions (values {:number number + :theme theme})))) diff --git a/src/luduverse/db-models/entry.clj b/src/luduverse/db-models/entry.clj new file mode 100644 index 0000000..ccd1f02 --- /dev/null +++ b/src/luduverse/db-models/entry.clj @@ -0,0 +1,32 @@ +(ns luduverse.db-models.entry + (:require [korma.core :refer :all] + [luduverse.db :refer :all] + [luduverse.db-models.user :as user] + [luduverse.db-models.competition :as competition] + [luduverse.db-models.source :as source])) + +(defn all [] + (select entries)) + +(defn by-ld-uid [ld-uid] + (first (select entries + (where {:ld_uid ld-uid})))) + +(defn exists? [ld-uid] + (not (empty? (select entries + (fields :id) + (where {:ld_uid ld-uid}) + (limit 1))))) + +(defn create! [fields] + (let [username (user/unique-user (:author fields)) + competition-id (:id (competition/latest))] + (if-not (exists? (:ld_uid fields)) + (insert entries (values {:ld_uid (:ld_uid fields) + :title (:title fields) + :description (:description fields) + :type (:type fields) + :user_id (:id username) + :competition_id competition-id}))) + (doseq [link (:links fields)] + (source/create! (:id (by-ld-uid (:ld_uid fields))) (:url link) (:title link))))) diff --git a/src/luduverse/db-models/source.clj b/src/luduverse/db-models/source.clj new file mode 100644 index 0000000..e842604 --- /dev/null +++ b/src/luduverse/db-models/source.clj @@ -0,0 +1,10 @@ +(ns luduverse.db-models.source + (:require [korma.core :refer :all] + [luduverse.db :refer :all])) + +(defn create! [entry_id uri title] +; (if (empty? (select sources (where {:entry_id entry_id :type title}))) + (insert sources (values {:entry_id entry_id + :uri uri + :type title})));) + diff --git a/src/luduverse/db-models/user.clj b/src/luduverse/db-models/user.clj new file mode 100644 index 0000000..ef41bb3 --- /dev/null +++ b/src/luduverse/db-models/user.clj @@ -0,0 +1,19 @@ +(ns luduverse.db-models.user + (:require [korma.core :refer :all] + [luduverse.db :refer :all])) + +(defn user-id [username] + (first (select users + (fields :id) + (where {:name username}) + (limit 1)))) + +(defn exists? [username] + (not (empty? (user-id username)))) + +(defn create! [username] + (insert users (values {:name username}))) + +(defn unique-user [username] + (if-not (exists? username) (create! username)) + (user-id username)) diff --git a/src/luduverse/db.clj b/src/luduverse/db.clj new file mode 100644 index 0000000..c558576 --- /dev/null +++ b/src/luduverse/db.clj @@ -0,0 +1,30 @@ +(ns luduverse.db + (:require [luduverse.db-schema :as schema] + [korma.core :refer :all] + [korma.db :refer [defdb mysql]])) + +(defdb db (mysql {:db "luduverse" + :user "root" + :password "dev" + :host "localhost"})) + +(declare entities competitions users sources) + +(defentity entries + (database db) + (belongs-to contests) + (belongs-to users) + (has-many sources)) + +(defentity competitions + (database db) + (has-many entries)) + +(defentity users + (database db) + (has-many entities)) + +(defentity sources + (database db) + (belongs-to entities)) +