From 8816cf758119a6a2073e561c8df297a833630986 Mon Sep 17 00:00:00 2001 From: Joris Date: Mon, 8 Aug 2016 20:58:17 +0200 Subject: Show incomes in a table and update like payments are updated --- src/server/Model/Database.hs | 1 + src/server/Model/Income.hs | 34 +++++++++++++++++++++++++------- src/server/Model/Json/EditIncome.hs | 20 +++++++++++++++++++ src/server/Model/Message/Key.hs | 1 + src/server/Model/Message/Translations.hs | 5 +++++ 5 files changed, 54 insertions(+), 7 deletions(-) create mode 100644 src/server/Model/Json/EditIncome.hs (limited to 'src/server/Model') diff --git a/src/server/Model/Database.hs b/src/server/Model/Database.hs index 4526fc5..6a2fefe 100644 --- a/src/server/Model/Database.hs +++ b/src/server/Model/Database.hs @@ -61,6 +61,7 @@ Income date Day amount Int createdAt UTCTime + editedAt UTCTime Maybe deletedAt UTCTime Maybe deriving Show |] diff --git a/src/server/Model/Income.hs b/src/server/Model/Income.hs index 62ab0ed..f389661 100644 --- a/src/server/Model/Income.hs +++ b/src/server/Model/Income.hs @@ -1,8 +1,9 @@ module Model.Income ( getJsonIncome , getIncomes - , createIncome - , deleteOwnIncome + , create + , editOwn + , deleteOwn ) where import Data.Time.Clock (getCurrentTime) @@ -23,13 +24,32 @@ getJsonIncome incomeEntity = getIncomes :: Persist [Entity Income] getIncomes = selectList [IncomeDeletedAt ==. Nothing] [] -createIncome :: UserId -> Day -> Int -> Persist IncomeId -createIncome userId date amount = do +create :: UserId -> Day -> Int -> Persist IncomeId +create userId date amount = do now <- liftIO getCurrentTime - insert (Income userId date amount now Nothing) + insert (Income userId date amount now Nothing Nothing) -deleteOwnIncome :: Entity User -> IncomeId -> Persist Bool -deleteOwnIncome user incomeId = do +editOwn :: UserId -> IncomeId -> Day -> Int -> Persist Bool +editOwn userId incomeId date amount = do + mbIncome <- get incomeId + case mbIncome of + Just income -> + if incomeUserId income == userId + then do + now <- liftIO getCurrentTime + update incomeId + [ IncomeEditedAt =. Just now + , IncomeDate =. date + , IncomeAmount =. amount + ] + return True + else + return False + Nothing -> + return False + +deleteOwn :: Entity User -> IncomeId -> Persist Bool +deleteOwn user incomeId = do mbIncome <- get incomeId case mbIncome of Just income -> diff --git a/src/server/Model/Json/EditIncome.hs b/src/server/Model/Json/EditIncome.hs new file mode 100644 index 0000000..be3c7dc --- /dev/null +++ b/src/server/Model/Json/EditIncome.hs @@ -0,0 +1,20 @@ +{-# LANGUAGE DeriveGeneric #-} + +module Model.Json.EditIncome + ( EditIncome(..) + ) where + +import GHC.Generics + +import Data.Aeson +import Data.Time.Calendar (Day) + +import Model.Database (IncomeId) + +data EditIncome = EditIncome + { id :: IncomeId + , date :: Day + , amount :: Int + } deriving (Show, Generic) + +instance FromJSON EditIncome diff --git a/src/server/Model/Message/Key.hs b/src/server/Model/Message/Key.hs index 2723dd5..4ffc890 100644 --- a/src/server/Model/Message/Key.hs +++ b/src/server/Model/Message/Key.hs @@ -97,6 +97,7 @@ data Key = | CumulativeIncomesSince | Income | MonthlyNetIncomes + | AddIncome | IncomeNotDeleted | IncomeDate | IncomeAmount diff --git a/src/server/Model/Message/Translations.hs b/src/server/Model/Message/Translations.hs index 5dcf428..32b58fc 100644 --- a/src/server/Model/Message/Translations.hs +++ b/src/server/Model/Message/Translations.hs @@ -374,6 +374,11 @@ m l MonthlyNetIncomes = English -> "Monthly incomes" French -> "Revenus mensuels nets" +m l AddIncome = + case l of + English -> "Add an income" + French -> "Ajouter un revenu" + m l IncomeNotDeleted = case l of English -> "The income could not have been deleted." -- cgit v1.2.3