aboutsummaryrefslogtreecommitdiff
path: root/server/src/Persistence/Income.hs
blob: ba7ad19fba02cfdcd567856e3a48524dee35a668 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
module Persistence.Income
  ( count
  , list
  , listAll
  , listModifiedSince
  , create
  , edit
  , delete
  , definedForAll
  ) where

import qualified Data.List              as L
import qualified Data.Maybe             as Maybe
import qualified Data.Text              as T
import           Data.Time.Calendar     (Day)
import           Data.Time.Clock        (UTCTime)
import           Data.Time.Clock        (getCurrentTime)
import           Database.SQLite.Simple (FromRow (fromRow), Only (Only))
import qualified Database.SQLite.Simple as SQLite
import           Prelude                hiding (id, until)

import           Common.Model           (Income (..), IncomeId, PaymentId,
                                         UserId)

import           Model.Query            (Query (Query))

newtype Row = Row Income

instance FromRow Row where
  fromRow = Row <$> (Income <$>
    SQLite.field <*>
    SQLite.field <*>
    SQLite.field <*>
    SQLite.field <*>
    SQLite.field <*>
    SQLite.field <*>
    SQLite.field)

data CountRow = CountRow Int

instance FromRow CountRow where
  fromRow = CountRow <$> SQLite.field

count :: Query Int
count =
  Query (\conn ->
    (Maybe.fromMaybe 0 . fmap (\(CountRow n) -> n) . Maybe.listToMaybe) <$>
      SQLite.query_ conn "SELECT COUNT(*) FROM income WHERE deleted_at IS NULL"
  )

list :: Int -> Int -> Query [Income]
list 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)
  )

listAll :: Query [Income]
listAll =
  Query (\conn ->
    map (\(Row i) -> i) <$>
      SQLite.query_ conn "SELECT * FROM income WHERE deleted_at IS NULL"
  )

listModifiedSince :: UTCTime -> Query [Income]
listModifiedSince since =
  Query (\conn ->
    map (\(Row i) -> i) <$>
      SQLite.query
        conn
        (SQLite.Query . T.intercalate " " $
          [ "SELECT *"
          , "FROM income"
          , "WHERE"
          ,   "created_at >= ?"
          ,   "OR edited_at >= ?"
          ,   "OR deleted_at >= ?"
          ])
        (Only since)
  )

create :: UserId -> Day -> Int -> Query Income
create userId date amount =
  Query (\conn -> do
    createdAt <- getCurrentTime
    SQLite.execute
      conn
      "INSERT INTO income (user_id, date, amount, created_at) VALUES (?, ?, ?, ?)"
      (userId, date, amount, createdAt)
    incomeId <- SQLite.lastInsertRowId conn
    return $ Income
      { _income_id        = incomeId
      , _income_userId    = userId
      , _income_date      = date
      , _income_amount    = amount
      , _income_createdAt = createdAt
      , _income_editedAt  = Nothing
      , _income_deletedAt = Nothing
      }
  )

edit :: UserId -> IncomeId -> Day -> Int -> Query (Maybe Income)
edit userId incomeId incomeDate incomeAmount =
  Query (\conn -> do
    mbIncome <- fmap (\(Row i) -> i) . Maybe.listToMaybe <$>
      SQLite.query conn "SELECT * FROM income WHERE id = ?" (Only incomeId)
    case mbIncome of
      Just income ->
        do
          currentTime <- getCurrentTime
          SQLite.execute
            conn
            "UPDATE income SET edited_at = ?, date = ?, amount = ? WHERE id = ? AND user_id = ?"
            (currentTime, incomeDate, incomeAmount, incomeId, userId)
          return . Just $ Income
            { _income_id        = incomeId
            , _income_userId    = userId
            , _income_date      = incomeDate
            , _income_amount    = incomeAmount
            , _income_createdAt = _income_createdAt income
            , _income_editedAt  = Just currentTime
            , _income_deletedAt = Nothing
            }
      Nothing ->
        return Nothing
  )

delete :: UserId -> PaymentId -> Query ()
delete userId paymentId =
  Query (\conn ->
    SQLite.execute
      conn
      "UPDATE income SET deleted_at = datetime('now') WHERE id = ? AND user_id = ?"
      (paymentId, userId)
  )

data UserDayRow = UserDayRow (UserId, Day)

instance FromRow UserDayRow where
  fromRow = do
    user <- SQLite.field
    day <- SQLite.field
    return $ UserDayRow (user, day)

definedForAll :: [UserId] -> Query (Maybe Day)
definedForAll users =
  Query (\conn ->
    (fromRows . fmap (\(UserDayRow (user, day)) -> (user, day))) <$>
      SQLite.query_
        conn
        "SELECT user_id, MIN(date) FROM income WHERE deleted_at IS NULL GROUP BY user_id;"
  )
  where
    fromRows rows =
      if L.sort users == L.sort (map fst rows) then
        Maybe.listToMaybe . L.sort . map snd $ rows
      else
        Nothing