aboutsummaryrefslogtreecommitdiff
path: root/server/src/Model/Income.hs
blob: a6174bcdccedd6a599915585ff23e2137b0406da (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
{-# OPTIONS_GHC -fno-warn-orphans #-}

module Model.Income
  ( list
  , create
  , editOwn
  , deleteOwn
  , modifiedDuring
  ) where

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

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

import           Model.Query            (Query (Query))
import           Resource               (Resource, resourceCreatedAt,
                                         resourceDeletedAt, resourceEditedAt)

instance Resource Income where
  resourceCreatedAt = _income_createdAt
  resourceEditedAt = _income_editedAt
  resourceDeletedAt = _income_deletedAt

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

list :: Query [Income]
list = Query (\conn -> SQLite.query_ conn "SELECT * FROM income WHERE deleted_at IS NULL")

create :: UserId -> Day -> Int -> Query IncomeId
create incomeUserId incomeDate incomeAmount =
  Query (\conn -> do
    now <- getCurrentTime
    SQLite.execute
      conn
      "INSERT INTO income (user_id, date, amount, created_at) VALUES (?, ?, ?, ?)"
      (incomeUserId, incomeDate, incomeAmount, now)
    SQLite.lastInsertRowId conn
  )

editOwn :: UserId -> IncomeId -> Day -> Int -> Query Bool
editOwn incomeUserId incomeId incomeDate incomeAmount =
  Query (\conn -> do
    mbIncome <- listToMaybe <$> SQLite.query conn "SELECT * FROM income WHERE id = ?" (Only incomeId)
    case mbIncome of
      Just income ->
        if _income_userId income == incomeUserId
          then do
            now <- getCurrentTime
            SQLite.execute
              conn
              "UPDATE income SET edited_at = ?, date = ?, amount = ? WHERE id = ?"
              (now, incomeDate, incomeAmount, incomeId)
            return True
          else
            return False
      Nothing ->
        return False
  )

deleteOwn :: User -> IncomeId -> Query Bool
deleteOwn user incomeId =
  Query (\conn -> do
    mbIncome <- listToMaybe <$> SQLite.query conn "SELECT * FROM income WHERE id = ?" (Only incomeId)
    case mbIncome of
      Just income ->
        if _income_userId income == _user_id user
          then do
            now <- getCurrentTime
            SQLite.execute conn "UPDATE income SET deleted_at = ? WHERE id = ?" (now, incomeId)
            return True
          else
            return False
      Nothing ->
        return False
  )

modifiedDuring :: UTCTime -> UTCTime -> Query [Income]
modifiedDuring start end =
  Query (\conn ->
    SQLite.query
      conn
      "SELECT * FROM income WHERE (created_at >= ? AND created_at <= ?) OR (edited_at >= ? AND edited_at <= ?) OR (deleted_at >= ? AND deleted_at <= ?)"
      (start, end, start, end, start, end)
  )