summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorGravatar Benjamin Barenblat <bbaren@mit.edu>2013-05-02 21:33:21 -0400
committerGravatar Benjamin Barenblat <bbaren@mit.edu>2013-05-02 21:33:21 -0400
commit9d0d1c93cae2ec0a58a6229e10701b86c74a3d49 (patch)
tree93504b8be5bb1cfc8a4e1b6948db44a941e9528e
parent7236d15876c3f8c1097bd397c1e2bc15aeb1a7da (diff)
Disallow multiple upvotes
-rw-r--r--forum/forum.ur12
1 files changed, 10 insertions, 2 deletions
diff --git a/forum/forum.ur b/forum/forum.ur
index 85f3e19..3081e7d 100644
--- a/forum/forum.ur
+++ b/forum/forum.ur
@@ -52,6 +52,11 @@ fun queryColumn [tab ::: Name] [field ::: Name] [state ::: Type]
: transaction state =
query q (fn row state => return (f row.tab.field state)) initial
+fun unless [m ::: Type -> Type] (_ : monad m) (cond : bool) (computation : m {}) =
+ if cond
+ then return ()
+ else computation
+
(* Sum all the votes on a single question. *)
fun getScore (questionId : int) : transaction Score.score =
queryColumn (SELECT Vote.Value FROM vote
@@ -65,8 +70,11 @@ fun recordVote (value : Score.score) (entryId : int) _formData : transaction pag
in the first place. *)
let val author = Author.nameError authorOpt
in
- dml (INSERT INTO vote (QuestionId, Author, Value)
- VALUES ({[entryId]}, {[author]}, {[value]}));
+ alreadyVoted <- hasRows (SELECT Vote.Value FROM vote
+ WHERE Vote.QuestionId = {[entryId]}
+ AND Vote.Author = {[author]});
+ unless alreadyVoted (dml (INSERT INTO vote (QuestionId, Author, Value)
+ VALUES ({[entryId]}, {[author]}, {[value]})));
detail entryId
end