Add the models ... the mysql connection is not working yet ... :(
This commit is contained in:
parent
66a29f3ad3
commit
276a36965f
6 changed files with 110 additions and 0 deletions
|
@ -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"]]
|
||||
|
|
18
src/luduverse/db-models/competition.clj
Normal file
18
src/luduverse/db-models/competition.clj
Normal file
|
@ -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}))))
|
32
src/luduverse/db-models/entry.clj
Normal file
32
src/luduverse/db-models/entry.clj
Normal file
|
@ -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)))))
|
10
src/luduverse/db-models/source.clj
Normal file
10
src/luduverse/db-models/source.clj
Normal file
|
@ -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})));)
|
||||
|
19
src/luduverse/db-models/user.clj
Normal file
19
src/luduverse/db-models/user.clj
Normal file
|
@ -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))
|
30
src/luduverse/db.clj
Normal file
30
src/luduverse/db.clj
Normal file
|
@ -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))
|
||||
|
Loading…
Reference in a new issue