diff options
Diffstat (limited to 'src/server')
-rw-r--r-- | src/server/Controller/Payment.hs | 6 | ||||
-rw-r--r-- | src/server/Main.hs | 5 | ||||
-rw-r--r-- | src/server/Model/Payment.hs | 41 |
3 files changed, 20 insertions, 32 deletions
diff --git a/src/server/Controller/Payment.hs b/src/server/Controller/Payment.hs index d233aa2..d2a9258 100644 --- a/src/server/Controller/Payment.hs +++ b/src/server/Controller/Payment.hs @@ -30,10 +30,10 @@ import qualified Model.Json.PaymentId as JP import Model.Message import Model.Message.Key (Key(PaymentNotDeleted)) -getPayments :: Int -> Int -> ActionM () -getPayments page perPage = +getPayments :: ActionM () +getPayments = Secure.loggedAction (\_ -> do - (liftIO $ runDb (P.getPunctualPayments page perPage)) >>= json + (liftIO $ runDb P.getPunctualPayments) >>= json ) getMonthlyPayments :: ActionM () diff --git a/src/server/Main.hs b/src/server/Main.hs index 3539120..3ac489e 100644 --- a/src/server/Main.hs +++ b/src/server/Main.hs @@ -54,10 +54,7 @@ main = do -- Payments - get "/payments" $ do - page <- param "page" :: ActionM Int - perPage <- param "perPage" :: ActionM Int - getPayments page perPage + get "/payments" getPayments get "/monthlyPayments" getMonthlyPayments diff --git a/src/server/Model/Payment.hs b/src/server/Model/Payment.hs index 233cafa..de4a759 100644 --- a/src/server/Model/Payment.hs +++ b/src/server/Model/Payment.hs @@ -14,24 +14,18 @@ import Control.Monad.IO.Class (liftIO) import Database.Persist import qualified Database.Persist as P -import Database.Esqueleto -import qualified Database.Esqueleto as E import Model.Database import Model.Frequency import qualified Model.Json.Payment as P -getPunctualPayments :: Int -> Int -> Persist [P.Payment] -getPunctualPayments page perPage = do - xs <- select $ - from $ \(payment) -> do - where_ (isNothing (payment ^. PaymentDeletedAt)) - where_ (payment ^. PaymentFrequency E.==. val Punctual) - orderBy [desc (payment ^. PaymentCreation)] - limit . fromIntegral $ perPage - offset . fromIntegral $ (page - 1) * perPage - return payment - return (map getJsonPayment xs) +getPunctualPayments :: Persist [P.Payment] +getPunctualPayments = + map getJsonPayment <$> selectList + [ PaymentDeletedAt P.==. Nothing + , PaymentFrequency P.==. Punctual + ] + [ Desc PaymentCreation ] getUserMonthlyPayments :: UserId -> Persist [P.Payment] getUserMonthlyPayments userId = @@ -39,12 +33,11 @@ getUserMonthlyPayments userId = getMonthlyPayments :: Persist [Entity Payment] getMonthlyPayments = - select $ - from $ \payment -> do - where_ (isNothing (payment ^. PaymentDeletedAt)) - where_ (payment ^. PaymentFrequency E.==. val Monthly) - orderBy [desc (lower_ (payment ^. PaymentName))] - return payment + selectList + [ PaymentDeletedAt P.==. Nothing + , PaymentFrequency P.==. Monthly + ] + [ Desc PaymentName ] getJsonPayment :: Entity Payment -> P.Payment getJsonPayment paymentEntity = @@ -79,9 +72,7 @@ deleteOwnPayment user paymentId = do getPaymentsCount :: Persist Int getPaymentsCount = - unValue . head <$> - (select $ - from $ \payment -> do - where_ (isNothing (payment ^. PaymentDeletedAt)) - where_ (payment ^. PaymentFrequency E.==. val Punctual) - return countRows) :: Persist Int + count + [ PaymentDeletedAt P.==. Nothing + , PaymentFrequency P.==. Punctual + ] |