From b73ba24f3440b81698c9d5c370739d03f958f059 Mon Sep 17 00:00:00 2001 From: Joris Date: Thu, 31 Dec 2015 19:34:29 +0100 Subject: Fetch all the payments, do the paging only in the UI --- src/client/elm/InitViewAction.elm | 4 ++-- src/client/elm/ServerCommunication.elm | 15 ++---------- src/client/elm/Update/LoggedIn.elm | 19 +++++++-------- src/client/elm/View/LoggedIn/Paging.elm | 14 +++++------ src/client/elm/View/LoggedIn/Table.elm | 2 ++ src/server/Controller/Payment.hs | 6 ++--- src/server/Main.hs | 5 +--- src/server/Model/Payment.hs | 41 +++++++++++++-------------------- 8 files changed, 40 insertions(+), 66 deletions(-) diff --git a/src/client/elm/InitViewAction.elm b/src/client/elm/InitViewAction.elm index 7c353a7..cdfc0fc 100644 --- a/src/client/elm/InitViewAction.elm +++ b/src/client/elm/InitViewAction.elm @@ -8,7 +8,7 @@ import Json.Decode as Json exposing ((:=)) import Update exposing (Action(GoLoggedInView, GoSignInView)) -import Model.Payment exposing (Payments, paymentsDecoder, perPage) +import Model.Payment exposing (Payments, paymentsDecoder) import Model.Payer exposing (Payers, payersDecoder) import Model.User exposing (Users, usersDecoder, UserId, userIdDecoder) @@ -20,6 +20,6 @@ loggedInView = Task.map GoLoggedInView (Http.get usersDecoder "/users") `Task.andMap` (Http.get ("id" := userIdDecoder) "/whoAmI") `Task.andMap` (Http.get paymentsDecoder "/monthlyPayments") - `Task.andMap` (Http.get paymentsDecoder ("/payments?page=1&perPage=" ++ toString perPage)) + `Task.andMap` (Http.get paymentsDecoder "/payments") `Task.andMap` (Http.get ("number" := Json.int) "/payments/count") `Task.andMap` (Http.get payersDecoder "/payers") diff --git a/src/client/elm/ServerCommunication.elm b/src/client/elm/ServerCommunication.elm index 3ecfc3b..390bcfd 100644 --- a/src/client/elm/ServerCommunication.elm +++ b/src/client/elm/ServerCommunication.elm @@ -36,7 +36,6 @@ type Communication = | SetIncome Time Int | DeletePayment Payment Int | DeleteMonthlyPayment PaymentId - | UpdatePage Int | SignOut serverCommunications : Signal.Mailbox Communication @@ -59,8 +58,7 @@ sendRequest communication = AddPayment userId name cost -> post (addPaymentURL name cost Punctual) - |> flip Task.andThen (always (getPaymentsAtPage 1)) - |> Task.map (\payments -> U.UpdateLoggedIn (UL.AddPayment userId name cost payments)) + |> Task.map (always (U.UpdateLoggedIn (UL.AddPayment userId name cost))) AddMonthlyPayment name cost -> post (addPaymentURL name cost Monthly) @@ -69,17 +67,12 @@ sendRequest communication = DeletePayment payment currentPage -> post (deletePaymentURL payment.id) - |> flip Task.andThen (always (getPaymentsAtPage currentPage)) - |> Task.map (\payments -> U.UpdateLoggedIn (UL.DeletePayment payment payments)) + |> Task.map (always (U.UpdateLoggedIn (UL.DeletePayment payment))) DeleteMonthlyPayment id -> post (deletePaymentURL id) |> Task.map (always (U.UpdateLoggedIn (UL.UpdateMonthly (UM.DeletePayment id)))) - UpdatePage page -> - getPaymentsAtPage page - |> flip Task.andThen (Task.succeed << U.UpdateLoggedIn << UL.UpdatePage page) - SetIncome currentTime amount -> post ("/income?amount=" ++ (toString amount)) |> Task.map (always (U.UpdateLoggedIn (UL.UpdateAccount (UA.UpdateIncome currentTime amount)))) @@ -88,10 +81,6 @@ sendRequest communication = post "/signOut" |> Task.map (always U.GoSignInView) -getPaymentsAtPage : Int -> Task Http.Error Payments -getPaymentsAtPage page = - Http.get paymentsDecoder ("payments?page=" ++ toString page ++ "&perPage=" ++ toString perPage) - addPaymentURL : String -> Int -> Frequency -> String addPaymentURL name cost frequency = "/payment/add?name=" ++ name ++ "&cost=" ++ (toString cost) ++ "&frequency=" ++ (toString frequency) diff --git a/src/client/elm/Update/LoggedIn.elm b/src/client/elm/Update/LoggedIn.elm index 5292c25..2dc65c3 100644 --- a/src/client/elm/Update/LoggedIn.elm +++ b/src/client/elm/Update/LoggedIn.elm @@ -19,11 +19,11 @@ import Update.LoggedIn.Account as UA type LoggedAction = UpdateAdd AddPaymentAction | UpdatePayments Payments - | AddPayment UserId String Int Payments + | AddPayment UserId String Int | AddMonthlyPayment PaymentId String Int | ToggleEdit PaymentId - | DeletePayment Payment Payments - | UpdatePage Int Payments + | DeletePayment Payment + | UpdatePage Int | UpdateMonthly UM.MonthlyAction | UpdateAccount UA.AccountAction @@ -34,10 +34,9 @@ updateLoggedIn model action loggedInView = { loggedInView | add = updateAddPayment addPaymentAction loggedInView.add } UpdatePayments payments -> { loggedInView | payments = payments } - AddPayment userId name cost payments -> + AddPayment userId name cost -> { loggedInView - | payments = payments - , currentPage = 1 + | currentPage = 1 , add = initAddPayment Punctual , account = UA.updateAccount (UA.UpdatePayer userId model.currentTime cost) loggedInView.account , paymentsCount = loggedInView.paymentsCount + 1 @@ -51,16 +50,14 @@ updateLoggedIn model action loggedInView = } ToggleEdit id -> { loggedInView | paymentEdition = if loggedInView.paymentEdition == Just id then Nothing else Just id } - DeletePayment payment payments -> + DeletePayment payment -> { loggedInView - | payments = payments - , account = UA.updateAccount (UA.UpdatePayer payment.userId (Date.toTime payment.creation) -payment.cost) loggedInView.account + | account = UA.updateAccount (UA.UpdatePayer payment.userId (Date.toTime payment.creation) -payment.cost) loggedInView.account , paymentsCount = loggedInView.paymentsCount - 1 } - UpdatePage page payments -> + UpdatePage page -> { loggedInView | currentPage = page - , payments = payments } UpdateMonthly monthlyAction -> { loggedInView | monthly = UM.updateMonthly monthlyAction loggedInView.monthly } diff --git a/src/client/elm/View/LoggedIn/Paging.elm b/src/client/elm/View/LoggedIn/Paging.elm index 608113b..e40c5aa 100644 --- a/src/client/elm/View/LoggedIn/Paging.elm +++ b/src/client/elm/View/LoggedIn/Paging.elm @@ -9,8 +9,6 @@ import Html.Events exposing (..) import Model.View.LoggedInView exposing (..) import Model.Payment exposing (perPage) -import ServerCommunication as SC exposing (serverCommunications) - import Update exposing (..) import Update.LoggedIn exposing (..) @@ -58,7 +56,7 @@ firstPage : Html firstPage = button [ class "page" - , onClick serverCommunications.address (SC.UpdatePage 1) + , onClick actions.address (UpdateLoggedIn (UpdatePage 1)) ] [ renderIcon "fast-backward" ] @@ -66,7 +64,7 @@ previousPage : LoggedInView -> Html previousPage loggedInView = button [ class "page" - , onClick serverCommunications.address (SC.UpdatePage (loggedInView.currentPage - 1)) + , onClick actions.address (UpdateLoggedIn (UpdatePage (loggedInView.currentPage - 1))) ] [ renderIcon "backward" ] @@ -74,7 +72,7 @@ nextPage : LoggedInView -> Html nextPage loggedInView = button [ class "page" - , onClick serverCommunications.address (SC.UpdatePage (loggedInView.currentPage + 1)) + , onClick actions.address (UpdateLoggedIn (UpdatePage (loggedInView.currentPage + 1))) ] [ renderIcon "forward" ] @@ -82,7 +80,7 @@ lastPage : Int -> Html lastPage maxPage = button [ class "page" - , onClick serverCommunications.address (SC.UpdatePage maxPage) + , onClick actions.address (UpdateLoggedIn (UpdatePage maxPage)) ] [ renderIcon "fast-forward" ] @@ -94,7 +92,7 @@ paymentsPage loggedInView page = [ ("page", True) , ("current", onCurrentPage) ] - , onClick serverCommunications.address <| - if onCurrentPage then SC.NoCommunication else SC.UpdatePage page + , onClick actions.address <| + if onCurrentPage then NoOp else UpdateLoggedIn (UpdatePage page) ] [ text (toString page) ] diff --git a/src/client/elm/View/LoggedIn/Table.elm b/src/client/elm/View/LoggedIn/Table.elm index f5a08b5..51a7b73 100644 --- a/src/client/elm/View/LoggedIn/Table.elm +++ b/src/client/elm/View/LoggedIn/Table.elm @@ -49,6 +49,8 @@ paymentLines model loggedInView = loggedInView.payments |> List.sortBy (Date.toTime << .creation) |> List.reverse + |> List.drop ((loggedInView.currentPage - 1) * perPage) + |> List.take perPage |> List.map (paymentLine model loggedInView) paymentLine : Model -> LoggedInView -> Payment -> Html 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 + ] -- cgit v1.2.3