2015-07-20 23:14:21 +00:00
|
|
|
module Handler.Barcode where
|
|
|
|
|
|
|
|
import Import
|
|
|
|
|
2015-07-20 23:42:52 +00:00
|
|
|
getHomeBarcodeR :: Handler Html
|
2015-07-21 07:14:38 +00:00
|
|
|
getHomeBarcodeR = do
|
2015-07-21 09:37:43 +00:00
|
|
|
eub <- handleSelectParam
|
|
|
|
case eub of
|
|
|
|
Just (Left uId) -> do
|
|
|
|
redirect $ SelectR uId
|
|
|
|
Just (Right _) -> do
|
|
|
|
setMessageI MsgBarcodeNotUser
|
|
|
|
redirect $ HomeR
|
2015-07-21 07:14:38 +00:00
|
|
|
Nothing -> do
|
2015-07-21 09:37:43 +00:00
|
|
|
redirect $ HomeR
|
2015-07-20 23:28:05 +00:00
|
|
|
|
2015-07-20 23:42:52 +00:00
|
|
|
getSelectBarcodeR :: UserId -> Handler Html
|
2015-07-21 07:14:38 +00:00
|
|
|
getSelectBarcodeR uId = do
|
2015-07-21 09:37:43 +00:00
|
|
|
eub <- handleSelectParam
|
|
|
|
case eub of
|
|
|
|
Just (Right bId) -> do
|
|
|
|
redirect $ BuyR uId bId
|
|
|
|
Just (Left _) -> do
|
|
|
|
setMessageI MsgBarcodeNotBev
|
|
|
|
redirect $ SelectR uId
|
|
|
|
Nothing ->
|
|
|
|
redirect $ SelectR uId
|
|
|
|
|
|
|
|
getSelectCashBarcodeR :: Handler Html
|
|
|
|
getSelectCashBarcodeR = do
|
|
|
|
eub <- handleSelectParam
|
|
|
|
case eub of
|
|
|
|
Just (Right bId) -> do
|
|
|
|
redirect $ BuyCashR bId
|
|
|
|
Just (Left _) -> do
|
|
|
|
setMessageI MsgBarcodeNotBev
|
|
|
|
redirect $ SelectCashR
|
|
|
|
Nothing -> do
|
|
|
|
redirect $ SelectCashR
|
|
|
|
|
|
|
|
handleSelectParam :: Handler (Maybe (Either UserId BeverageId))
|
|
|
|
handleSelectParam = do
|
2015-07-21 07:14:38 +00:00
|
|
|
c <- lookupGetParam "barcode"
|
|
|
|
case c of
|
|
|
|
Just code -> do
|
|
|
|
be <- runDB $ getBy $ UniqueBarcode code
|
|
|
|
case be of
|
|
|
|
Just (Entity _ bar) -> do
|
|
|
|
case barcodeIsUser bar of
|
2015-07-21 09:37:43 +00:00
|
|
|
True -> do
|
|
|
|
case (barcodeUser bar, barcodeBev bar) of
|
|
|
|
(Just uId, Nothing) ->
|
|
|
|
return $ Just $ Left uId
|
|
|
|
_ ->
|
|
|
|
error "Malformed barcode"
|
2015-07-21 07:14:38 +00:00
|
|
|
False -> do
|
2015-07-21 09:37:43 +00:00
|
|
|
case (barcodeBev bar, barcodeUser bar) of
|
|
|
|
(Just bId, Nothing) ->
|
|
|
|
return $ Just $ Right bId
|
|
|
|
_ ->
|
2015-07-21 07:14:38 +00:00
|
|
|
error "Malformed barcode"
|
|
|
|
Nothing -> do
|
|
|
|
setMessageI MsgBarcodeUnknown
|
2015-07-21 09:37:43 +00:00
|
|
|
return Nothing
|
2015-07-21 07:14:38 +00:00
|
|
|
Nothing -> do
|
|
|
|
setMessageI MsgProvideBarcode
|
2015-07-21 09:37:43 +00:00
|
|
|
return Nothing
|