Convert CourseListR to dbTable
This commit is contained in:
parent
25112a5f67
commit
1787dc1dcb
@ -179,3 +179,7 @@ FileCorrectedDeleted: Korrigiert (gelöscht)
|
|||||||
RatingUpdated: Korrektur gespeichert
|
RatingUpdated: Korrektur gespeichert
|
||||||
RatingDeleted: Korrektur zurückgesetzt
|
RatingDeleted: Korrektur zurückgesetzt
|
||||||
RatingFilesUpdated: Korrigierte Dateien überschrieben
|
RatingFilesUpdated: Korrigierte Dateien überschrieben
|
||||||
|
|
||||||
|
CourseMembers: Teilnehmer
|
||||||
|
CourseMembersCount num@Int64: #{display num}
|
||||||
|
CourseMembersCountLimited num@Int64 max@Int64: #{display num}/#{display max}
|
||||||
2
models
2
models
@ -60,7 +60,7 @@ Course
|
|||||||
shorthand Text
|
shorthand Text
|
||||||
term TermId
|
term TermId
|
||||||
school SchoolId
|
school SchoolId
|
||||||
capacity Int Maybe
|
capacity Int64 Maybe
|
||||||
-- canRegisterNow = maybe False (<= currentTime) registerFrom && maybe True (>= currentTime) registerTo
|
-- canRegisterNow = maybe False (<= currentTime) registerFrom && maybe True (>= currentTime) registerTo
|
||||||
registerFrom UTCTime Maybe
|
registerFrom UTCTime Maybe
|
||||||
registerTo UTCTime Maybe
|
registerTo UTCTime Maybe
|
||||||
|
|||||||
@ -7,6 +7,7 @@
|
|||||||
{-# LANGUAGE MultiParamTypeClasses #-}
|
{-# LANGUAGE MultiParamTypeClasses #-}
|
||||||
{-# LANGUAGE TypeFamilies #-}
|
{-# LANGUAGE TypeFamilies #-}
|
||||||
{-# LANGUAGE FlexibleContexts #-}
|
{-# LANGUAGE FlexibleContexts #-}
|
||||||
|
{-# LANGUAGE OverloadedLists #-}
|
||||||
|
|
||||||
module Handler.Course where
|
module Handler.Course where
|
||||||
|
|
||||||
@ -19,7 +20,9 @@ import Data.Function ((&))
|
|||||||
-- import Yesod.Form.Bootstrap3
|
-- import Yesod.Form.Bootstrap3
|
||||||
|
|
||||||
import Colonnade hiding (fromMaybe,bool)
|
import Colonnade hiding (fromMaybe,bool)
|
||||||
import Yesod.Colonnade
|
-- import Yesod.Colonnade
|
||||||
|
|
||||||
|
import qualified Database.Esqueleto as E
|
||||||
|
|
||||||
import qualified Data.UUID.Cryptographic as UUID
|
import qualified Data.UUID.Cryptographic as UUID
|
||||||
|
|
||||||
@ -37,45 +40,55 @@ getTermCurrentR = do
|
|||||||
|
|
||||||
|
|
||||||
getTermCourseListR :: TermId -> Handler Html
|
getTermCourseListR :: TermId -> Handler Html
|
||||||
getTermCourseListR tidini = do
|
getTermCourseListR tid = do
|
||||||
(term,courses) <- runDB $ (,)
|
void . runDB $ get404 tid -- Just ensure the term exists
|
||||||
<$> get tidini
|
|
||||||
<*> selectList [CourseTerm ==. tidini] [Asc CourseShorthand]
|
let
|
||||||
when (isNothing term) $ do
|
tableData :: E.SqlExpr (Entity Course) -> E.SqlQuery (E.SqlExpr (Entity Course), E.SqlExpr (E.Value Int64))
|
||||||
addMessage "warning" [shamlet| Semester #{toPathPiece tidini} nicht gefunden. |]
|
tableData course = do
|
||||||
redirect TermShowR
|
E.where_ $ course E.^. CourseTerm E.==. E.val tid
|
||||||
-- TODO: several runDBs per TableRow are probably too inefficient!
|
let
|
||||||
let colonnadeTerms = mconcat
|
participants = E.sub_select . E.from $ \courseParticipant -> do
|
||||||
[ headed "Kürzel" $ (\ckv ->
|
E.where_ $ courseParticipant E.^. CourseParticipantCourse E.==. course E.^. CourseId
|
||||||
let c = entityVal ckv
|
return (E.countRows :: E.SqlExpr (E.Value Int64))
|
||||||
shd = courseShorthand c
|
return (course, participants)
|
||||||
tid = courseTerm c
|
psValidator = def
|
||||||
in [whamlet| <a href=@{CourseR tid shd CShowR}>#{shd} |] )
|
& defaultSorting [("shorthand", SortAsc)]
|
||||||
-- , headed "Institut" $ [shamlet| #{course} |]
|
|
||||||
, headed "Beginn Anmeldung" $ fromString.(maybe "" formatTimeGerWD).courseRegisterFrom.entityVal
|
coursesTable <- dbTable psValidator $ DBTable
|
||||||
, headed "Ende Anmeldung" $ fromString.(maybe "" formatTimeGerWD).courseRegisterTo.entityVal
|
{ dbtSQLQuery = tableData
|
||||||
, headed "Teilnehmer" $ (\ckv -> do
|
, dbtColonnade = widgetColonnade $ mconcat
|
||||||
let cid = entityKey ckv
|
[ sortable (Just "shorthand") (textCell MsgCourse) $ anchorCell'
|
||||||
partiNum <- handlerToWidget $ runDB $ count [CourseParticipantCourse ==. cid]
|
(\(Entity _ Course{..}, _) -> CourseR courseTerm courseShorthand CShowR)
|
||||||
[whamlet| #{show partiNum} |]
|
(\(Entity _ Course{..}, _) -> toWidget courseShorthand)
|
||||||
)
|
, sortable (Just "register-from") (textCell MsgRegisterFrom) $ \(Entity _ Course{..}, _) -> textCell $ display courseRegisterFrom
|
||||||
, headed " " $ (\ckv ->
|
, sortable (Just "register-to") (textCell MsgRegisterTo) $ \(Entity _ Course{..}, _) -> textCell $ display courseRegisterTo
|
||||||
let c = entityVal ckv
|
, sortable (Just "members") (textCell MsgCourseMembers) $ \(Entity _ Course{..}, E.Value num) -> textCell $ case courseCapacity of
|
||||||
shd = courseShorthand c
|
Nothing -> MsgCourseMembersCount num
|
||||||
tid = courseTerm c
|
Just max -> MsgCourseMembersCountLimited num max
|
||||||
in do
|
|
||||||
adminLink <- handlerToWidget $ isAuthorized (CourseR tid shd CEditR) False
|
|
||||||
-- if (adminLink==Authorized) then linkButton "Ändern" BCWarning (CEditR tid shd) else ""
|
|
||||||
[whamlet|
|
|
||||||
$if adminLink == Authorized
|
|
||||||
<a href=@{CourseR tid shd CEditR}>
|
|
||||||
editieren
|
|
||||||
|]
|
|
||||||
)
|
|
||||||
]
|
]
|
||||||
let coursesTable = encodeWidgetTable tableSortable colonnadeTerms courses
|
, dbtSorting = [ ( "shorthand"
|
||||||
|
, SortColumn $ \course -> course E.^. CourseShorthand
|
||||||
|
)
|
||||||
|
, ( "register-from"
|
||||||
|
, SortColumn $ \course -> course E.^. CourseRegisterFrom
|
||||||
|
)
|
||||||
|
, ( "register-to"
|
||||||
|
, SortColumn $ \course -> course E.^. CourseRegisterTo
|
||||||
|
)
|
||||||
|
, ( "members"
|
||||||
|
, SortColumn $ \course -> E.sub_select . E.from $ \courseParticipant -> do
|
||||||
|
E.where_ $ courseParticipant E.^. CourseParticipantCourse E.==. course E.^. CourseId
|
||||||
|
return (E.countRows :: E.SqlExpr (E.Value Int64))
|
||||||
|
)
|
||||||
|
]
|
||||||
|
, dbtFilter = []
|
||||||
|
, dbtAttrs = tableDefault
|
||||||
|
, dbtIdent = "courses" :: Text
|
||||||
|
}
|
||||||
|
|
||||||
defaultLayout $ do
|
defaultLayout $ do
|
||||||
setTitleI . MsgTermCourseListTitle $ tidini
|
setTitleI . MsgTermCourseListTitle $ tid
|
||||||
$(widgetFile "courses")
|
$(widgetFile "courses")
|
||||||
|
|
||||||
getCShowR :: TermId -> Text -> Handler Html
|
getCShowR :: TermId -> Text -> Handler Html
|
||||||
@ -129,7 +142,7 @@ postCRegisterR tid csh = do
|
|||||||
actTime <- liftIO $ getCurrentTime
|
actTime <- liftIO $ getCurrentTime
|
||||||
regOk <- runDB $ do
|
regOk <- runDB $ do
|
||||||
reg <- count [CourseParticipantCourse ==. cid]
|
reg <- count [CourseParticipantCourse ==. cid]
|
||||||
if NTop (Just reg) < NTop (courseCapacity course)
|
if NTop (Just $ fromIntegral reg) < NTop (courseCapacity course)
|
||||||
then -- current capacity has room
|
then -- current capacity has room
|
||||||
insertUnique $ CourseParticipant cid aid actTime
|
insertUnique $ CourseParticipant cid aid actTime
|
||||||
else do -- no space left
|
else do -- no space left
|
||||||
@ -260,7 +273,7 @@ data CourseForm = CourseForm
|
|||||||
, cfShort :: Text
|
, cfShort :: Text
|
||||||
, cfTerm :: TermId
|
, cfTerm :: TermId
|
||||||
, cfSchool :: SchoolId
|
, cfSchool :: SchoolId
|
||||||
, cfCapacity :: Maybe Int
|
, cfCapacity :: Maybe Int64
|
||||||
, cfSecret :: Maybe Text
|
, cfSecret :: Maybe Text
|
||||||
, cfMatFree :: Bool
|
, cfMatFree :: Bool
|
||||||
, cfRegFrom :: Maybe UTCTime
|
, cfRegFrom :: Maybe UTCTime
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user