From 99c4d5de4e55c628b2e20483e9d4f440cdc5d9f2 Mon Sep 17 00:00:00 2001 From: Fane Bastin Date: Mon, 28 Mar 2022 13:30:28 +1100 Subject: [PATCH] Finish off Level04 --- src/Level04/Core.hs | 25 ++++++++++++++++++------- src/Level04/Types.hs | 4 +++- 2 files changed, 21 insertions(+), 8 deletions(-) diff --git a/src/Level04/Core.hs b/src/Level04/Core.hs index 5e61601..913336c 100644 --- a/src/Level04/Core.hs +++ b/src/Level04/Core.hs @@ -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, @@ -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 -> diff --git a/src/Level04/Types.hs b/src/Level04/Types.hs index 0d19efe..0af1cea 100644 --- a/src/Level04/Types.hs +++ b/src/Level04/Types.hs @@ -14,6 +14,8 @@ module Level04.Types , getCommentText , renderContentType , fromDBComment + , encodeComment + , encodeTopic ) where import GHC.Generics (Generic) @@ -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 (..))