diff options
author | Joris | 2016-06-26 12:31:24 +0200 |
---|---|---|
committer | Joris | 2016-06-26 12:31:24 +0200 |
commit | 9ec84e3a20c767f6525639f58cd22715e302b88d (patch) | |
tree | a080552859180707472c1a289080857c0a54fc06 /src/server | |
parent | 5cb36652ccf07c9e0995ebc421a837ad7d258469 (diff) |
Add an editable date field for punctual payment creation
Diffstat (limited to 'src/server')
-rw-r--r-- | src/server/Controller/Income.hs | 10 | ||||
-rw-r--r-- | src/server/Controller/Payment.hs | 34 | ||||
-rw-r--r-- | src/server/Main.hs | 30 | ||||
-rw-r--r-- | src/server/Model/Database.hs | 3 | ||||
-rw-r--r-- | src/server/Model/Income.hs | 8 | ||||
-rw-r--r-- | src/server/Model/Init.hs | 4 | ||||
-rw-r--r-- | src/server/Model/Json/CreateIncome.hs (renamed from src/server/Model/Json/AddIncome.hs) | 10 | ||||
-rw-r--r-- | src/server/Model/Json/CreatePayment.hs | 22 | ||||
-rw-r--r-- | src/server/Model/Json/Income.hs | 2 | ||||
-rw-r--r-- | src/server/Model/Json/Payment.hs | 4 | ||||
-rw-r--r-- | src/server/Model/Message/Key.hs | 5 | ||||
-rw-r--r-- | src/server/Model/Message/Translations.hs | 11 | ||||
-rw-r--r-- | src/server/Model/Payment.hs | 31 | ||||
-rw-r--r-- | src/server/MonthlyPaymentJob.hs | 9 | ||||
-rw-r--r-- | src/server/Utils/Time.hs | 23 |
15 files changed, 108 insertions, 98 deletions
diff --git a/src/server/Controller/Income.hs b/src/server/Controller/Income.hs index 70e40ce..fa575c5 100644 --- a/src/server/Controller/Income.hs +++ b/src/server/Controller/Income.hs @@ -2,7 +2,7 @@ module Controller.Income ( getIncomes - , addIncome + , createIncome , deleteOwnIncome ) where @@ -24,7 +24,7 @@ import Json (jsonId) import Model.Database import qualified Model.Income as Income import qualified Model.Message.Key as Key -import qualified Model.Json.AddIncome as Json +import qualified Model.Json.CreateIncome as Json getIncomes :: ActionM () getIncomes = @@ -32,10 +32,10 @@ getIncomes = (liftIO $ map Income.getJsonIncome <$> runDb Income.getIncomes) >>= json ) -addIncome :: Json.AddIncome -> ActionM () -addIncome (Json.AddIncome date amount) = +createIncome :: Json.CreateIncome -> ActionM () +createIncome (Json.CreateIncome date amount) = Secure.loggedAction (\user -> - (liftIO . runDb $ Income.addIncome (entityKey user) date amount) >>= jsonId + (liftIO . runDb $ Income.createIncome (entityKey user) date amount) >>= jsonId ) deleteOwnIncome :: Text -> ActionM () diff --git a/src/server/Controller/Payment.hs b/src/server/Controller/Payment.hs index 294e4c4..55edea5 100644 --- a/src/server/Controller/Payment.hs +++ b/src/server/Controller/Payment.hs @@ -1,9 +1,9 @@ {-# LANGUAGE OverloadedStrings #-} module Controller.Payment - ( getPayments - , createPayment - , deleteOwnPayment + ( list + , create + , deleteOwn ) where import Web.Scotty @@ -16,34 +16,32 @@ import Control.Monad.IO.Class (liftIO) import Data.Text (Text) import qualified Data.Text.Lazy as TL -import qualified Data.Aeson.Types as Json import qualified Secure -import Json (jsonObject) +import Json (jsonId) import Model.Database -import qualified Model.Payment as P -import Model.Frequency +import qualified Model.Payment as Payment import Model.Message.Key (Key(PaymentNotDeleted)) +import qualified Model.Json.CreatePayment as Json -getPayments :: ActionM () -getPayments = +list :: ActionM () +list = Secure.loggedAction (\_ -> do - (liftIO $ runDb P.getPayments) >>= json + (liftIO $ runDb Payment.list) >>= json ) -createPayment :: Text -> Int -> Frequency -> ActionM () -createPayment name cost frequency = - Secure.loggedAction (\user -> do - paymentId <- liftIO . runDb $ P.createPayment (entityKey user) name cost frequency - jsonObject [("id", Json.Number . fromIntegral . keyToInt64 $ paymentId)] +create :: Json.CreatePayment -> ActionM () +create (Json.CreatePayment date name cost frequency) = + Secure.loggedAction (\user -> + (liftIO . runDb $ Payment.create (entityKey user) date name cost frequency) >>= jsonId ) -deleteOwnPayment :: Text -> ActionM () -deleteOwnPayment paymentId = +deleteOwn :: Text -> ActionM () +deleteOwn paymentId = Secure.loggedAction (\user -> do - deleted <- liftIO . runDb $ P.deleteOwnPayment user (textToKey paymentId) + deleted <- liftIO . runDb $ Payment.deleteOwn user (textToKey paymentId) if deleted then status ok200 diff --git a/src/server/Main.hs b/src/server/Main.hs index d04a3ac..72e8675 100644 --- a/src/server/Main.hs +++ b/src/server/Main.hs @@ -14,12 +14,10 @@ import qualified Data.Text.IO as T import Controller.Index import Controller.SignIn -import Controller.Payment -import Controller.User +import Controller.Payment as Payment import Controller.Income import Model.Database (runMigrations) -import Model.Frequency import qualified Conf @@ -52,32 +50,18 @@ main = do post "/signOut" (signOut conf) - -- Users + -- Payments - get "/users" getUsers + post "/payment" $ jsonData >>= Payment.create - get "/whoAmI" whoAmI + delete "/payment" $ do + paymentId <- param "id" :: ActionM Text + Payment.deleteOwn paymentId -- Incomes - get "/incomes" getIncomes - - post "/income" $ jsonData >>= addIncome + post "/income" $ jsonData >>= createIncome delete "/income" $ do incomeId <- param "id" :: ActionM Text deleteOwnIncome incomeId - - -- Payments - - get "/payments" getPayments - - post "/payment/add" $ do - name <- param "name" :: ActionM Text - cost <- param "cost" :: ActionM Int - frequency <- param "frequency" :: ActionM Frequency - createPayment name cost frequency - - delete "/payment" $ do - paymentId <- param "id" :: ActionM Text - deleteOwnPayment paymentId diff --git a/src/server/Model/Database.hs b/src/server/Model/Database.hs index 5df925a..7e67f9a 100644 --- a/src/server/Model/Database.hs +++ b/src/server/Model/Database.hs @@ -35,9 +35,10 @@ User deriving Show Payment userId UserId - creation UTCTime + date Day name Text cost Int + createdAt UTCTime deletedAt UTCTime Maybe frequency Frequency deriving Show diff --git a/src/server/Model/Income.hs b/src/server/Model/Income.hs index 119a44f..62ab0ed 100644 --- a/src/server/Model/Income.hs +++ b/src/server/Model/Income.hs @@ -1,7 +1,7 @@ module Model.Income ( getJsonIncome , getIncomes - , addIncome + , createIncome , deleteOwnIncome ) where @@ -23,10 +23,10 @@ getJsonIncome incomeEntity = getIncomes :: Persist [Entity Income] getIncomes = selectList [IncomeDeletedAt ==. Nothing] [] -addIncome :: UserId -> Day -> Int -> Persist IncomeId -addIncome userId day amount = do +createIncome :: UserId -> Day -> Int -> Persist IncomeId +createIncome userId date amount = do now <- liftIO getCurrentTime - insert (Income userId day amount now Nothing) + insert (Income userId date amount now Nothing) deleteOwnIncome :: Entity User -> IncomeId -> Persist Bool deleteOwnIncome user incomeId = do diff --git a/src/server/Model/Init.hs b/src/server/Model/Init.hs index 167eead..09ac627 100644 --- a/src/server/Model/Init.hs +++ b/src/server/Model/Init.hs @@ -11,7 +11,7 @@ import Database.Persist import Model.Database import Model.Json.Init (Init, Init(Init)) -import Model.Payment (getPayments) +import qualified Model.Payment as Payment import Model.User (getUsers, getJsonUser) import Model.Income (getIncomes, getJsonIncome) @@ -21,7 +21,7 @@ getInit :: Entity User -> Persist Init getInit user = liftIO . runDb $ do users <- getUsers - payments <- getPayments + payments <- Payment.list incomes <- getIncomes return $ Init { Init.users = map getJsonUser users diff --git a/src/server/Model/Json/AddIncome.hs b/src/server/Model/Json/CreateIncome.hs index 6570ba9..cf9b1c3 100644 --- a/src/server/Model/Json/AddIncome.hs +++ b/src/server/Model/Json/CreateIncome.hs @@ -1,7 +1,7 @@ {-# LANGUAGE DeriveGeneric #-} -module Model.Json.AddIncome - ( AddIncome(..) +module Model.Json.CreateIncome + ( CreateIncome(..) ) where import GHC.Generics @@ -9,9 +9,9 @@ import GHC.Generics import Data.Aeson import Data.Time.Calendar (Day) -data AddIncome = AddIncome - { day :: Day +data CreateIncome = CreateIncome + { date :: Day , amount :: Int } deriving (Show, Generic) -instance FromJSON AddIncome +instance FromJSON CreateIncome diff --git a/src/server/Model/Json/CreatePayment.hs b/src/server/Model/Json/CreatePayment.hs new file mode 100644 index 0000000..f117daf --- /dev/null +++ b/src/server/Model/Json/CreatePayment.hs @@ -0,0 +1,22 @@ +{-# LANGUAGE DeriveGeneric #-} + +module Model.Json.CreatePayment + ( CreatePayment(..) + ) where + +import GHC.Generics + +import Data.Aeson +import Data.Time.Calendar (Day) +import Data.Text (Text) + +import Model.Frequency (Frequency) + +data CreatePayment = CreatePayment + { date :: Day + , name :: Text + , cost :: Int + , frequency :: Frequency + } deriving (Show, Generic) + +instance FromJSON CreatePayment diff --git a/src/server/Model/Json/Income.hs b/src/server/Model/Json/Income.hs index e80ab63..bb1ac97 100644 --- a/src/server/Model/Json/Income.hs +++ b/src/server/Model/Json/Income.hs @@ -14,7 +14,7 @@ import Model.Database (IncomeId, UserId) data Income = Income { id :: IncomeId , userId :: UserId - , day :: Day + , date :: Day , amount :: Int } deriving (Show, Generic) diff --git a/src/server/Model/Json/Payment.hs b/src/server/Model/Json/Payment.hs index 7f4de15..04c6de8 100644 --- a/src/server/Model/Json/Payment.hs +++ b/src/server/Model/Json/Payment.hs @@ -6,16 +6,16 @@ module Model.Json.Payment import GHC.Generics -import Data.Time import Data.Text (Text) import Data.Aeson +import Data.Time.Calendar (Day) import Model.Database (PaymentId, UserId) import Model.Frequency data Payment = Payment { id :: PaymentId - , creation :: UTCTime + , date :: Day , name :: Text , cost :: Int , userId :: UserId diff --git a/src/server/Model/Message/Key.hs b/src/server/Model/Message/Key.hs index 8b957f1..093024b 100644 --- a/src/server/Model/Message/Key.hs +++ b/src/server/Model/Message/Key.hs @@ -75,6 +75,7 @@ data Key = | PaymentName | PaymentCost + | PaymentDate | PaymentPunctual | PaymentMonthly @@ -90,7 +91,7 @@ data Key = | Income | MonthlyNetIncomes | IncomeNotDeleted - | IncomeCreation + | IncomeDate | IncomeAmount | ConfirmDelete @@ -117,4 +118,4 @@ data Key = deriving (Enum, Bounded, Show) instance Json.ToJSON Key where - toJSON = Json.String . T.pack . show + toJSON = Json.String . T.pack . show diff --git a/src/server/Model/Message/Translations.hs b/src/server/Model/Message/Translations.hs index df3f402..6522d75 100644 --- a/src/server/Model/Message/Translations.hs +++ b/src/server/Model/Message/Translations.hs @@ -312,6 +312,11 @@ m l PaymentCost = English -> "Cost" French -> "Coût" +m l PaymentDate = + case l of + English -> "Date" + French -> "Date" + m l PaymentPunctual = case l of English -> "Punctual" @@ -344,10 +349,10 @@ m l IncomeNotDeleted = English -> "The income could not have been deleted." French -> "Le revenu n'a pas pu être supprimé." -m l IncomeCreation = +m l IncomeDate = case l of - English -> "Creation" - French -> "Création" + English -> "Date" + French -> "Date" m l IncomeAmount = case l of diff --git a/src/server/Model/Payment.hs b/src/server/Model/Payment.hs index 28f1a09..51ca152 100644 --- a/src/server/Model/Payment.hs +++ b/src/server/Model/Payment.hs @@ -1,14 +1,15 @@ {-# LANGUAGE OverloadedStrings #-} module Model.Payment - ( getPayments - , getMonthlyPayments - , createPayment - , deleteOwnPayment + ( list + , listMonthly + , create + , deleteOwn ) where import Data.Text (Text) import Data.Time.Clock (getCurrentTime) +import Data.Time.Calendar (Day) import Control.Monad.IO.Class (liftIO) @@ -18,14 +19,14 @@ import Model.Database import Model.Frequency import qualified Model.Json.Payment as P -getPayments :: Persist [P.Payment] -getPayments = +list :: Persist [P.Payment] +list = map getJsonPayment <$> selectList [ PaymentDeletedAt ==. Nothing ] - [ Desc PaymentCreation ] + [] -getMonthlyPayments :: Persist [Entity Payment] -getMonthlyPayments = +listMonthly :: Persist [Entity Payment] +listMonthly = selectList [ PaymentDeletedAt ==. Nothing , PaymentFrequency ==. Monthly @@ -37,20 +38,20 @@ getJsonPayment paymentEntity = let payment = entityVal paymentEntity in P.Payment { P.id = entityKey paymentEntity - , P.creation = paymentCreation payment + , P.date = paymentDate payment , P.name = paymentName payment , P.cost = paymentCost payment , P.userId = paymentUserId payment , P.frequency = paymentFrequency payment } -createPayment :: UserId -> Text -> Int -> Frequency -> Persist PaymentId -createPayment userId name cost frequency = do +create :: UserId -> Day -> Text -> Int -> Frequency -> Persist PaymentId +create userId date name cost frequency = do now <- liftIO getCurrentTime - insert (Payment userId now name cost Nothing frequency) + insert (Payment userId date name cost now Nothing frequency) -deleteOwnPayment :: Entity User -> PaymentId -> Persist Bool -deleteOwnPayment user paymentId = do +deleteOwn :: Entity User -> PaymentId -> Persist Bool +deleteOwn user paymentId = do mbPayment <- get paymentId case mbPayment of Just payment -> diff --git a/src/server/MonthlyPaymentJob.hs b/src/server/MonthlyPaymentJob.hs index f5f6878..c4022c9 100644 --- a/src/server/MonthlyPaymentJob.hs +++ b/src/server/MonthlyPaymentJob.hs @@ -11,11 +11,11 @@ import Database.Persist (entityVal, insert) import Job (jobListener) import Model.Database -import Model.Payment (getMonthlyPayments) +import qualified Model.Payment as Payment import Model.JobKind import Model.Frequency -import Utils.Time (belongToCurrentMonth) +import Utils.Time (belongToCurrentMonth, timeToDay) monthlyPaymentJobListener :: IO () monthlyPaymentJobListener = @@ -26,7 +26,8 @@ monthlyPaymentJobListener = monthlyPaymentJob :: Persist () monthlyPaymentJob = do - monthlyPayments <- map entityVal <$> getMonthlyPayments + monthlyPayments <- map entityVal <$> Payment.listMonthly now <- liftIO $ getCurrentTime - let punctualPayments = map (\p -> p { paymentFrequency = Punctual, paymentCreation = now }) monthlyPayments + actualDay <- liftIO $ timeToDay now + let punctualPayments = map (\p -> p { paymentFrequency = Punctual, paymentDate = actualDay, paymentCreatedAt = now }) monthlyPayments sequence_ $ map insert punctualPayments diff --git a/src/server/Utils/Time.hs b/src/server/Utils/Time.hs index 0d6ed73..e8c7ac1 100644 --- a/src/server/Utils/Time.hs +++ b/src/server/Utils/Time.hs @@ -1,7 +1,6 @@ module Utils.Time ( belongToCurrentMonth - , getLocalDate - , Date(..) + , timeToDay ) where import Data.Time.Clock @@ -10,18 +9,16 @@ import Data.Time.Calendar belongToCurrentMonth :: UTCTime -> IO Bool belongToCurrentMonth time = do - timeMonth <- month <$> getLocalDate time - actualMonth <- month <$> (getCurrentTime >>= getLocalDate) + timeMonth <- dayMonth <$> timeToDay time + actualMonth <- dayMonth <$> (getCurrentTime >>= timeToDay) return (timeMonth == actualMonth) -getLocalDate :: UTCTime -> IO Date -getLocalDate time = do +timeToDay :: UTCTime -> IO Day +timeToDay time = do timeZone <- getCurrentTimeZone - let (y, m, d) = toGregorian . localDay $ utcToLocalTime timeZone time - return (Date y m d) + return . localDay $ utcToLocalTime timeZone time -data Date = Date - { year :: Integer - , month :: Int - , day :: Int - } +dayMonth :: Day -> Int +dayMonth day = + let (_, month, _) = toGregorian day + in month |