mateamt/src/Model/User.hs

200 lines
4.5 KiB
Haskell
Raw Normal View History

2019-04-15 20:23:25 +00:00
{-# LANGUAGE OverloadedStrings #-}
module Model.User where
import qualified Data.Text as T
2019-04-15 20:23:25 +00:00
import Data.Time.Calendar
import Data.Time.Clock
2019-04-21 15:27:15 +00:00
import Data.Profunctor.Product (p6)
2019-04-15 20:23:25 +00:00
2019-04-17 14:14:44 +00:00
import Data.Int (Int64)
2019-04-15 20:23:25 +00:00
import qualified Database.PostgreSQL.Simple as PGS
import Control.Arrow ((<<<))
2019-05-09 14:53:19 +00:00
import Control.Monad.IO.Class (liftIO)
2019-04-15 20:23:25 +00:00
import Opaleye as O
import qualified Opaleye.Constant as C
-- internal imports
2019-04-17 14:14:44 +00:00
import Types.User
2019-04-17 05:51:15 +00:00
import Types.Refine
2019-05-09 14:53:19 +00:00
import Types.Reader
import Classes
2019-04-17 05:51:15 +00:00
2019-04-16 11:03:07 +00:00
initUser :: PGS.Query
2019-07-21 14:05:05 +00:00
initUser = mconcat
2019-08-10 09:43:33 +00:00
[ "CREATE TABLE IF NOT EXISTS \"user\" ("
, "user_id SERIAL PRIMARY KEY,"
, "user_ident TEXT NOT NULL,"
, "user_balance INTEGER NOT NULL,"
, "user_timestamp DATE NOT NULL,"
, "user_email TEXT,"
, "user_avatar INTEGER REFERENCES \"avatar\"(\"avatar_id\") ON DELETE CASCADE"
2019-07-21 13:02:59 +00:00
, ")"
]
2019-04-16 11:03:07 +00:00
2019-04-15 20:23:25 +00:00
userTable :: Table
( Maybe (Field SqlInt4)
, Field SqlText
, Field SqlInt4
, Field SqlDate
, FieldNullable SqlText
, FieldNullable SqlInt4
)
( Field SqlInt4
, Field SqlText
, Field SqlInt4
, Field SqlDate
, FieldNullable SqlText
, FieldNullable SqlInt4
)
userTable = table "user" (
p6
2019-04-21 15:27:15 +00:00
( tableField "user_id"
, tableField "user_ident"
, tableField "user_balance"
, tableField "user_timestamp"
, tableField "user_email"
, tableField "user_avatar"
2019-04-15 20:23:25 +00:00
)
)
userSelect
:: UserRefine
2019-09-06 19:44:47 +00:00
-> PGS.Connection
2019-09-09 10:54:56 +00:00
-> MateHandler [UserSummary]
2019-09-06 19:44:47 +00:00
userSelect ref conn = do
2019-10-14 21:34:45 +00:00
today <- utctDay <$> liftIO getCurrentTime
users <- liftIO $ map fromDatabase <$> runSelect conn (
2019-10-31 23:00:41 +00:00
orderBy (asc (\(_, ident, _, _, _, _) -> ident)) (
keepWhen (\(_, _, _, ts, _, _) -> case ref of
AllUsers ->
C.constant True
ActiveUsers ->
ts .>= C.constant (addDays (-30) today)
OldUsers ->
ts .< C.constant (addDays (-30) today)
) <<< selectTable userTable)
) :: MateHandler [User]
2019-04-17 05:51:15 +00:00
mapM
(\(User i1 i2 _ _ _ i6) -> return $
2019-09-09 10:54:56 +00:00
UserSummary i1 i2 i6
2019-04-15 20:23:25 +00:00
)
2019-04-17 05:51:15 +00:00
users
2019-04-15 20:23:25 +00:00
2019-05-16 16:37:04 +00:00
userDetailsSelect
2019-09-06 19:44:47 +00:00
:: Int
-> PGS.Connection
2019-05-16 16:37:04 +00:00
-> MateHandler UserDetails
2019-09-07 00:48:16 +00:00
userDetailsSelect uid conn = do
2021-06-10 15:54:57 +00:00
users <- liftIO $ map fromDatabase <$> runSelect conn (limit 1 $
keepWhen (\(uuid, _, _, _, _, _) ->
2019-09-07 00:48:16 +00:00
uuid .== C.constant uid
2019-05-16 16:37:04 +00:00
) <<< queryTable userTable
) :: MateHandler [User]
2019-05-16 16:37:04 +00:00
head <$> mapM
(\(User i1 i2 i3 _ i5 i6) -> return $
UserDetails i1 i2 i3 i5 i6
2019-05-16 16:37:04 +00:00
)
users
2019-07-20 16:36:47 +00:00
userBalanceSelect
:: PGS.Connection
-> Int
-> MateHandler Int
2019-09-07 00:48:16 +00:00
userBalanceSelect conn uid = do
liftIO $ userBalance . fromDatabase . head <$> runSelect conn (
keepWhen (\(uuid, _, _, _, _, _) ->
2019-09-07 00:48:16 +00:00
uuid .== C.constant uid
2019-07-20 16:36:47 +00:00
) <<< queryTable userTable
)
2019-07-20 16:36:47 +00:00
2019-07-28 09:55:22 +00:00
insertUser
:: T.Text
-> Maybe T.Text
2019-07-28 09:55:22 +00:00
-> Day
-> PGS.Connection
-> MateHandler Int
insertUser ident email now conn = fmap head $ liftIO $ runInsert_ conn $ Insert
2019-04-15 20:23:25 +00:00
{ iTable = userTable
, iRows =
[
( C.constant (Nothing :: Maybe Int)
, C.constant ident
2019-04-15 20:23:25 +00:00
, C.constant (0 :: Int)
, C.constant now
, C.constant email
2019-04-15 20:23:25 +00:00
, C.constant (Nothing :: Maybe Int)
)
]
, iReturning = rReturning (\(uid, _, _, _, _, _) -> uid)
2019-04-15 20:23:25 +00:00
, iOnConflict = Nothing
2019-04-16 04:23:08 +00:00
}
2019-07-28 09:55:22 +00:00
updateUserDetails
:: Int
-> UserDetailsSubmit
-> Day
-> PGS.Connection
-> MateHandler Int64
updateUserDetails uid uds now conn = liftIO $ runUpdate_ conn $ Update
2019-07-20 16:36:47 +00:00
{ uTable = userTable
, uUpdateWith = updateEasy (\(id_, _, i3, _, _, _) ->
2019-04-16 04:23:08 +00:00
( id_
2019-05-16 16:37:04 +00:00
, C.constant (userDetailsSubmitIdent uds)
, i3
2019-05-09 14:53:19 +00:00
, C.constant now
2019-05-16 16:37:04 +00:00
, C.constant (userDetailsSubmitEmail uds)
, C.constant (userDetailsSubmitAvatar uds)
2019-04-16 04:23:08 +00:00
)
)
2019-10-14 20:50:42 +00:00
, uWhere = \(i1, _, _, _, _, _) -> i1 .== C.constant uid
2019-07-20 16:36:47 +00:00
, uReturning = rCount
}
2019-12-26 08:26:32 +00:00
updateUserTimestamp
:: Int
-> Day
-> PGS.Connection
-> MateHandler Int64
updateUserTimestamp uid now conn = liftIO $ runUpdate_ conn $ Update
{ uTable = userTable
, uUpdateWith = updateEasy (\(id_, ident, balance, _, email, ava) ->
( id_
, ident
, balance
, C.constant now
, email
, ava
)
)
, uWhere = \(i1, _, _, _, _, _) -> i1 .== C.constant uid
, uReturning = rCount
}
2019-07-20 16:36:47 +00:00
addToUserBalance
:: Int
-> Int
2019-07-28 09:55:22 +00:00
-> PGS.Connection
-> MateHandler Int64
addToUserBalance uid amount conn = liftIO $ runUpdate_ conn $ Update
2019-07-20 16:36:47 +00:00
{ uTable = userTable
, uUpdateWith = updateEasy (\(id_, i2, i3, i4, i5, i6) ->
2019-07-20 16:36:47 +00:00
( id_
, i2
, i3 + C.constant amount
, i4
, i5
, i6
)
)
2019-10-14 20:50:42 +00:00
, uWhere = \(i1, _, _, _, _, _) -> i1 .== C.constant uid
2019-07-20 16:36:47 +00:00
, uReturning = rCount
2019-04-16 04:23:08 +00:00
}