diff options
author | Joris | 2016-06-27 14:36:03 +0200 |
---|---|---|
committer | Joris | 2016-06-27 14:36:03 +0200 |
commit | f605541cbaaa3c339eef8f345547bcd653d3f721 (patch) | |
tree | 1e800df7736e482290ca138726595e067e4a5cf9 /src/server/Model | |
parent | 885dfd7708e338a3220c85b7f22a3ac267aad3f7 (diff) |
Add the edit functionnality on payments
Diffstat (limited to 'src/server/Model')
-rw-r--r-- | src/server/Model/Database.hs | 5 | ||||
-rw-r--r-- | src/server/Model/Json/CreatePayment.hs | 4 | ||||
-rw-r--r-- | src/server/Model/Json/EditPayment.hs | 24 | ||||
-rw-r--r-- | src/server/Model/Message/Key.hs | 2 | ||||
-rw-r--r-- | src/server/Model/Message/Translations.hs | 10 | ||||
-rw-r--r-- | src/server/Model/Payment.hs | 34 |
6 files changed, 69 insertions, 10 deletions
diff --git a/src/server/Model/Database.hs b/src/server/Model/Database.hs index 7e67f9a..4526fc5 100644 --- a/src/server/Model/Database.hs +++ b/src/server/Model/Database.hs @@ -35,12 +35,13 @@ User deriving Show Payment userId UserId - date Day name Text cost Int + date Day + frequency Frequency createdAt UTCTime + editedAt UTCTime Maybe deletedAt UTCTime Maybe - frequency Frequency deriving Show SignIn token Text diff --git a/src/server/Model/Json/CreatePayment.hs b/src/server/Model/Json/CreatePayment.hs index f117daf..4ba9e1a 100644 --- a/src/server/Model/Json/CreatePayment.hs +++ b/src/server/Model/Json/CreatePayment.hs @@ -13,9 +13,9 @@ import Data.Text (Text) import Model.Frequency (Frequency) data CreatePayment = CreatePayment - { date :: Day - , name :: Text + { name :: Text , cost :: Int + , date :: Day , frequency :: Frequency } deriving (Show, Generic) diff --git a/src/server/Model/Json/EditPayment.hs b/src/server/Model/Json/EditPayment.hs new file mode 100644 index 0000000..4e91000 --- /dev/null +++ b/src/server/Model/Json/EditPayment.hs @@ -0,0 +1,24 @@ +{-# LANGUAGE DeriveGeneric #-} + +module Model.Json.EditPayment + ( EditPayment(..) + ) where + +import GHC.Generics + +import Data.Aeson +import Data.Time.Calendar (Day) +import Data.Text (Text) + +import Model.Frequency (Frequency) +import Model.Database (PaymentId) + +data EditPayment = EditPayment + { id :: PaymentId + , name :: Text + , cost :: Int + , date :: Day + , frequency :: Frequency + } deriving (Show, Generic) + +instance FromJSON EditPayment diff --git a/src/server/Model/Message/Key.hs b/src/server/Model/Message/Key.hs index 84ff486..b60067c 100644 --- a/src/server/Model/Message/Key.hs +++ b/src/server/Model/Message/Key.hs @@ -63,6 +63,8 @@ data Key = | Frequency | InvalidFrequency | AddPayment + | ClonePayment + | EditPayment | PaymentNotDeleted | Punctual | Monthly diff --git a/src/server/Model/Message/Translations.hs b/src/server/Model/Message/Translations.hs index a29b84e..3c92601 100644 --- a/src/server/Model/Message/Translations.hs +++ b/src/server/Model/Message/Translations.hs @@ -245,6 +245,16 @@ m l AddPayment = English -> "Add a payment" French -> "Ajouter un paiement" +m l ClonePayment = + case l of + English -> "Clone a payment" + French -> "Cloner un paiement" + +m l EditPayment = + case l of + English -> "Edit a payment" + French -> "Modifier un paiement" + m l PaymentNotDeleted = case l of English -> "The payment could not have been deleted." diff --git a/src/server/Model/Payment.hs b/src/server/Model/Payment.hs index 51ca152..0d5e188 100644 --- a/src/server/Model/Payment.hs +++ b/src/server/Model/Payment.hs @@ -4,6 +4,7 @@ module Model.Payment ( list , listMonthly , create + , editOwn , deleteOwn ) where @@ -45,17 +46,38 @@ getJsonPayment paymentEntity = , P.frequency = paymentFrequency payment } -create :: UserId -> Day -> Text -> Int -> Frequency -> Persist PaymentId -create userId date name cost frequency = do +create :: UserId -> Text -> Int -> Day -> Frequency -> Persist PaymentId +create userId name cost date frequency = do now <- liftIO getCurrentTime - insert (Payment userId date name cost now Nothing frequency) + insert (Payment userId name cost date frequency now Nothing Nothing) -deleteOwn :: Entity User -> PaymentId -> Persist Bool -deleteOwn user paymentId = do +editOwn :: UserId -> PaymentId -> Text -> Int -> Day -> Frequency -> Persist Bool +editOwn userId paymentId name cost date frequency = do mbPayment <- get paymentId case mbPayment of Just payment -> - if paymentUserId payment == entityKey user + if paymentUserId payment == userId + then do + now <- liftIO getCurrentTime + update paymentId + [ PaymentEditedAt =. Just now + , PaymentName =. name + , PaymentCost =. cost + , PaymentDate =. date + , PaymentFrequency =. frequency + ] + return True + else + return False + Nothing -> + return False + +deleteOwn :: UserId -> PaymentId -> Persist Bool +deleteOwn userId paymentId = do + mbPayment <- get paymentId + case mbPayment of + Just payment -> + if paymentUserId payment == userId then do now <- liftIO getCurrentTime update paymentId [PaymentDeletedAt =. Just now] |