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!" userListHandler :: (Database :> es, Error ServerError :> es, FileSystem :> es) => Eff es [User] userListHandler = databaseRead_ "SELECT id, name FROM users" userGetHandler :: (Database :> es, Error ServerError :> es, FileSystem :> es) => Int -> Eff es User userGetHandler userId = databaseRead (queryUser userId) >>= \mUser -> case mUser of (a:_) -> pure a [] -> pure (User 0 "No user found") userPostHandler :: (Database :> es, Error ServerError :> es, FileSystem :> es) => String -> Eff es NoContent userPostHandler name = databaseWrite (writeUser name) >>= \_ -> return NoContent