module Handler.Course.Events.Schedule
  ( getCEvScheduleOptSetR , postCEvScheduleOptSetR
  , getCEvScheduleOptDelR , postCEvScheduleOptDelR
  ) where

import Import


getCEvScheduleOptSetR, postCEvScheduleOptSetR :: TermId -> SchoolId -> CourseShorthand -> CryptoUUIDCourseEvent -> Bool -> Handler Html
getCEvScheduleOptSetR = postCEvScheduleOptSetR
postCEvScheduleOptSetR tid ssh csh ceId opt = do
  uid <- requireAuthId
  eId <- decrypt ceId

  runDB $ void $ upsert (CourseEventScheduleOpt
      { courseEventScheduleOptCourseEvent =  eId
      , courseEventScheduleOptUser        =  uid
      , courseEventScheduleOptOpt         =  opt
      })
      [ CourseEventScheduleOptOpt         =. opt
      ]

  addMessageI Success $ bool MsgScheduleOptOutSuccess MsgScheduleOptInSuccess opt
  redirect $ CourseR tid ssh csh CShowR


getCEvScheduleOptDelR, postCEvScheduleOptDelR :: TermId -> SchoolId -> CourseShorthand -> CryptoUUIDCourseEvent -> Handler Html
getCEvScheduleOptDelR = postCEvScheduleOptDelR
postCEvScheduleOptDelR tid ssh csh ceId = do
  uid <- requireAuthId
  eId <- decrypt ceId

  runDB $ deleteBy (UniqueCourseEventScheduleOpt eId uid)

  addMessageI Success MsgScheduleOptDeleteSuccess
  redirect $ CourseR tid ssh csh CShowR
