module Handler.Tutorial.Schedule
  ( getTScheduleOptSetR, postTScheduleOptSetR
  , getTScheduleOptDelR, postTScheduleOptDelR
  ) where

import Import

import Handler.Utils.Tutorial


getTScheduleOptSetR, postTScheduleOptSetR :: TermId -> SchoolId -> CourseShorthand -> TutorialName -> Bool -> Handler Html
getTScheduleOptSetR = postTScheduleOptSetR
postTScheduleOptSetR tid ssh csh tutn opt = do
  uid <- requireAuthId
  
  runDB $ do
    tutid <- fmap entityKey $ fetchTutorial tid ssh csh tutn
    void $ upsert TutorialScheduleOpt
      { tutorialScheduleOptTutorial =  tutid
      , tutorialScheduleOptUser     =  uid
      , tutorialScheduleOptOpt      =  opt
      }
      [ TutorialScheduleOptOpt      =. opt
      ]

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


getTScheduleOptDelR, postTScheduleOptDelR :: TermId -> SchoolId -> CourseShorthand -> TutorialName -> Handler Html
getTScheduleOptDelR = postTScheduleOptDelR
postTScheduleOptDelR tid ssh csh tutn = do
  uid <- requireAuthId

  runDB $ do
    tutid <- fmap entityKey $ fetchTutorial tid ssh csh tutn
    deleteBy $ UniqueTutorialScheduleOpt tutid uid

  addMessageI Success MsgScheduleOptDeleteSuccess
  redirect $ CourseR tid ssh csh CShowR
