From 8cd63a64abafe21378c35c2489d49f24c9ece3c9 Mon Sep 17 00:00:00 2001 From: Joris Date: Mon, 4 Apr 2016 01:27:36 +0200 Subject: Add income list CRUD in user page --- src/server/Controller/Income.hs | 28 ++++++++++++++++++++++++---- src/server/Controller/Payment.hs | 6 +++--- 2 files changed, 27 insertions(+), 7 deletions(-) (limited to 'src/server/Controller') diff --git a/src/server/Controller/Income.hs b/src/server/Controller/Income.hs index 51861d3..4474d51 100644 --- a/src/server/Controller/Income.hs +++ b/src/server/Controller/Income.hs @@ -2,21 +2,29 @@ module Controller.Income ( getIncomes - , setIncome + , addIncome + , deleteOwnIncome ) where import Web.Scotty +import Network.HTTP.Types.Status (ok200, badRequest400) + import Control.Monad.IO.Class (liftIO) import Database.Persist +import Data.Text (Text) +import qualified Data.Text.Lazy as TL +import Data.Time.Clock (UTCTime) + import qualified Secure import Json (jsonId) import Model.Database import qualified Model.Income as Income +import qualified Model.Message.Key as Key getIncomes :: ActionM () getIncomes = @@ -24,8 +32,20 @@ getIncomes = (liftIO $ map Income.getJsonIncome <$> runDb Income.getIncomes) >>= json ) -setIncome :: Int -> ActionM () -setIncome amount = +addIncome :: UTCTime -> Int -> ActionM () +addIncome creation amount = + Secure.loggedAction (\user -> + (liftIO . runDb $ Income.addIncome (entityKey user) creation amount) >>= jsonId + ) + +deleteOwnIncome :: Text -> ActionM () +deleteOwnIncome incomeId = Secure.loggedAction (\user -> do - (liftIO . runDb $ Income.setIncome (entityKey user) amount) >>= jsonId + deleted <- liftIO . runDb $ Income.deleteOwnIncome user (textToKey incomeId) + if deleted + then + status ok200 + else do + status badRequest400 + text . TL.pack . show $ Key.IncomeNotDeleted ) diff --git a/src/server/Controller/Payment.hs b/src/server/Controller/Payment.hs index 204794a..7e8d0a3 100644 --- a/src/server/Controller/Payment.hs +++ b/src/server/Controller/Payment.hs @@ -3,7 +3,7 @@ module Controller.Payment ( getPayments , createPayment - , deletePayment + , deleteOwnPayment ) where import Web.Scotty @@ -46,8 +46,8 @@ createPayment name cost frequency = jsonObject [("id", Json.Number . fromIntegral . keyToInt64 $ paymentId)] ) -deletePayment :: Text -> ActionM () -deletePayment paymentId = +deleteOwnPayment :: Text -> ActionM () +deleteOwnPayment paymentId = Secure.loggedAction (\user -> do deleted <- liftIO . runDb $ P.deleteOwnPayment user (textToKey paymentId) if deleted -- cgit v1.2.3