diff --git a/Handler/Common.hs b/Handler/Common.hs index fc532ab..ccb58f6 100644 --- a/Handler/Common.hs +++ b/Handler/Common.hs @@ -7,6 +7,7 @@ import qualified Data.List as L import qualified Data.Text.Lazy.Encoding as E import qualified Data.Text.Read as R import Data.Maybe +import qualified Data.Char as C import Yesod.Form.Functions import Text.Shakespeare.Text import Network.Mail.Mime @@ -93,53 +94,52 @@ handleBarcodes :: Either UserId BeverageId -> [Text] -> Handler () handleBarcodes (Left uId) nbs = do raws <- runDB $ selectList [BarcodeUser ==. Just uId] [] obs <- return $ map (barcodeCode . entityVal) raws - case length nbs > length obs of - True -> do - sect <- return $ nbs L.\\ obs - _ <- mapM (\b -> runDB $ insert $ Barcode - b - True - (Just uId) - Nothing - ) sect - return () - False -> do - sect <- return $ obs L.\\ nbs - ents <- mapM (runDB . getBy . UniqueBarcode) sect - mapM_ (runDB . delete . entityKey . fromJust) ents + toDel <- return $ obs L.\\ nbs + toAdd <- return $ nbs L.\\ obs + _ <- mapM (\b -> runDB $ insert_ $ Barcode + b + True + (Just uId) + Nothing + ) toAdd + ents <- mapM (runDB . getBy . UniqueBarcode) toDel + mapM_ (runDB . delete . entityKey . fromJust) ents handleBarcodes (Right bId) nbs = do raws <- runDB $ selectList [BarcodeBev ==. Just bId] [] obs <- return $ map (barcodeCode . entityVal) raws - case length nbs > length obs of - True -> do - sect <- return $ nbs L.\\ obs - _ <- mapM (\b -> runDB $ insert $ Barcode - b - False - Nothing - (Just bId) - ) sect - return () - False -> do - sect <- return $ obs L.\\ nbs - ents <- mapM (runDB . getBy . UniqueBarcode) sect - mapM_ (runDB . delete . entityKey . fromJust) ents + toDel <- return $ obs L.\\ nbs + toAdd <- return $ nbs L.\\ obs + _ <- mapM (\b -> runDB $ insert $ Barcode + b + False + Nothing + (Just bId) + ) toAdd + ents <- mapM (runDB . getBy . UniqueBarcode) toDel + mapM_ (runDB . delete . entityKey . fromJust) ents handleGetParam :: Maybe Text -> Either UserId BeverageId -> Handler () handleGetParam Nothing _ = return () handleGetParam (Just b) eub = do - e <- runDB $ getBy $ UniqueBarcode b - case e of - Nothing -> do - _ <- case eub of - Left uId -> do - runDB $ insert_ $ Barcode b True (Just uId) Nothing - Right bId -> do - runDB $ insert_ $ Barcode b False Nothing (Just bId) - setMessageI MsgBarcodeAdded - Just _ -> - setMessageI MsgBarcodeDuplicate + f <- return $ T.filter C.isAlphaNum b + case (T.length f) > 0 of + True -> do + e <- runDB $ getBy $ UniqueBarcode f + case e of + Nothing -> do + _ <- case eub of + Left uId -> do + -- should usernames containing, among other, spaces cause problems, replace b for f here + runDB $ insert_ $ Barcode b True (Just uId) Nothing + Right bId -> do + -- and here + runDB $ insert_ $ Barcode b False Nothing (Just bId) + setMessageI MsgBarcodeAdded + Just _ -> + setMessageI MsgBarcodeDuplicate + False -> do + setMessageI MsgProvideBarcode amountField :: (RenderMessage (HandlerSite m) FormMessage, Show a, Monad m, Integral a) => Field m a amountField = Field diff --git a/messages/cz.msg b/messages/cz.msg index b2dfdaf..8db0a24 100644 --- a/messages/cz.msg +++ b/messages/cz.msg @@ -102,7 +102,7 @@ AvatarInUseError: Avatar nelze smazat dokud je v užitku Volume: Objem v l CorrectedAmount: Stráta BarcodeNotUser: Tento kód není pro uživatele, ale pro produkt -ProvideBarcode: Prosím zadej kód +ProvideBarcode: Prosím zadej správný kód BarcodeNotBev: Tento kód není pro produkt, ale pro uživatele BarcodeUnknown: Tento kód je neznámý BarcodeField: Čárový kódy diff --git a/messages/de.msg b/messages/de.msg index f61c2aa..eef11b7 100644 --- a/messages/de.msg +++ b/messages/de.msg @@ -102,7 +102,7 @@ AvatarInUseError: Avatare können nicht gelöscht werden, solange sie in Benutzu Volume: Volumen in l CorrectedAmount: Schwund BarcodeNotUser: Dieser Barcode ist keinem Benutzer zugeordnet, sondern einem Produkt -ProvideBarcode: Bitte Barcode angeben +ProvideBarcode: Bitte korrekten Barcode angeben BarcodeNotBev: Dieser Barcode ist keinem Produkt zugeordnet, sondern einem Benutzer BarcodeUnknown: Dieser Barcode ist unbekannt BarcodeField: Barcodes diff --git a/messages/en.msg b/messages/en.msg index 64f3fd8..09fb1b8 100644 --- a/messages/en.msg +++ b/messages/en.msg @@ -102,7 +102,7 @@ AvatarInUseError: You can not delete avatars that are currently in use Volume: Volume in l CorrectedAmount: Loss BarcodeNotUser: This barcode is no user, but a product -ProvideBarcode: Please provide a barcode +ProvideBarcode: Please provide a correct barcode BarcodeNotBev: This barcode is no product, but a user BarcodeUnknown: This barcode is unknown BarcodeField: Barcodes