blob: f9958e10daf7222cd5e9b16dccaeb8b0f40fd4df (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
|
{-# LANGUAGE DeriveGeneric #-}
{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE TemplateHaskell #-}
module Model.Frequency
( Frequency(..)
) where
import Data.Aeson
import Database.SQLite.Simple (SQLData(SQLText))
import Database.SQLite.Simple.FromField (fieldData, FromField(fromField))
import Database.SQLite.Simple.Ok (Ok(Ok, Errors))
import Database.SQLite.Simple.ToField (ToField(toField))
import GHC.Generics
import qualified Data.Text as T
import Web.Scotty (parseParam, Parsable, readEither)
data Frequency =
Punctual
| Monthly
deriving (Eq, Show, Read, Generic)
instance Parsable Frequency where parseParam = readEither
instance FromJSON Frequency
instance ToJSON Frequency
instance FromField Frequency where
fromField field = case fieldData field of
SQLText text -> Ok (read (T.unpack text) :: Frequency)
_ -> Errors [error "SQLText field required for frequency"]
instance ToField Frequency where
toField frequency = SQLText . T.pack . show $ frequency
|