2016-05-01 07:32:22 +09:00
|
|
|
{- This file is part of Vervis.
|
|
|
|
-
|
|
|
|
- Written in 2016 by fr33domlover <fr33domlover@riseup.net>.
|
|
|
|
-
|
|
|
|
- ♡ Copying is an act of love. Please copy, reuse and share.
|
|
|
|
-
|
|
|
|
- The author(s) have dedicated all copyright and related and neighboring
|
|
|
|
- rights to this software to the public domain worldwide. This software is
|
|
|
|
- distributed without any warranty.
|
|
|
|
-
|
|
|
|
- You should have received a copy of the CC0 Public Domain Dedication along
|
|
|
|
- with this software. If not, see
|
|
|
|
- <http://creativecommons.org/publicdomain/zero/1.0/>.
|
|
|
|
-}
|
|
|
|
|
|
|
|
module Vervis.Handler.Ticket
|
|
|
|
( getTicketsR
|
|
|
|
, postTicketsR
|
|
|
|
, getTicketNewR
|
2016-05-01 08:02:44 +09:00
|
|
|
, getTicketR
|
2016-05-02 15:13:57 +09:00
|
|
|
, putTicketR
|
|
|
|
, deleteTicketR
|
2016-05-02 20:34:11 +09:00
|
|
|
, postTicketR
|
2016-05-02 15:13:57 +09:00
|
|
|
, getTicketEditR
|
2016-05-01 07:32:22 +09:00
|
|
|
)
|
|
|
|
where
|
|
|
|
|
|
|
|
import Prelude
|
|
|
|
|
2016-05-01 18:58:55 +09:00
|
|
|
import Data.Maybe (fromMaybe)
|
2016-05-01 07:32:22 +09:00
|
|
|
import Data.Text (Text)
|
2016-05-01 18:58:55 +09:00
|
|
|
import Data.Time.Format (formatTime, defaultTimeLocale)
|
2016-05-01 19:15:38 +09:00
|
|
|
import Database.Esqueleto hiding ((==.), (+=.), update)
|
2016-05-01 08:02:44 +09:00
|
|
|
import Database.Persist
|
2016-05-01 07:32:22 +09:00
|
|
|
import Text.Blaze.Html (Html, toHtml)
|
2016-05-01 18:58:55 +09:00
|
|
|
import Yesod.Auth (requireAuthId)
|
2016-05-01 07:32:22 +09:00
|
|
|
import Yesod.Core (defaultLayout)
|
2016-05-02 20:34:11 +09:00
|
|
|
import Yesod.Core.Handler (setMessage, redirect, lookupPostParam, notFound)
|
2016-05-01 07:32:22 +09:00
|
|
|
import Yesod.Core.Widget (setTitle)
|
|
|
|
import Yesod.Form.Functions (runFormPost)
|
2016-05-01 19:15:38 +09:00
|
|
|
import Yesod.Form.Types (FormResult (..))
|
2016-05-01 18:58:55 +09:00
|
|
|
import Yesod.Persist.Core (runDB, get404, getBy404)
|
2016-05-01 07:32:22 +09:00
|
|
|
|
2016-05-03 06:20:25 +09:00
|
|
|
import qualified Data.Text as T (filter, intercalate, pack)
|
2016-05-01 18:58:55 +09:00
|
|
|
import qualified Database.Esqueleto as E ((==.))
|
2016-05-01 07:32:22 +09:00
|
|
|
|
|
|
|
import Vervis.Form.Ticket
|
|
|
|
import Vervis.Foundation
|
2016-05-03 06:20:25 +09:00
|
|
|
import Vervis.MediaType (MediaType (Markdown))
|
2016-05-01 07:32:22 +09:00
|
|
|
import Vervis.Model
|
2016-05-03 06:20:25 +09:00
|
|
|
import Vervis.Render (renderSourceT)
|
2016-05-01 07:32:22 +09:00
|
|
|
import Vervis.Settings (widgetFile)
|
|
|
|
|
|
|
|
getTicketsR :: Text -> Text -> Handler Html
|
2016-05-01 08:02:44 +09:00
|
|
|
getTicketsR shar proj = do
|
2016-05-01 18:58:55 +09:00
|
|
|
rows <- runDB $ select $ from $ \ (ticket, person, sharer) -> do
|
|
|
|
where_ $
|
|
|
|
ticket ^. TicketCreator E.==. person ^. PersonId &&.
|
|
|
|
person ^. PersonIdent E.==. sharer ^. SharerId
|
|
|
|
orderBy [asc $ ticket ^. TicketNumber]
|
|
|
|
return
|
|
|
|
( ticket ^. TicketNumber
|
|
|
|
, sharer ^. SharerIdent
|
|
|
|
, sharer ^. SharerName
|
|
|
|
, ticket ^. TicketTitle
|
|
|
|
, ticket ^. TicketDone
|
|
|
|
)
|
2016-05-01 08:02:44 +09:00
|
|
|
defaultLayout $ do
|
|
|
|
setTitle $ toHtml $ T.intercalate " :: " [shar, proj, "Tickes"]
|
|
|
|
$(widgetFile "ticket/list")
|
2016-05-01 07:32:22 +09:00
|
|
|
|
|
|
|
postTicketsR :: Text -> Text -> Handler Html
|
2016-05-01 19:15:38 +09:00
|
|
|
postTicketsR shar proj = do
|
|
|
|
Entity pid project <- runDB $ do
|
|
|
|
Entity sid _sharer <- getBy404 $ UniqueSharerIdent shar
|
|
|
|
getBy404 $ UniqueProject proj sid
|
|
|
|
let next = projectNextTicket project
|
|
|
|
author <- requireAuthId
|
|
|
|
((result, widget), enctype) <- runFormPost $ newTicketForm pid next author
|
|
|
|
case result of
|
|
|
|
FormSuccess ticket -> do
|
|
|
|
runDB $ do
|
|
|
|
update pid [ProjectNextTicket +=. 1]
|
|
|
|
insert_ ticket
|
|
|
|
setMessage "Ticket created."
|
2016-05-02 18:15:10 +09:00
|
|
|
redirect $ TicketR shar proj (ticketNumber ticket)
|
2016-05-01 19:15:38 +09:00
|
|
|
FormMissing -> do
|
|
|
|
setMessage "Field(s) missing."
|
|
|
|
defaultLayout $(widgetFile "ticket/new")
|
|
|
|
FormFailure _l -> do
|
|
|
|
setMessage "Ticket creation failed, see errors below."
|
|
|
|
defaultLayout $(widgetFile "ticket/new")
|
2016-05-01 07:32:22 +09:00
|
|
|
|
|
|
|
getTicketNewR :: Text -> Text -> Handler Html
|
|
|
|
getTicketNewR shar proj = do
|
|
|
|
Entity pid project <- runDB $ do
|
|
|
|
Entity sid _sharer <- getBy404 $ UniqueSharerIdent shar
|
|
|
|
getBy404 $ UniqueProject proj sid
|
|
|
|
let next = projectNextTicket project
|
2016-05-01 18:58:55 +09:00
|
|
|
author <- requireAuthId
|
|
|
|
((_result, widget), enctype) <- runFormPost $ newTicketForm pid next author
|
2016-05-01 07:32:22 +09:00
|
|
|
defaultLayout $ do
|
|
|
|
setTitle $ toHtml $ T.intercalate " :: " [shar, proj, "New ticket"]
|
|
|
|
$(widgetFile "ticket/new")
|
2016-05-01 08:02:44 +09:00
|
|
|
|
|
|
|
getTicketR :: Text -> Text -> Int -> Handler Html
|
2016-05-01 18:58:55 +09:00
|
|
|
getTicketR shar proj num = do
|
2016-05-02 18:15:10 +09:00
|
|
|
(author, closer, ticket) <- runDB $ do
|
2016-05-01 18:58:55 +09:00
|
|
|
Entity sid _sharer <- getBy404 $ UniqueSharerIdent shar
|
|
|
|
Entity pid _project <- getBy404 $ UniqueProject proj sid
|
|
|
|
Entity _tid ticket <- getBy404 $ UniqueTicket pid num
|
|
|
|
person <- get404 $ ticketCreator ticket
|
|
|
|
author <- get404 $ personIdent person
|
2016-05-02 18:15:10 +09:00
|
|
|
closer <-
|
|
|
|
if ticketDone ticket
|
|
|
|
then do
|
|
|
|
person' <- get404 $ ticketCloser ticket
|
|
|
|
get404 $ personIdent person'
|
|
|
|
else return author
|
|
|
|
return (author, closer, ticket)
|
2016-05-03 06:20:25 +09:00
|
|
|
let desc = renderSourceT Markdown $ T.filter (/= '\r') $ ticketDesc ticket
|
2016-05-01 18:58:55 +09:00
|
|
|
defaultLayout $ do
|
|
|
|
setTitle $ toHtml $ T.intercalate " :: "
|
|
|
|
[shar, proj, "Tickets", T.pack ('#' : show num)]
|
|
|
|
$(widgetFile "ticket/one")
|
2016-05-02 15:13:57 +09:00
|
|
|
|
|
|
|
putTicketR :: Text -> Text -> Int -> Handler Html
|
2016-05-02 18:15:10 +09:00
|
|
|
putTicketR shar proj num = do
|
|
|
|
Entity tid ticket <- runDB $ do
|
|
|
|
Entity sid _sharer <- getBy404 $ UniqueSharerIdent shar
|
|
|
|
Entity pid _project <- getBy404 $ UniqueProject proj sid
|
|
|
|
getBy404 $ UniqueTicket pid num
|
|
|
|
user <- requireAuthId
|
|
|
|
((result, widget), enctype) <- runFormPost $ editTicketForm ticket user
|
|
|
|
case result of
|
|
|
|
FormSuccess ticket' -> do
|
|
|
|
runDB $ replace tid ticket'
|
2016-05-02 20:33:30 +09:00
|
|
|
setMessage "Ticket updated."
|
2016-05-02 18:15:10 +09:00
|
|
|
redirect $ TicketR shar proj num
|
|
|
|
FormMissing -> do
|
|
|
|
setMessage "Field(s) missing."
|
|
|
|
defaultLayout $(widgetFile "ticket/edit")
|
|
|
|
FormFailure _l -> do
|
|
|
|
setMessage "Ticket update failed, see errors below."
|
|
|
|
defaultLayout $(widgetFile "ticket/edit")
|
2016-05-02 15:13:57 +09:00
|
|
|
|
|
|
|
deleteTicketR :: Text -> Text -> Int -> Handler Html
|
2016-05-02 18:15:10 +09:00
|
|
|
deleteTicketR shar proj num =
|
|
|
|
--TODO: I can easily implement this, but should it even be possible to
|
|
|
|
--delete tickets?
|
|
|
|
error "Not implemented"
|
2016-05-02 15:13:57 +09:00
|
|
|
|
2016-05-02 20:34:11 +09:00
|
|
|
postTicketR :: Text -> Text -> Int -> Handler Html
|
|
|
|
postTicketR shar proj num = do
|
|
|
|
mmethod <- lookupPostParam "_method"
|
|
|
|
case mmethod of
|
|
|
|
Just "PUT" -> putTicketR shar proj num
|
|
|
|
Just "DELETE" -> deleteTicketR shar proj num
|
|
|
|
_ -> notFound
|
|
|
|
|
2016-05-02 15:13:57 +09:00
|
|
|
getTicketEditR :: Text -> Text -> Int -> Handler Html
|
2016-05-02 18:15:10 +09:00
|
|
|
getTicketEditR shar proj num = do
|
|
|
|
Entity _tid ticket <- runDB $ do
|
|
|
|
Entity sid _sharer <- getBy404 $ UniqueSharerIdent shar
|
|
|
|
Entity pid _project <- getBy404 $ UniqueProject proj sid
|
|
|
|
getBy404 $ UniqueTicket pid num
|
|
|
|
user <- requireAuthId
|
|
|
|
((_result, widget), enctype) <- runFormPost $ editTicketForm ticket user
|
|
|
|
defaultLayout $ do
|
|
|
|
setTitle $ toHtml $ T.intercalate " :: "
|
|
|
|
[shar, proj, "Tickets", T.pack ('#' : show num), "Edit"]
|
|
|
|
$(widgetFile "ticket/edit")
|