mirror of
https://code.sup39.dev/repos/Wqawg
synced 2025-01-28 20:47:51 +09:00
Dedicated NewTicket type for ticket creation
This commit is contained in:
parent
2eb89cf460
commit
1b7cee4b78
2 changed files with 39 additions and 33 deletions
|
@ -14,7 +14,8 @@
|
||||||
-}
|
-}
|
||||||
|
|
||||||
module Vervis.Form.Ticket
|
module Vervis.Form.Ticket
|
||||||
( newTicketForm
|
( NewTicket (..)
|
||||||
|
, newTicketForm
|
||||||
, editTicketForm
|
, editTicketForm
|
||||||
)
|
)
|
||||||
where
|
where
|
||||||
|
@ -23,6 +24,7 @@ import Prelude
|
||||||
|
|
||||||
import Control.Monad.IO.Class (liftIO)
|
import Control.Monad.IO.Class (liftIO)
|
||||||
import Control.Monad.Trans.Class (lift)
|
import Control.Monad.Trans.Class (lift)
|
||||||
|
import Data.Text (Text)
|
||||||
import Data.Time.Calendar (Day (..))
|
import Data.Time.Calendar (Day (..))
|
||||||
import Data.Time.Clock (getCurrentTime, UTCTime (..))
|
import Data.Time.Clock (getCurrentTime, UTCTime (..))
|
||||||
import Yesod.Form
|
import Yesod.Form
|
||||||
|
@ -31,8 +33,6 @@ import Vervis.Foundation (Form, Handler)
|
||||||
import Vervis.Model
|
import Vervis.Model
|
||||||
|
|
||||||
--TODO use custom fields to ensure uniqueness or other constraints?
|
--TODO use custom fields to ensure uniqueness or other constraints?
|
||||||
--TODO stuff like number and created - do I generate them here using monadic
|
|
||||||
-- form or do I rely on handler to provide? which approach is better?
|
|
||||||
|
|
||||||
defTime :: UTCTime
|
defTime :: UTCTime
|
||||||
defTime = UTCTime (ModifiedJulianDay 0) 0
|
defTime = UTCTime (ModifiedJulianDay 0) 0
|
||||||
|
@ -40,22 +40,20 @@ defTime = UTCTime (ModifiedJulianDay 0) 0
|
||||||
now :: AForm Handler UTCTime
|
now :: AForm Handler UTCTime
|
||||||
now = lift $ liftIO getCurrentTime
|
now = lift $ liftIO getCurrentTime
|
||||||
|
|
||||||
newTicketAForm :: ProjectId -> Int -> PersonId -> AForm Handler Ticket
|
data NewTicket = NewTicket
|
||||||
newTicketAForm pid number author = Ticket
|
{ ntTitle :: Text
|
||||||
<$> pure pid
|
, ntDesc :: Text
|
||||||
<*> pure number
|
}
|
||||||
<*> now
|
|
||||||
<*> pure author
|
newTicketAForm :: AForm Handler NewTicket
|
||||||
<*> areq textField "Title*" Nothing
|
newTicketAForm = NewTicket
|
||||||
|
<$> areq textField "Title*" Nothing
|
||||||
<*> ( maybe "" unTextarea <$>
|
<*> ( maybe "" unTextarea <$>
|
||||||
aopt textareaField "Description (Markdown)" Nothing
|
aopt textareaField "Description (Markdown)" Nothing
|
||||||
)
|
)
|
||||||
<*> pure False
|
|
||||||
<*> pure defTime
|
|
||||||
<*> pure author
|
|
||||||
|
|
||||||
newTicketForm :: ProjectId -> Int -> PersonId -> Form Ticket
|
newTicketForm :: Form NewTicket
|
||||||
newTicketForm pid number author = renderDivs $ newTicketAForm pid number author
|
newTicketForm = renderDivs newTicketAForm
|
||||||
|
|
||||||
editTicketAForm :: Ticket -> PersonId -> AForm Handler Ticket
|
editTicketAForm :: Ticket -> PersonId -> AForm Handler Ticket
|
||||||
editTicketAForm ticket pid = fmap fixDone $ Ticket
|
editTicketAForm ticket pid = fmap fixDone $ Ticket
|
||||||
|
|
|
@ -27,8 +27,11 @@ where
|
||||||
|
|
||||||
import Prelude
|
import Prelude
|
||||||
|
|
||||||
|
import Control.Monad.IO.Class (liftIO)
|
||||||
import Data.Maybe (fromMaybe)
|
import Data.Maybe (fromMaybe)
|
||||||
import Data.Text (Text)
|
import Data.Text (Text)
|
||||||
|
import Data.Time.Calendar (Day (..))
|
||||||
|
import Data.Time.Clock (UTCTime (..), getCurrentTime)
|
||||||
import Data.Time.Format (formatTime, defaultTimeLocale)
|
import Data.Time.Format (formatTime, defaultTimeLocale)
|
||||||
import Database.Esqueleto hiding ((==.), (+=.), update)
|
import Database.Esqueleto hiding ((==.), (+=.), update)
|
||||||
import Database.Persist
|
import Database.Persist
|
||||||
|
@ -71,19 +74,31 @@ getTicketsR shar proj = do
|
||||||
|
|
||||||
postTicketsR :: Text -> Text -> Handler Html
|
postTicketsR :: Text -> Text -> Handler Html
|
||||||
postTicketsR shar proj = do
|
postTicketsR shar proj = do
|
||||||
Entity pid project <- runDB $ do
|
((result, widget), enctype) <- runFormPost newTicketForm {-pid next author-}
|
||||||
|
case result of
|
||||||
|
FormSuccess nt -> do
|
||||||
|
author <- requireAuthId
|
||||||
|
now <- liftIO getCurrentTime
|
||||||
|
tnum <- runDB $ do
|
||||||
|
Entity pid project <- do
|
||||||
Entity sid _sharer <- getBy404 $ UniqueSharerIdent shar
|
Entity sid _sharer <- getBy404 $ UniqueSharerIdent shar
|
||||||
getBy404 $ UniqueProject proj sid
|
getBy404 $ UniqueProject proj sid
|
||||||
let next = projectNextTicket project
|
let ticket = Ticket
|
||||||
author <- requireAuthId
|
{ ticketProject = pid
|
||||||
((result, widget), enctype) <- runFormPost $ newTicketForm pid next author
|
, ticketNumber = projectNextTicket project
|
||||||
case result of
|
, ticketCreated = now
|
||||||
FormSuccess ticket -> do
|
, ticketCreator = author
|
||||||
runDB $ do
|
, ticketTitle = ntTitle nt
|
||||||
|
, ticketDesc = ntDesc nt
|
||||||
|
, ticketDone = False
|
||||||
|
, ticketClosed = UTCTime (ModifiedJulianDay 0) 0
|
||||||
|
, ticketCloser = author
|
||||||
|
}
|
||||||
update pid [ProjectNextTicket +=. 1]
|
update pid [ProjectNextTicket +=. 1]
|
||||||
insert_ ticket
|
insert_ ticket
|
||||||
|
return $ ticketNumber ticket
|
||||||
setMessage "Ticket created."
|
setMessage "Ticket created."
|
||||||
redirect $ TicketR shar proj (ticketNumber ticket)
|
redirect $ TicketR shar proj tnum
|
||||||
FormMissing -> do
|
FormMissing -> do
|
||||||
setMessage "Field(s) missing."
|
setMessage "Field(s) missing."
|
||||||
defaultLayout $(widgetFile "ticket/new")
|
defaultLayout $(widgetFile "ticket/new")
|
||||||
|
@ -93,15 +108,8 @@ postTicketsR shar proj = do
|
||||||
|
|
||||||
getTicketNewR :: Text -> Text -> Handler Html
|
getTicketNewR :: Text -> Text -> Handler Html
|
||||||
getTicketNewR shar proj = do
|
getTicketNewR shar proj = do
|
||||||
Entity pid project <- runDB $ do
|
((_result, widget), enctype) <- runFormPost newTicketForm
|
||||||
Entity sid _sharer <- getBy404 $ UniqueSharerIdent shar
|
defaultLayout $(widgetFile "ticket/new")
|
||||||
getBy404 $ UniqueProject proj sid
|
|
||||||
let next = projectNextTicket project
|
|
||||||
author <- requireAuthId
|
|
||||||
((_result, widget), enctype) <- runFormPost $ newTicketForm pid next author
|
|
||||||
defaultLayout $ do
|
|
||||||
setTitle $ toHtml $ T.intercalate " :: " [shar, proj, "New ticket"]
|
|
||||||
$(widgetFile "ticket/new")
|
|
||||||
|
|
||||||
getTicketR :: Text -> Text -> Int -> Handler Html
|
getTicketR :: Text -> Text -> Int -> Handler Html
|
||||||
getTicketR shar proj num = do
|
getTicketR shar proj num = do
|
||||||
|
|
Loading…
Add table
Reference in a new issue