More DB work ...

This commit is contained in:
Aaron Mueller 2013-09-29 01:08:25 +02:00
parent 452e3d3245
commit 146f1bef37
5 changed files with 52 additions and 7 deletions

View 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))))

View file

@ -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)})))))

View file

@ -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

View 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))

View file

@ -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}))