Add deletion of a vote

This commit is contained in:
Konrad Merz 2015-03-30 13:39:03 +02:00
parent 9376ad3174
commit 66a902b16b
2 changed files with 22 additions and 0 deletions

View file

@ -60,6 +60,10 @@ render (pollId, pollName, pollDesc) options voters cants errors editVoter = do
A.href (H.stringValue (
"/polls/" ++ (show pollId) ++ "/vote/" ++ cant ++ "/edit")) $
"Edit"
H.td $ H.a ! A.class_ "btn" !
A.href (H.stringValue (
"/polls/" ++ (show pollId) ++ "/vote/" ++ cant ++ "/delete")) $
"Delete"
renderVoter voter = do
if (voter == editVoter)
then
@ -78,6 +82,10 @@ render (pollId, pollName, pollDesc) options voters cants errors editVoter = do
A.href (H.stringValue (
"/polls/" ++ (show pollId) ++ "/vote/" ++ voter ++ "/edit")) $
"Edit"
H.td $ H.a ! A.class_ "btn" !
A.href (H.stringValue (
"/polls/" ++ (show pollId) ++ "/vote/" ++ voter ++ "/delete")) $
"Delete"
renderVoteCount (id, _, _) = do
H.td ! A.class_ "count" $ H.toHtml (show count)
where count = M.fold(\ids acc ->

View file

@ -101,6 +101,12 @@ scottySite = do
id <- S.param "id" :: S.ActionM String
name <- S.param "name" :: S.ActionM String
showAction id [] name
S.get "/polls/:id/vote/:name/delete" $ do
id <- S.param "id" :: S.ActionM String
name <- S.param "name" :: S.ActionM String
options <- liftIO $ getOptionsByPollId id
deleteVote id name (optionIds options)
S.redirect $ T.pack $ "/polls/" ++ id
S.post "/polls/:id/update" $ do
id <- S.param "id"
name <- S.param "name"
@ -255,3 +261,11 @@ doVoting name opt_ids choosen_opt_ids id = do
case (length choosen_opt_ids) of
0 -> createCant id name opt_ids
otherwise -> voteForOptions name opt_ids choosen_opt_ids id
deleteVote id name opts = do
mapM_ (\i -> runSqlite "noodle.db" $ do
deleteWhere [VoteOptionId ==. (toSqlKey i), VoteVoter ==. name]
) opts
runSqlite "noodle.db" $ do
deleteWhere [CantPollId ==. pollId, CantName ==. name]
where pollId = (toSqlKey (read id))