Created
September 6, 2009 17:12
-
-
Save duck1123/181873 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(ns net.mycyclopedia.model.statement | |
(:use net.mycyclopedia.model | |
(clojure.contrib str-utils) | |
(net.mycyclopedia.model | |
[auth :only (current-user)]) | |
(compojure [html :exclude (link-to)] | |
http) | |
compojure.validation.predicates | |
[clojure.contrib.sql :only (with-query-results | |
transaction | |
do-commands | |
insert-records)]) | |
(:require (net.mycyclopedia.model | |
[user :as user]) | |
(clj-record | |
[boot :as boot] | |
[core :as core] | |
[query :as q])) | |
(:import (org.joda.time DateTime))) | |
(core/init-model | |
(:associations | |
(belongs-to users :on user) | |
(belongs-to entries :as subject :on subj) | |
(belongs-to entries :as object :on obj) | |
(belongs-to predicates :on pred))) | |
(defmethod link-to :statement | |
[statement] | |
(compojure.html/link-to (uri statement) (:id statement))) | |
(defn check-user | |
"If the statement map does not have a user, assoc one from (current-user)" | |
[statement] | |
(if (:user statement) | |
statement | |
;; TODO: test value here | |
(assoc statement :user 1))) | |
(defn check-created | |
[statement] | |
(if (:created statement) | |
statement | |
(assoc statement :created (.toString (DateTime.) "yyyy-MM-dd HH:mm:ss")))) | |
(defn check-number | |
[statement sym] | |
(if (present? (sym statement)) | |
(if (integer? (sym statement)) | |
statement | |
(assoc statement sym (Integer/parseInt (sym statement)))) | |
(dissoc statement sym))) | |
(defn valid-statement? | |
[statement] | |
(and (:subj statement) | |
(:pred statement) | |
(or (:obj statement) | |
(and (:value statement) | |
(or (:lang statement) | |
(:datatype statement))) | |
(:uri statement)) | |
(:user statement) | |
(:created statement) | |
(:privacy statement) | |
(not (and (:obj statement) | |
(:value statement))) | |
(-> statement | |
check-user | |
check-created | |
(check-number :subj) | |
(check-number :obj)))) | |
;; Actions | |
(defn show | |
[id] | |
(first | |
(find-records | |
(q/and-conditions | |
{:id id} | |
(q/or-conditions | |
{:privacy 0} | |
{:user (:id (current-user))})) | |
:limit 1))) | |
(defn index | |
[] | |
(find-records | |
(q/or-conditions | |
{:privacy 0} | |
{:user (:id (current-user))}) | |
:order {:created :desc})) | |
(defn create | |
[& params] | |
(core/create "statements" (dissoc params) :*)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment