Fix database representation of term for correct sorting
This commit is contained in:
parent
b334df4939
commit
d33956dfaa
@ -95,7 +95,6 @@ main = db $ do
|
||||
, courseTerm = TermKey summer2018
|
||||
, courseSchool = ifi
|
||||
, courseCapacity = Just 20
|
||||
, courseHasRegistration = True
|
||||
, courseRegisterFrom = Just now
|
||||
, courseRegisterTo = Just ((3600 * 24 * 60) `addUTCTime` now )
|
||||
, courseDeregisterUntil = Nothing
|
||||
@ -122,7 +121,6 @@ main = db $ do
|
||||
, courseTerm = TermKey summer2017
|
||||
, courseSchool = ifi
|
||||
, courseCapacity = Just 20
|
||||
, courseHasRegistration = False
|
||||
, courseRegisterFrom = Nothing
|
||||
, courseRegisterTo = Nothing
|
||||
, courseDeregisterUntil = Nothing
|
||||
@ -141,7 +139,6 @@ main = db $ do
|
||||
, courseTerm = TermKey summer2018
|
||||
, courseSchool = ifi
|
||||
, courseCapacity = Just 20
|
||||
, courseHasRegistration = True
|
||||
, courseRegisterFrom = Just now
|
||||
, courseRegisterTo = Just ((3600 * 24 * 60) `addUTCTime` now )
|
||||
, courseDeregisterUntil = Nothing
|
||||
@ -160,7 +157,6 @@ main = db $ do
|
||||
, courseTerm = TermKey winter2017
|
||||
, courseSchool = ifi
|
||||
, courseCapacity = Just 30
|
||||
, courseHasRegistration = False
|
||||
, courseRegisterFrom = Nothing
|
||||
, courseRegisterTo = Nothing
|
||||
, courseDeregisterUntil = Nothing
|
||||
@ -179,7 +175,6 @@ main = db $ do
|
||||
, courseTerm = TermKey summer2017
|
||||
, courseSchool = ifi
|
||||
, courseCapacity = Just 50
|
||||
, courseHasRegistration = False
|
||||
, courseRegisterFrom = Nothing
|
||||
, courseRegisterTo = Nothing
|
||||
, courseDeregisterUntil = Nothing
|
||||
@ -198,7 +193,6 @@ main = db $ do
|
||||
, courseTerm = TermKey summer2018
|
||||
, courseSchool = ifi
|
||||
, courseCapacity = Just 50
|
||||
, courseHasRegistration = False
|
||||
, courseRegisterFrom = Nothing
|
||||
, courseRegisterTo = Nothing
|
||||
, courseDeregisterUntil = Nothing
|
||||
|
||||
@ -161,14 +161,30 @@ termFromText t
|
||||
, Right season <- seasonFromChar s
|
||||
= Right TermIdentifier{..}
|
||||
| otherwise = Left $ "Invalid TermIdentifier: “" <> t <> "”"
|
||||
|
||||
termToRational :: TermIdentifier -> Rational
|
||||
termToRational TermIdentifier{..} = fromInteger year + seasonOffset
|
||||
where
|
||||
seasonOffset
|
||||
| Summer <- season = 0
|
||||
| Winter <- season = 0.5
|
||||
|
||||
termFromRational :: Rational -> TermIdentifier
|
||||
termFromRational n = TermIdentifier{..}
|
||||
where
|
||||
year = floor n
|
||||
remainder = n - (fromInteger $ floor n)
|
||||
season
|
||||
| remainder == 0 = Summer
|
||||
| otherwise = Winter
|
||||
|
||||
instance PersistField TermIdentifier where
|
||||
toPersistValue = PersistText . termToText
|
||||
fromPersistValue (PersistText t) = termFromText t
|
||||
toPersistValue = PersistRational . termToRational
|
||||
fromPersistValue (PersistRational t) = Right $ termFromRational t
|
||||
fromPersistValue x = Left $ "Expected TermIdentifier, received: " <> tshow x
|
||||
|
||||
instance PersistFieldSql TermIdentifier where
|
||||
sqlType _ = SqlString
|
||||
sqlType _ = SqlNumeric 5 1
|
||||
|
||||
instance ToHttpApiData TermIdentifier where
|
||||
toUrlPiece = termToText
|
||||
|
||||
Loading…
Reference in New Issue
Block a user