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
|
||||
(:use korma.core
|
||||
[ldview.models.db]))
|
||||
[ldview.models.db])
|
||||
(:require [ldview.models.user :as user]))
|
||||
|
||||
(defn all []
|
||||
(select entries))
|
||||
|
||||
(defn insert [fields]
|
||||
(insert entries (values fields)))
|
||||
(defn exists? [ld-uid]
|
||||
(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)"]
|
||||
[:description "TEXT"]
|
||||
[:user_id "INT"]
|
||||
[:contest_id "INT"])
|
||||
[:competition_id "INT"])
|
||||
|
||||
(sql/create-table :users
|
||||
[:id "INT PRIMARY KEY AUTO_INCREMENT"]
|
||||
[:name "VARCHAR(255)"])
|
||||
|
||||
(sql/create-table :contests
|
||||
(sql/create-table :competitions
|
||||
[:id "INT PRIMARY KEY AUTO_INCREMENT"]
|
||||
[:number "INT"]
|
||||
[:name "VARCHAR(255)"])
|
||||
|
||||
(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)
|
||||
description (html/text (nth (html/select [content] [:p]) 2))
|
||||
images (images-on-entry content)]
|
||||
{:id entry-id
|
||||
{:ld_uid entry-id
|
||||
:title title
|
||||
:author author
|
||||
:description description
|
||||
:author author
|
||||
:links links
|
||||
:images images}))
|
||||
|
||||
|
|
Loading…
Reference in a new issue