yammat/Handler/Select.hs

87 lines
2.8 KiB
Haskell
Raw Normal View History

2015-08-09 21:16:33 +02:00
-- yammat - Yet Another MateMAT
-- Copyright (C) 2015 Amedeo Molnár
--
-- This program is free software: you can redistribute it and/or modify
-- it under the terms of the GNU Affero General Public License as published
-- by the Free Software Foundation, either version 3 of the License, or
-- (at your option) any later version.
--
-- This program is distributed in the hope that it will be useful,
-- but WITHOUT ANY WARRANTY; without even the implied warranty of
-- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
-- GNU Affero General Public License for more details.
--
-- You should have received a copy of the GNU Affero General Public License
-- along with this program. If not, see <http://www.gnu.org/licenses/>.
2015-04-04 06:46:33 +02:00
module Handler.Select where
import Import
import Handler.Common
import qualified Text.Read as R
import qualified Data.Text as T
2015-04-16 02:51:08 +02:00
import Data.Maybe
2015-04-04 06:46:33 +02:00
getSelectR :: UserId -> Handler Html
getSelectR uId = do
mUser <- runDB $ get uId
case mUser of
Just user -> do
2015-04-04 08:18:15 +02:00
master <- getYesod
2015-04-09 13:10:23 +02:00
beverages <- runDB $ selectList [BeverageAmount >. 0] [Asc BeverageIdent]
2015-09-15 00:49:13 +02:00
defaultLayout $
2015-04-04 06:46:33 +02:00
$(widgetFile "select")
Nothing -> do
2015-04-10 00:40:58 +02:00
setMessageI MsgUserUnknown
2015-09-15 00:49:13 +02:00
redirect HomeR
2015-04-04 06:46:33 +02:00
getSelectCashR :: Handler Html
getSelectCashR = do
2015-04-09 13:10:23 +02:00
beverages <- runDB $ selectList [BeverageAmount >. 0] [Asc BeverageIdent]
2015-09-15 00:49:13 +02:00
defaultLayout $
2015-04-04 06:46:33 +02:00
$(widgetFile "selectCash")
getRechargeR :: UserId -> Handler Html
getRechargeR uId = do
mUser <- runDB $ get uId
case mUser of
Just user -> do
2015-10-22 23:57:27 +02:00
(rechargeWidget, enctype) <- generateFormPost
$ renderBootstrap3 BootstrapBasicForm rechargeForm
2015-04-10 00:40:58 +02:00
currency <- appCurrency <$> appSettings <$> getYesod
2015-09-15 00:49:13 +02:00
defaultLayout $
2015-04-04 06:46:33 +02:00
$(widgetFile "recharge")
Nothing -> do
2015-04-10 00:40:58 +02:00
setMessageI MsgUserUnknown
2015-09-15 00:49:13 +02:00
redirect HomeR
2015-04-04 06:46:33 +02:00
postRechargeR :: UserId -> Handler Html
postRechargeR uId = do
mUser <- runDB $ get uId
case mUser of
Just user -> do
2015-10-22 23:57:27 +02:00
((res, _), _) <- runFormPost
$ renderBootstrap3 BootstrapBasicForm rechargeForm
2015-04-04 06:46:33 +02:00
case res of
2015-09-15 00:49:13 +02:00
FormSuccess amount ->
if amount < 0
then do
setMessageI MsgNegativeRecharge
redirect $ RechargeR uId
else do
updateCashier amount ("Guthaben: " `T.append` userIdent user)
2015-04-13 14:49:14 +02:00
time <- liftIO getCurrentTime
2015-09-15 00:49:13 +02:00
let secs = R.read $ formatTime defaultTimeLocale "%s" time
2015-04-13 14:49:14 +02:00
runDB $ update uId [UserBalance +=. amount, UserTimestamp =. secs]
setMessageI MsgRecharged
2015-09-15 00:49:13 +02:00
redirect HomeR
2015-04-04 06:46:33 +02:00
_ -> do
2015-04-10 00:40:58 +02:00
setMessageI MsgRechargeError
2015-09-15 00:49:13 +02:00
redirect HomeR
2015-04-04 06:46:33 +02:00
Nothing -> do
2015-04-10 00:40:58 +02:00
setMessageI MsgUserUnknown
2015-09-15 00:49:13 +02:00
redirect HomeR
2015-04-04 06:46:33 +02:00
2015-10-22 23:57:27 +02:00
rechargeForm :: AForm Handler Int
rechargeForm = areq currencyField (bfs MsgValue) (Just 0)
<* bootstrapSubmit (msgToBSSubmit MsgRecharge)