Skip to content

Commit

Permalink
Finish off Level04
Browse files Browse the repository at this point in the history
  • Loading branch information
FaneBastinBellroy committed Mar 28, 2022
1 parent 782e126 commit 99c4d5d
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 8 deletions.
25 changes: 18 additions & 7 deletions src/Level04/Core.hs
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,16 @@ import Data.Text.Encoding (decodeUtf8)
import Data.Text.Lazy.Encoding (encodeUtf8)
import Database.SQLite.SimpleErrors.Types (SQLiteResponse)
import Level04.Conf (Conf (..), firstAppConfig)
import Level04.DB (initDB)
import Level04.DB
import qualified Level04.DB as DB
import Level04.Types
( ContentType (JSON, PlainText),
( Comment (..),
ContentType (JSON, PlainText),
Error (..),
RqType (AddRq, ListRq, ViewRq),
Topic (..),
encodeComment,
encodeTopic,
mkCommentText,
mkTopic,
renderContentType,
Expand Down Expand Up @@ -152,16 +156,23 @@ app db rq cb = do
--
-- For both the 'ViewRq' and 'ListRq' functions, we'll need to pass the correct 'Encoder' to the
-- 'resp200Json' function.
toLBS :: Show a => a -> LBS.ByteString
toLBS = LBS.pack . show

handleRequest ::
DB.FirstAppDB ->
RqType ->
IO (Either Error Response)
handleRequest _db (AddRq _ _) =
(resp200 PlainText "Success" <$) <$> error "AddRq handler not implemented"
handleRequest _db (ViewRq _) =
error "ViewRq handler not implemented"
-- addCommentToTopic
handleRequest _db (AddRq tp ct) =
(resp200 PlainText "Comment added successfully!" <$)
<$> addCommentToTopic _db tp ct
-- getComments
handleRequest _db (ViewRq tp) =
(resp200Json (E.list encodeComment) <$>) <$> getComments _db tp
-- getTopics
handleRequest _db ListRq =
error "ListRq handler not implemented"
(resp200Json (E.list encodeTopic) <$>) <$> getTopics _db

mkRequest ::
Request ->
Expand Down
4 changes: 3 additions & 1 deletion src/Level04/Types.hs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ module Level04.Types
, getCommentText
, renderContentType
, fromDBComment
, encodeComment
, encodeTopic
) where

import GHC.Generics (Generic)
Expand Down Expand Up @@ -41,7 +43,7 @@ import Level04.DB.Types (DBComment (..))
-- just spin up another module.
import Level04.Types.CommentText (CommentText (..), getCommentText,
mkCommentText, commentToText)
import Level04.Types.Topic (Topic (..), getTopic, mkTopic)
import Level04.Types.Topic (Topic (..), getTopic, mkTopic, encodeTopic)

import Level04.Types.Error (Error (..))

Expand Down

0 comments on commit 99c4d5d

Please sign in to comment.