From 573d2af0df47c0cb78423e3e9906f300bdde9730 Mon Sep 17 00:00:00 2001 From: nek0 Date: Sat, 10 Aug 2019 10:36:20 +0200 Subject: [PATCH] continue implementation --- src/Main.hs | 4 +++- src/Model/Journal.hs | 45 ++++++++++++++++++++++++++++++++++++++++++-- 2 files changed, 46 insertions(+), 3 deletions(-) diff --git a/src/Main.hs b/src/Main.hs index de8f784..e3109a5 100644 --- a/src/Main.hs +++ b/src/Main.hs @@ -81,7 +81,9 @@ app initState = productStockUpdate :<|> productList :<|> - buy + buy :<|> + + journalShow ) userApi :: Proxy UserAPI diff --git a/src/Model/Journal.hs b/src/Model/Journal.hs index 19a1c0b..35e3c26 100644 --- a/src/Model/Journal.hs +++ b/src/Model/Journal.hs @@ -2,6 +2,8 @@ {-# LANGUAGE Arrows #-} module Model.Journal where +import Data.Maybe (isJust) + import Data.Time (UTCTime) import Data.Time.Clock @@ -25,7 +27,7 @@ import Types initJournal :: PGS.Query initJournal = mconcat [ "CREATE TABLE IF NOT EXISTS \"journal\" (" - , "journal_id SERIAL NOT NULL ON DELETE CASCADE," + , "journal_id SERIAL NOT NULL," , "journal_timestamp TIMESTAMPTZ NOT NULL," , "journal_description TEXT NOT NULL," , "journal_total_amount INTEGER NOT NULL," @@ -52,12 +54,51 @@ journalTable = table "journal" ( ( tableField "journal_id" , tableField "journal_timestamp" , tableField "journal_description" - , tableField "journal_total_amout" + , tableField "journal_total_amount" , tableField "journal_entry_is_check" ) ) +selectJournalEntries + :: Maybe Int -- limit + -> Maybe Int -- offset + -> PGS.Connection + -> MateHandler [JournalEntry] +selectJournalEntries mlimit moffset conn = liftIO $ do + let lim = case mlimit of + Just l -> limit (l + 1) + Nothing -> id + off = case moffset of + Just o -> offset o + Nothing -> id + entries <- runSelect conn + ( proc () -> do + ret <- lim $ off $ orderBy (desc (\(id_, _, _, _, _) -> id_)) + (queryTable journalTable) -< () + returnA -< ret + ) :: IO + [ ( Int + , UTCTime + , T.Text + , Int + , Bool + ) + ] + return $ fst $ foldr + (\(id_, ts, desc, tot, check) (fin, last)-> + (JournalEntry id_ desc ts (tot - last) tot check : fin, tot) + ) + ( [] + , if isJust mlimit && not (null entries) + then (\(_, _, _, x, _) -> x) (last entries) + else 0 + ) + ( if isJust mlimit && length entries > 1 + then init entries + else entries + ) + selectLatestJournalEntry :: PGS.Connection -> MateHandler (Maybe JournalEntry)