2024-09-18 13:16:36 -05:00
|
|
|
module Handlers where
|
|
|
|
|
|
|
|
import Core
|
|
|
|
import Database
|
2024-10-13 16:49:57 -05:00
|
|
|
import Logger
|
2024-09-18 13:16:36 -05:00
|
|
|
|
|
|
|
import qualified Data.ByteString.Char8 as C
|
|
|
|
import Data.List
|
|
|
|
import qualified Data.Text as T
|
|
|
|
import Effectful
|
|
|
|
import Effectful.Error.Static (Error, throwError)
|
|
|
|
import Servant hiding ((:>), throwError)
|
|
|
|
import qualified Servant as S
|
|
|
|
|
2024-10-13 16:49:57 -05:00
|
|
|
type CRUD es = ( Database :> es, Logger :> es, Error ServerError :> es )
|
2024-09-18 13:16:36 -05:00
|
|
|
|
2024-10-13 16:49:57 -05:00
|
|
|
rootHandler :: (Logger :> es, Error ServerError :> es) => Eff es T.Text
|
|
|
|
rootHandler = (writeLog Info "Hit the root!") >>= \_ ->
|
|
|
|
return "Hello, World!"
|
2024-09-18 13:16:36 -05:00
|
|
|
|
2024-10-13 16:49:57 -05:00
|
|
|
userListHandler :: CRUD es => Eff es [User]
|
2024-10-14 07:37:26 -05:00
|
|
|
userListHandler = (writeLog Info "Selected all users!") >>= \_ ->
|
2024-10-13 16:49:57 -05:00
|
|
|
databaseRead_ "SELECT id, name FROM users"
|
|
|
|
|
|
|
|
userGetHandler :: CRUD es => UserId -> Eff es User
|
2024-10-13 12:31:45 -05:00
|
|
|
userGetHandler userId = databaseRead (queryUser userId) >>= \mUser ->
|
2024-09-18 13:16:36 -05:00
|
|
|
case mUser of
|
2024-10-13 12:00:45 -05:00
|
|
|
(a:_) -> pure a
|
2024-10-13 16:49:57 -05:00
|
|
|
[] -> pure (User (UserId 0) "No user found")
|
2024-09-18 13:16:36 -05:00
|
|
|
|
2024-10-13 16:49:57 -05:00
|
|
|
userPostHandler :: CRUD es => T.Text -> Eff es NoContent
|
2024-10-13 12:31:45 -05:00
|
|
|
userPostHandler name =
|
|
|
|
databaseWrite (writeUser name) >>= \_ -> return NoContent
|
2024-10-13 16:49:57 -05:00
|
|
|
|
|
|
|
userDeleteHandler :: CRUD es => UserId -> Eff es NoContent
|
|
|
|
userDeleteHandler userId =
|
|
|
|
databaseWrite (deleteUser userId) >>= \_ -> return NoContent
|