refactor(favourites): introduce FavouriteReason
This commit is contained in:
parent
a79e63a963
commit
9d2995b6a5
@ -35,12 +35,6 @@ CourseEdit -- who edited when a row in table "Course", kept indef
|
|||||||
user UserId
|
user UserId
|
||||||
time UTCTime
|
time UTCTime
|
||||||
course CourseId
|
course CourseId
|
||||||
CourseFavourite -- which user accessed which course when, only displayed to user for convenience;
|
|
||||||
user UserId -- max number of rows kept per user is user-defined by column 'maxFavourites' in table "User"
|
|
||||||
time UTCTime -- oldest is removed first
|
|
||||||
course CourseId
|
|
||||||
UniqueCourseFavourite user course
|
|
||||||
deriving Show
|
|
||||||
Lecturer -- course ownership
|
Lecturer -- course ownership
|
||||||
user UserId
|
user UserId
|
||||||
course CourseId
|
course CourseId
|
||||||
|
|||||||
10
models/courses/favourite.model
Normal file
10
models/courses/favourite.model
Normal file
@ -0,0 +1,10 @@
|
|||||||
|
CourseFavourite -- which user accessed which course when, only displayed to user for convenience;
|
||||||
|
user UserId
|
||||||
|
course CourseId
|
||||||
|
reason FavouriteReason
|
||||||
|
lastVisit UTCTime
|
||||||
|
UniqueCourseFavourite user course
|
||||||
|
CourseNoFavourite
|
||||||
|
user UserId
|
||||||
|
course CourseId
|
||||||
|
UniqueCourseNoFavourite user course
|
||||||
1
routes
1
routes
@ -104,6 +104,7 @@
|
|||||||
!/course/new CourseNewR GET POST !lecturer
|
!/course/new CourseNewR GET POST !lecturer
|
||||||
/course/#TermId/#SchoolId/#CourseShorthand CourseR !lecturer:
|
/course/#TermId/#SchoolId/#CourseShorthand CourseR !lecturer:
|
||||||
/ CShowR GET !free
|
/ CShowR GET !free
|
||||||
|
/favourite CFavouriteR POST
|
||||||
/register CRegisterR GET POST !timeANDcapacityANDallocation-timeAND¬exam-result !lecturerANDallocation-time
|
/register CRegisterR GET POST !timeANDcapacityANDallocation-timeAND¬exam-result !lecturerANDallocation-time
|
||||||
/register-template CRegisterTemplateR GET !free
|
/register-template CRegisterTemplateR GET !free
|
||||||
/edit CEditR GET POST
|
/edit CEditR GET POST
|
||||||
|
|||||||
@ -1522,24 +1522,22 @@ instance Yesod UniWorX where
|
|||||||
now <- liftIO $ getCurrentTime
|
now <- liftIO $ getCurrentTime
|
||||||
uid <- MaybeT $ liftHandler maybeAuthId
|
uid <- MaybeT $ liftHandler maybeAuthId
|
||||||
cid <- MaybeT . getKeyBy $ TermSchoolCourseShort tid ssh csh
|
cid <- MaybeT . getKeyBy $ TermSchoolCourseShort tid ssh csh
|
||||||
user <- MaybeT $ get uid
|
User{userMaxFavourites} <- MaybeT $ get uid
|
||||||
let courseFavourite = CourseFavourite uid now cid
|
|
||||||
|
|
||||||
$logDebugS "updateFavourites" [st|Updating/Inserting: #{tshow courseFavourite}|]
|
|
||||||
-- update Favourites
|
-- update Favourites
|
||||||
void . lift $ upsertBy
|
void . lift $ upsertBy
|
||||||
(UniqueCourseFavourite uid cid)
|
(UniqueCourseFavourite uid cid)
|
||||||
courseFavourite
|
(CourseFavourite uid cid FavouriteVisited now)
|
||||||
[CourseFavouriteTime =. now]
|
[CourseFavouriteLastVisit =. now]
|
||||||
-- prune Favourites to user-defined size
|
-- prune Favourites to user-defined size
|
||||||
oldFavs <- lift $ selectKeysList
|
oldFavs <- lift $ selectList [CourseFavouriteUser ==. uid] []
|
||||||
[ CourseFavouriteUser ==. uid]
|
let deleteFavs = oldFavs
|
||||||
[ Desc CourseFavouriteTime
|
& sortOn ((courseFavouriteReason &&& Down . courseFavouriteLastVisit) . entityVal)
|
||||||
, OffsetBy $ userMaxFavourites user
|
& drop userMaxFavourites
|
||||||
]
|
& filter ((<= FavouriteVisited) . courseFavouriteReason . entityVal)
|
||||||
lift . forM_ oldFavs $ \fav -> do
|
& map entityKey
|
||||||
$logDebugS "updateFavourites" "Deleting old favourite."
|
unless (null deleteFavs) $
|
||||||
delete fav
|
lift $ deleteWhere [CourseFavouriteId <-. deleteFavs]
|
||||||
_other -> return ()
|
_other -> return ()
|
||||||
normalizeRouteMiddleware :: Handler a -> Handler a
|
normalizeRouteMiddleware :: Handler a -> Handler a
|
||||||
normalizeRouteMiddleware handler = (*> handler) . runMaybeT $ do
|
normalizeRouteMiddleware handler = (*> handler) . runMaybeT $ do
|
||||||
|
|||||||
@ -29,3 +29,6 @@ getCNotesR, postCNotesR :: TermId -> SchoolId -> CourseShorthand -> Handler Html
|
|||||||
-- If they are shared, adjust MsgCourseUserNoteTooltip
|
-- If they are shared, adjust MsgCourseUserNoteTooltip
|
||||||
getCNotesR = postCNotesR
|
getCNotesR = postCNotesR
|
||||||
postCNotesR _ _ _ = defaultLayout [whamlet|You have corrector access to this course.|]
|
postCNotesR _ _ _ = defaultLayout [whamlet|You have corrector access to this course.|]
|
||||||
|
|
||||||
|
postCFavouriteR :: TermId -> SchoolId -> CourseShorthand -> Handler ()
|
||||||
|
postCFavouriteR _ _ _ = error "not implemented"
|
||||||
|
|||||||
@ -262,13 +262,6 @@ postProfileR = do
|
|||||||
when (stgDisplayEmail /= userDisplayEmail) $ do
|
when (stgDisplayEmail /= userDisplayEmail) $ do
|
||||||
queueDBJob $ JobChangeUserDisplayEmail uid stgDisplayEmail
|
queueDBJob $ JobChangeUserDisplayEmail uid stgDisplayEmail
|
||||||
addMessageI Info $ MsgUserDisplayEmailChangeSent stgDisplayEmail
|
addMessageI Info $ MsgUserDisplayEmailChangeSent stgDisplayEmail
|
||||||
when (stgMaxFavourties < userMaxFavourites) $ do
|
|
||||||
-- prune Favourites to user-defined size
|
|
||||||
oldFavs <- selectKeysList [ CourseFavouriteUser ==. uid]
|
|
||||||
[ Desc CourseFavouriteTime
|
|
||||||
, OffsetBy stgMaxFavourties
|
|
||||||
]
|
|
||||||
mapM_ delete oldFavs
|
|
||||||
let
|
let
|
||||||
symDiff = (stgSchools `Set.difference` userSchools) `Set.union` (userSchools `Set.difference` stgSchools)
|
symDiff = (stgSchools `Set.difference` userSchools) `Set.union` (userSchools `Set.difference` stgSchools)
|
||||||
forM_ symDiff $ \ssh -> if
|
forM_ symDiff $ \ssh -> if
|
||||||
|
|||||||
@ -550,6 +550,13 @@ customMigrations = Map.fromListWith (>>)
|
|||||||
UPDATE "exam" SET "bonus_rule" = jsonb_insert("bonus_rule", '{round}' :: text[], '0.01' :: jsonb) WHERE "bonus_rule"->>'rule' = 'bonus-points';
|
UPDATE "exam" SET "bonus_rule" = jsonb_insert("bonus_rule", '{round}' :: text[], '0.01' :: jsonb) WHERE "bonus_rule"->>'rule' = 'bonus-points';
|
||||||
|]
|
|]
|
||||||
)
|
)
|
||||||
|
, ( AppliedMigrationKey [migrationVersion|23.0.0|] [version|24.0.0|]
|
||||||
|
, whenM (tableExists "course_favourite") $
|
||||||
|
[executeQQ|
|
||||||
|
ALTER TABLE "course_favourite" RENAME COLUMN "time" TO "last_visit";
|
||||||
|
ALTER TABLE "course_favourite" ADD COLUMN "reason" jsonb DEFAULT '"visited"'::jsonb;
|
||||||
|
|]
|
||||||
|
)
|
||||||
]
|
]
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@ -131,3 +131,17 @@ instance YesodMail site => ToMailPart site (CsvRendered, CsvOptions) where
|
|||||||
_partContent .= Csv.encodeByNameWith (encOpts ^. _CsvEncodeOptions) csvRenderedHeader csvRenderedData
|
_partContent .= Csv.encodeByNameWith (encOpts ^. _CsvEncodeOptions) csvRenderedHeader csvRenderedData
|
||||||
instance YesodMail site => ToMailPart site CsvRendered where
|
instance YesodMail site => ToMailPart site CsvRendered where
|
||||||
toMailPart = toMailPart . (, def :: CsvOptions)
|
toMailPart = toMailPart . (, def :: CsvOptions)
|
||||||
|
|
||||||
|
|
||||||
|
data FavouriteReason
|
||||||
|
= FavouriteVisited
|
||||||
|
| FavouriteParticipant
|
||||||
|
| FavouriteManual
|
||||||
|
| FavouriteCurrent
|
||||||
|
deriving (Eq, Ord, Read, Show, Enum, Bounded, Generic, Typeable)
|
||||||
|
instance Universe FavouriteReason
|
||||||
|
instance Finite FavouriteReason
|
||||||
|
deriveJSON defaultOptions
|
||||||
|
{ constructorTagModifier = camelToPathPiece' 1
|
||||||
|
} ''FavouriteReason
|
||||||
|
derivePersistFieldJSON ''FavouriteReason
|
||||||
|
|||||||
Reference in New Issue
Block a user