From bb7e0a46882bca0b5cb36c09f531a83759d95cb4 Mon Sep 17 00:00:00 2001 From: Joris Date: Sun, 6 Sep 2015 23:49:10 +0200 Subject: Simplifying paymentJob --- src/server/Model/Payment.hs | 30 +++++++++++++----------------- 1 file changed, 13 insertions(+), 17 deletions(-) (limited to 'src/server/Model') diff --git a/src/server/Model/Payment.hs b/src/server/Model/Payment.hs index f07cec4..0db2f08 100644 --- a/src/server/Model/Payment.hs +++ b/src/server/Model/Payment.hs @@ -27,40 +27,36 @@ import qualified Model.Json.TotalPayment as TP getPunctualPayments :: Int -> Int -> Persist [P.Payment] getPunctualPayments page perPage = do xs <- select $ - from $ \(payment `InnerJoin` user) -> do - on (payment ^. PaymentUserId E.==. user ^. UserId) + 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, user) + return payment return (map getJsonPayment xs) getUserMonthlyPayments :: UserId -> Persist [P.Payment] getUserMonthlyPayments userId = - filter ((==) userId . P.userId) <$> getMonthlyPayments + filter ((==) userId . P.userId) . map getJsonPayment <$> getMonthlyPayments -getMonthlyPayments :: Persist [P.Payment] -getMonthlyPayments = do - xs <- select $ - from $ \(payment `InnerJoin` user) -> do - on (payment ^. PaymentUserId E.==. user ^. UserId) - where_ (isNothing (payment ^. PaymentDeletedAt)) - where_ (payment ^. PaymentFrequency E.==. val Monthly) - orderBy [desc (payment ^. PaymentCreation)] - return (payment, user) - return (map getJsonPayment xs) +getMonthlyPayments :: Persist [Entity Payment] +getMonthlyPayments = + selectList + [ PaymentDeletedAt P.==. Nothing + , PaymentFrequency P.==. Monthly + ] + [ Desc PaymentCreation ] -getJsonPayment :: (Entity Payment, Entity User) -> P.Payment -getJsonPayment (paymentEntity, userEntity) = +getJsonPayment :: Entity Payment -> P.Payment +getJsonPayment paymentEntity = let payment = entityVal paymentEntity in P.Payment { P.id = entityKey paymentEntity , P.creation = paymentCreation payment , P.name = paymentName payment , P.cost = paymentCost payment - , P.userId = entityKey userEntity + , P.userId = paymentUserId payment } createPayment :: UserId -> Text -> Int -> Frequency -> Persist PaymentId -- cgit v1.2.3