From 9d0d1c93cae2ec0a58a6229e10701b86c74a3d49 Mon Sep 17 00:00:00 2001 From: Benjamin Barenblat Date: Thu, 2 May 2013 21:33:21 -0400 Subject: Disallow multiple upvotes --- forum/forum.ur | 12 ++++++++++-- 1 file 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 -- cgit v1.2.3