From 537a88638f469c3f36ace417dc42f2ec6d1002f1 Mon Sep 17 00:00:00 2001 From: nek0 Date: Fri, 6 Sep 2019 21:43:46 +0200 Subject: [PATCH] new user transfer function added --- src/API.hs | 2 ++ src/Control/User.hs | 64 +++++++++++++++++++++++++++++++++------------ src/Main.hs | 1 + 3 files changed, 50 insertions(+), 17 deletions(-) diff --git a/src/API.hs b/src/API.hs index a5e0ebc..aa23530 100644 --- a/src/API.hs +++ b/src/API.hs @@ -36,6 +36,8 @@ type UserAPI = :> QueryParam "refine" Refine :> Get '[JSON] [User] :<|> "user" :> "recharge" :> AuthProtect "header-auth" :> ReqBody '[JSON] UserRecharge :> Post '[JSON] () + :<|> "user" :> "transfer" :> AuthProtect "header-auth" + :> ReqBody '[JSON] UserTransfer :> Post '[JSON] () :<|> "product" :> AuthProtect "header-auth" :> ReqBody '[JSON] ProductSubmit :> Post '[JSON] Int diff --git a/src/Control/User.hs b/src/Control/User.hs index e2595eb..87964b7 100644 --- a/src/Control/User.hs +++ b/src/Control/User.hs @@ -66,30 +66,60 @@ userList muid ref = do userSelect conn ref userRecharge :: Maybe Int -> UserRecharge -> MateHandler () -userRecharge (Just auid) (UserRecharge uid amount) = - if auid == uid +userRecharge (Just auid) (UserRecharge amount) = + if amount >= 0 + then do + conn <- rsConnection <$> ask + ud <- userDetailsSelect auid conn + void $ insertNewJournalEntry + (JournalSubmit + ("User \"" <> userDetailsIdent ud <> "\" recharged " <> + T.pack (show (fromIntegral amount / 100 :: Double))) + amount + ) + conn + void $ addToUserBalance auid amount conn + else + throwError $ err400 + { errBody = "Amounts less or equal zero are not acceptable." + } +userRecharge Nothing _ = + throwError $ err403 + { errBody = "No Authentication present." + } + +userTransfer :: Maybe Int -> UserTransfer -> MateHandler () +userTransfer (Just auid) (UserTransfer target amount) = + if amount >= 0 then - if amount >= 0 + if auid /= target then do conn <- rsConnection <$> ask - ud <- userDetailsSelect conn uid - void $ insertNewJournalEntry - (JournalSubmit - ("User \"" <> userDetailsIdent ud <> "\" recharged " <> - T.pack (show (fromIntegral amount / 100 :: Double))) - amount - ) - conn - void $ addToUserBalance uid amount conn - else - throwError $ err406 - { errBody = "Amounts less or equal zero are not acceptable" + user <- userDetailsSelect auid conn + if amount < userDetailsBalance user + then do + mtarget <- filter (\u -> userId u == target) <$> userSelect (Just All) conn + if not (null mtarget) + then do + void $ addToUserBalance auid (-amount) conn + void $ addToUserBalance target amount conn + else + throwError $ err400 + { errBody = "Target user not found." + } + else + throwError $ err400 + { errBody = "Not enough credit balance" } + else + throwError $ err400 + { errBody = "You can not transfer yourself money." + } else throwError $ err403 { errBody = "Wrong Authentication present" } -userRecharge Nothing _ = +userTransfer Nothing _ = throwError $ err403 - { errBody = "No Authentication present" + { errBody = "No Authentication present." } diff --git a/src/Main.hs b/src/Main.hs index b79e2ad..693c4cf 100644 --- a/src/Main.hs +++ b/src/Main.hs @@ -76,6 +76,7 @@ app initState = userUpdate :<|> userList :<|> userRecharge :<|> + userTransfer :<|> productNew :<|> productOverview :<|>