yammat/Handler/Select.hs

64 lines
1.8 KiB
Haskell
Raw Normal View History

2015-04-04 04:46:33 +00:00
module Handler.Select where
import Import
import Handler.Common
import qualified Text.Read as R
import qualified Data.Text as T
getSelectR :: UserId -> Handler Html
getSelectR uId = do
mUser <- runDB $ get uId
case mUser of
Just user -> do
2015-04-04 06:18:15 +00:00
master <- getYesod
2015-04-09 11:10:23 +00:00
beverages <- runDB $ selectList [BeverageAmount >. 0] [Asc BeverageIdent]
2015-04-04 04:46:33 +00:00
defaultLayout $ do
$(widgetFile "select")
Nothing -> do
2015-04-09 22:40:58 +00:00
setMessageI MsgUserUnknown
2015-04-04 04:46:33 +00:00
redirect $ HomeR
getSelectCashR :: Handler Html
getSelectCashR = do
2015-04-09 11:10:23 +00:00
beverages <- runDB $ selectList [BeverageAmount >. 0] [Asc BeverageIdent]
2015-04-04 04:46:33 +00:00
defaultLayout $ do
$(widgetFile "selectCash")
getRechargeR :: UserId -> Handler Html
getRechargeR uId = do
mUser <- runDB $ get uId
case mUser of
Just user -> do
(rechargeWidget, enctype) <- generateFormPost rechargeForm
2015-04-09 22:40:58 +00:00
currency <- appCurrency <$> appSettings <$> getYesod
2015-04-04 04:46:33 +00:00
defaultLayout $ do
$(widgetFile "recharge")
Nothing -> do
2015-04-09 22:40:58 +00:00
setMessageI MsgUserUnknown
2015-04-04 04:46:33 +00:00
redirect $ HomeR
postRechargeR :: UserId -> Handler Html
postRechargeR uId = do
mUser <- runDB $ get uId
case mUser of
Just user -> do
((res, _), _) <- runFormPost rechargeForm
case res of
FormSuccess amount -> do
updateCashier amount ("Guthaben: " `T.append` (userIdent user))
time <- liftIO getCurrentTime
secs <- return $ R.read $ formatTime defaultTimeLocale "%s" time
runDB $ update uId [UserBalance +=. amount, UserTimestamp =. secs]
2015-04-09 22:40:58 +00:00
setMessageI MsgRecharged
2015-04-04 04:46:33 +00:00
redirect $ HomeR
_ -> do
2015-04-09 22:40:58 +00:00
setMessageI MsgRechargeError
2015-04-04 04:46:33 +00:00
redirect $ HomeR
Nothing -> do
2015-04-09 22:40:58 +00:00
setMessageI MsgUserUnknown
2015-04-04 04:46:33 +00:00
redirect $ HomeR
rechargeForm :: Form Int
rechargeForm = renderDivs
2015-04-09 22:40:58 +00:00
$ areq currencyField (fieldSettingsLabel MsgValue) (Just 0)