HELPS/src/Handlers.hs

33 lines
1.1 KiB
Haskell
Raw Normal View History

2024-09-18 13:16:36 -05:00
module Handlers where
import Core
import Database
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 Effectful.FileSystem
import Effectful.FileSystem.IO.ByteString as EBS
import Servant hiding ((:>), throwError)
import qualified Servant as S
rootHandler :: (Error ServerError :> es) => Eff es T.Text
rootHandler = return "Hello, World!"
2024-10-13 12:31:45 -05:00
userListHandler :: ( Database :> es, Error ServerError :> es) => Eff es [User]
userListHandler = databaseRead_ "SELECT id, name FROM users"
2024-09-18 13:16:36 -05:00
2024-10-13 12:31:45 -05:00
userGetHandler :: (Database :> es, Error ServerError :> es)
=> Int -> Eff es User
userGetHandler userId = databaseRead (queryUser userId) >>= \mUser ->
2024-09-18 13:16:36 -05:00
case mUser of
(a:_) -> pure a
[] -> pure (User 0 "No user found")
2024-09-18 13:16:36 -05:00
2024-10-13 12:31:45 -05:00
userPostHandler :: ( Database :> es, Error ServerError :> es)
=> String -> Eff es NoContent
userPostHandler name =
databaseWrite (writeUser name) >>= \_ -> return NoContent