aboutsummaryrefslogtreecommitdiff
path: root/src/server/Controller
diff options
context:
space:
mode:
authorJoris2016-04-04 01:27:36 +0200
committerJoris2016-04-04 01:27:36 +0200
commit8cd63a64abafe21378c35c2489d49f24c9ece3c9 (patch)
tree541145481d1492f3e388002d931cb3f8fec0acb2 /src/server/Controller
parent01e4ce0fa7c369996ec4ef3a033d16d6fa0eb715 (diff)
Add income list CRUD in user page
Diffstat (limited to 'src/server/Controller')
-rw-r--r--src/server/Controller/Income.hs28
-rw-r--r--src/server/Controller/Payment.hs6
2 files changed, 27 insertions, 7 deletions
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