gonito/Handler/Tags.hs

133 lines
4.2 KiB
Haskell
Raw Normal View History

2021-03-11 21:23:18 +01:00
{-# LANGUAGE OverloadedLists #-}
2017-02-19 14:05:56 +01:00
module Handler.Tags where
2021-03-11 21:23:18 +01:00
import Import hiding (fromList, get)
2017-02-26 21:40:38 +01:00
import Handler.Common (checkIfAdmin)
2017-02-19 14:05:56 +01:00
import Yesod.Form.Bootstrap3 (BootstrapFormLayout (..), renderBootstrap3, bfs)
import qualified Yesod.Table as Table
2017-05-28 10:06:50 +02:00
import Handler.TagUtils
2021-03-11 21:23:18 +01:00
import Data.Swagger.Declare
import Data.Swagger hiding (Tag, tags)
import Data.Proxy as DPR
import Control.Lens hiding ((.=))
import Data.HashMap.Strict.InsOrd (fromList)
instance ToJSON (Entity Tag) where
toJSON v = object
[ "name" .= (tagName $ entityVal v)
, "description" .= (tagDescription $ entityVal v)
, "color" .= (tagColor $ entityVal v)
2021-03-11 21:23:18 +01:00
, "id" .= (entityKey v)
]
instance ToSchema (Entity Tag) where
declareNamedSchema _ = do
stringSchema <- declareSchemaRef (DPR.Proxy :: DPR.Proxy String)
intSchema <- declareSchemaRef (DPR.Proxy :: DPR.Proxy Int)
return $ NamedSchema (Just "Tag") $ mempty
& type_ .~ SwaggerObject
& properties .~
fromList [ ("name", stringSchema)
, ("description", stringSchema)
, ("color", stringSchema)
2021-03-11 21:23:18 +01:00
, ("id", intSchema)
]
& required .~ [ "name", "description", "color", "id" ]
2021-03-11 21:23:18 +01:00
listTagsApi :: Swagger
listTagsApi = spec & definitions .~ defs
where
(defs, spec) = runDeclare declareListTagsSwagger mempty
declareListTagsSwagger :: Declare (Definitions Schema) Swagger
declareListTagsSwagger = do
listTagsResponse <- declareResponse (DPR.Proxy :: DPR.Proxy [Entity Tag])
return $ mempty
& paths .~
[ ("/api/list-tags", mempty & get ?~ (mempty
& produces ?~ MimeList ["application/json"]
& description ?~ "Returns the list of all tags"
& at 200 ?~ Inline listTagsResponse))
]
getListTagsJsonR :: Handler Value
getListTagsJsonR = do
allTags <- fetchAllTags
return $ toJSON allTags
2017-02-19 14:05:56 +01:00
getTagsR :: Handler Html
getTagsR = do
(formWidget, formEnctype) <- generateFormPost tagForm
2018-06-27 13:32:45 +02:00
doTags formWidget formEnctype
2017-02-19 14:05:56 +01:00
postTagsR :: Handler Html
postTagsR = do
((result, formWidget), formEnctype) <- runFormPost tagForm
2018-06-27 13:32:45 +02:00
canTagsBeAdded <- canAddTags
when canTagsBeAdded $ do
2017-02-19 14:05:56 +01:00
case result of
FormSuccess (t, d, c) -> do
_ <- runDB $ insert $ Tag t d c
2017-02-19 14:05:56 +01:00
return ()
_ -> do
return ()
2018-06-27 13:32:45 +02:00
doTags formWidget formEnctype
canAddTags :: Handler Bool
canAddTags = do
mUser <- maybeAuth
app <- getYesod
let tagPermissions = appTagPermissions $ appSettings app
case tagPermissions of
OnlyAdminCanAddNewTags -> return $ checkIfAdmin mUser
EverybodyCanAddNewTags -> return $ isJust mUser
2017-02-19 14:05:56 +01:00
2021-03-11 21:23:18 +01:00
fetchAllTags :: (PersistQueryRead (YesodPersistBackend site),
YesodPersist site,
BaseBackend (YesodPersistBackend site) ~ SqlBackend)
=> HandlerFor site [Entity Tag]
fetchAllTags = runDB $ selectList [] [Asc TagName]
2018-06-27 13:32:45 +02:00
doTags formWidget formEnctype = do
2021-03-11 21:23:18 +01:00
tags <- fetchAllTags
2018-06-27 13:32:45 +02:00
canTagsBeAdded <- canAddTags
2017-02-19 14:05:56 +01:00
defaultLayout $ do
setTitle "Tags"
$(widgetFile "tags")
tagsTable :: Table.Table App (Entity Tag)
tagsTable = mempty
++ Table.text "tag" (\(Entity _ tag) -> tagName tag)
++ Table.text "description" (\(Entity _ tag) -> (fromMaybe (""::Text) (tagDescription tag)))
++ Table.text "color" (\(Entity _ tag) -> (fromMaybe (""::Text) (tagColor tag)))
2017-02-19 14:05:56 +01:00
tagForm :: Form (Text, Maybe Text, Maybe Text)
tagForm = renderBootstrap3 BootstrapBasicForm $ (,,)
2017-02-19 14:05:56 +01:00
<$> areq textField (bfs MsgTagName) Nothing
<*> aopt textField (bfs MsgTagDescription) Nothing
<*> aopt textField (bfs MsgColor) Nothing
2017-05-28 10:06:50 +02:00
getToggleSubmissionTagR :: SubmissionTagId -> Handler RepPlain
getToggleSubmissionTagR submissionTagId = do
mUser <- maybeAuth
if (checkIfAdmin mUser)
then
do
submissionTag <- runDB $ get404 submissionTagId
let newState = toggleTag $ submissionTagAccepted submissionTag
runDB $ update submissionTagId [SubmissionTagAccepted =. newState]
return $ RepPlain $ toContent $ tagClass newState
else
do
return $ RepPlain $ toContent ("BLOCKED" :: Text)