blob: 813a4b51c3055264525fba5f64f56a3b71963370 (
plain) (
blame)
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
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
|
{-# LANGUAGE EmptyDataDecls #-}
{-# LANGUAGE FlexibleContexts #-}
{-# LANGUAGE GADTs #-}
{-# LANGUAGE GeneralizedNewtypeDeriving #-}
{-# LANGUAGE MultiParamTypeClasses #-}
{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE QuasiQuotes #-}
{-# LANGUAGE TemplateHaskell #-}
{-# LANGUAGE TypeFamilies #-}
--------------------------------------------------------------------------------
module Types where
--------------------------------------------------------------------------------
import Data.Aeson
import Data.Text
import Database.Persist.TH
--------------------------------------------------------------------------------
share [mkPersist sqlSettings, mkMigrate "migrateAll"] [persistLowerCase|
Account
name Text
age Int
UniqueName name
deriving Eq Read Show
|]
instance FromJSON Account where
parseJSON = withObject "User" $ \ v ->
Account <$> v .: "name"
<*> v .: "age"
instance ToJSON Account where
toJSON (Account name age) =
object [ "name" .= name
, "age" .= age
]
newtype Username = Username Text
deriving (Eq, Show)
instance ToJSON Username where
toJSON (Username x) = toJSON x
newtype Password = Password Text
deriving (Eq, Show)
instance ToJSON Password where
toJSON (Password x) = toJSON x
data Role = RegularUser | Manager | Admin
deriving (Eq, Show)
instance ToJSON Role where
toJSON RegularUser = "user"
toJSON Manager = "manager"
toJSON Admin = "admin"
data Session = Session
{ username :: Username
, password :: Password
, role :: Role
} deriving (Eq, Show)
instance ToJSON Session where
toJSON (Session username password role) =
object [ "username" .= username
, "password" .= password
, "role" .= role
]
|