More DB work ...
This commit is contained in:
parent
452e3d3245
commit
146f1bef37
5 changed files with 52 additions and 7 deletions
9
src/ldview/models/competition.clj
Normal file
9
src/ldview/models/competition.clj
Normal file
|
@ -0,0 +1,9 @@
|
||||||
|
(ns ldview.models.competition
|
||||||
|
(:use korma.core
|
||||||
|
[ldview.models.db]
|
||||||
|
[ldview.util]))
|
||||||
|
|
||||||
|
(defn latest []
|
||||||
|
(first (select competitions
|
||||||
|
(where {:number *competition*})
|
||||||
|
(limit 1))))
|
|
@ -1,9 +1,25 @@
|
||||||
(ns ldview.models.entity
|
(ns ldview.models.entity
|
||||||
(:use korma.core
|
(:use korma.core
|
||||||
[ldview.models.db]))
|
[ldview.models.db])
|
||||||
|
(:require [ldview.models.user :as user]))
|
||||||
|
|
||||||
(defn all []
|
(defn all []
|
||||||
(select entries))
|
(select entries))
|
||||||
|
|
||||||
(defn insert [fields]
|
(defn exists? [ld-uid]
|
||||||
(insert entries (values fields)))
|
(not (empty? (select entities
|
||||||
|
(fields :id)
|
||||||
|
(where {:ld_uid ld-uid})
|
||||||
|
(limit 1)))))
|
||||||
|
|
||||||
|
(defn create! [fields]
|
||||||
|
(let [user (user/unique-user (:user fields))
|
||||||
|
competition-id (competition/latest)]
|
||||||
|
(if-not (exists? (:ld_uid fields))
|
||||||
|
(insert entries (values {
|
||||||
|
:ld_uid (:ld_uid fields)
|
||||||
|
:title (:title fields)
|
||||||
|
:description (:description fields)
|
||||||
|
:user_id (:id user)
|
||||||
|
:competition_id (:id competition)})))))
|
||||||
|
|
||||||
|
|
|
@ -24,14 +24,15 @@
|
||||||
[:title "VARCHAR(255)"]
|
[:title "VARCHAR(255)"]
|
||||||
[:description "TEXT"]
|
[:description "TEXT"]
|
||||||
[:user_id "INT"]
|
[:user_id "INT"]
|
||||||
[:contest_id "INT"])
|
[:competition_id "INT"])
|
||||||
|
|
||||||
(sql/create-table :users
|
(sql/create-table :users
|
||||||
[:id "INT PRIMARY KEY AUTO_INCREMENT"]
|
[:id "INT PRIMARY KEY AUTO_INCREMENT"]
|
||||||
[:name "VARCHAR(255)"])
|
[:name "VARCHAR(255)"])
|
||||||
|
|
||||||
(sql/create-table :contests
|
(sql/create-table :competitions
|
||||||
[:id "INT PRIMARY KEY AUTO_INCREMENT"]
|
[:id "INT PRIMARY KEY AUTO_INCREMENT"]
|
||||||
|
[:number "INT"]
|
||||||
[:name "VARCHAR(255)"])
|
[:name "VARCHAR(255)"])
|
||||||
|
|
||||||
(sql/create-table :sources
|
(sql/create-table :sources
|
||||||
|
|
19
src/ldview/models/user.clj
Normal file
19
src/ldview/models/user.clj
Normal file
|
@ -0,0 +1,19 @@
|
||||||
|
(ns ldview.models.user
|
||||||
|
(:use korma.core
|
||||||
|
[ldview.models.db]))
|
||||||
|
|
||||||
|
(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))
|
|
@ -51,10 +51,10 @@
|
||||||
links (links-on-entry content)
|
links (links-on-entry content)
|
||||||
description (html/text (nth (html/select [content] [:p]) 2))
|
description (html/text (nth (html/select [content] [:p]) 2))
|
||||||
images (images-on-entry content)]
|
images (images-on-entry content)]
|
||||||
{:id entry-id
|
{:ld_uid entry-id
|
||||||
:title title
|
:title title
|
||||||
:author author
|
|
||||||
:description description
|
:description description
|
||||||
|
:author author
|
||||||
:links links
|
:links links
|
||||||
:images images}))
|
:images images}))
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue