aboutsummaryrefslogtreecommitdiff
path: root/src/server/Model/User.hs
blob: ab398227826b1789fdbf317bbe53ed80d20f348d (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
module Model.User
  ( list
  , getUser
  , getJson
  , findUser
  , createUser
  , deleteUser
  ) where

import Data.Text (Text)
import Data.Time.Clock (getCurrentTime)
import Data.List (find)

import Control.Monad.IO.Class (liftIO)

import Database.Persist

import Model.Database
import qualified Model.Json.User as Json

list :: Persist [Entity User]
list = selectList [] [Desc UserCreation]

getUser :: Text -> Persist (Maybe (Entity User))
getUser email = selectFirst [UserEmail ==. email] []

findUser :: UserId -> [Entity User] -> Maybe User
findUser i = fmap entityVal . find ((==) i . entityKey)

getJson :: Entity User -> Json.User
getJson userEntity =
  let user = entityVal userEntity
  in  Json.User (entityKey userEntity) (userName user) (userEmail user)

createUser :: Text -> Text -> Persist UserId
createUser email name = do
  now <- liftIO getCurrentTime
  insert $ User now email name

deleteUser :: Text -> Persist ()
deleteUser email =
  deleteWhere [UserEmail ==. email]