diff options
author | Joris | 2019-10-27 20:26:29 +0100 |
---|---|---|
committer | Joris | 2019-10-27 20:26:29 +0100 |
commit | b97ad942495352c3fc1e0c820cfba82a9693ac7a (patch) | |
tree | f554831888929e2eff5e1fe478f92758637d37cf /server/src | |
parent | 8ef4d96644bce59bbb736af6359e644743e5610a (diff) |
WIP Set up server side paging for incomes
Diffstat (limited to 'server/src')
-rw-r--r-- | server/src/Controller/Income.hs | 13 | ||||
-rw-r--r-- | server/src/Main.hs | 5 | ||||
-rw-r--r-- | server/src/Persistence/Income.hs | 26 |
3 files changed, 42 insertions, 2 deletions
diff --git a/server/src/Controller/Income.hs b/server/src/Controller/Income.hs index 236e032..3272cbf 100644 --- a/server/src/Controller/Income.hs +++ b/server/src/Controller/Income.hs @@ -1,5 +1,6 @@ module Controller.Income ( list + , listv2 , create , edit , delete @@ -12,7 +13,7 @@ import Web.Scotty hiding (delete) import Common.Model (CreateIncomeForm (..), EditIncomeForm (..), IncomeId, - User (..)) + IncomesAndCount (..), User (..)) import qualified Controller.Helper as ControllerHelper import Model.CreateIncome (CreateIncome (..)) @@ -28,6 +29,16 @@ list = (liftIO . Query.run $ IncomePersistence.list) >>= json ) +listv2 :: Int -> Int -> ActionM () +listv2 page perPage = + Secure.loggedAction (\_ -> + (liftIO . Query.run $ do + count <- IncomePersistence.count + incomes <- IncomePersistence.listv2 page perPage + return $ IncomesAndCount incomes count + ) >>= json + ) + create :: CreateIncomeForm -> ActionM () create form = Secure.loggedAction (\user -> diff --git a/server/src/Main.hs b/server/src/Main.hs index 9882092..00e8d1c 100644 --- a/server/src/Main.hs +++ b/server/src/Main.hs @@ -54,6 +54,11 @@ main = do paymentId <- S.param "id" Payment.delete paymentId + S.get "/api/v2/incomes" $ do + page <- S.param "page" + perPage <- S.param "perPage" + Income.listv2 page perPage + S.get "/api/incomes" $ Income.list diff --git a/server/src/Persistence/Income.hs b/server/src/Persistence/Income.hs index 2b9bf0c..de55a18 100644 --- a/server/src/Persistence/Income.hs +++ b/server/src/Persistence/Income.hs @@ -1,5 +1,7 @@ module Persistence.Income - ( list + ( count + , list + , listv2 , create , edit , delete @@ -29,6 +31,18 @@ instance FromRow Row where SQLite.field <*> SQLite.field) +data Count = Count Int + +instance FromRow Count where + fromRow = Count <$> SQLite.field + +count :: Query Int +count = + Query (\conn -> + (\[Count n] -> n) <$> + SQLite.query_ conn "SELECT COUNT(*) FROM income WHERE deleted_at IS NULL" + ) + list :: Query [Income] list = Query (\conn -> @@ -36,6 +50,16 @@ list = SQLite.query_ conn "SELECT * FROM income WHERE deleted_at IS NULL" ) +listv2 :: Int -> Int -> Query [Income] +listv2 page perPage = + Query (\conn -> + map (\(Row i) -> i) <$> + SQLite.query + conn + "SELECT * FROM income WHERE deleted_at IS NULL ORDER BY date DESC LIMIT ? OFFSET ?" + (perPage, (page - 1) * perPage) + ) + create :: UserId -> Day -> Int -> Query Income create userId date amount = Query (\conn -> do |