-
Notifications
You must be signed in to change notification settings - Fork 11
/
Types.hs
91 lines (77 loc) · 2.58 KB
/
Types.hs
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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
module Types
( Post(..)
, PostQuery(..)
, CodeRecord(..)
, TokenRecord(..)
, User(..)
, ResolvedPost(..)
, ResolvedCode(..)
, ResolvedToken(..)
, ID
, EmailAddress
, Token
, Code
) where
import ClassyPrelude
import qualified Crypto.Hash.MD5 as MD5
import Data.Aeson ((.=))
import qualified Data.Aeson as Aeson
import qualified Data.Aeson.Types as Aeson
import qualified Data.ByteString.Base16 as Hex
import Database.Schema (PostRowId, EmailAddress, Token)
data PostQuery = PostQuery { postQueryBefore :: Maybe ID
, postQueryAfter :: Maybe ID
, postQueryLimit :: Int
}
data Post = Post { postID :: ID
, postUserID :: ID
, postContent :: Text
, postAt :: UTCTime
, postParentID :: Maybe ID
, postCount :: Int
}
data CodeRecord = CodeRecord { codeValue :: Code
, codeGeneratedAt :: UTCTime
, codeValid :: Bool
, codeUserID :: ID
}
data TokenRecord = TokenRecord { tokenID :: ID
, tokenValue :: Token
, tokenUserID :: ID
}
data User = User { userID :: ID
, userName :: Text
, userEmail :: EmailAddress
}
data ResolvedPost = ResolvedPost Post User
data ResolvedCode = ResolvedCode CodeRecord User
data ResolvedToken = ResolvedToken TokenRecord User
postPairs :: Post -> [Aeson.Pair]
postPairs Post{..} = [ "id" .= postID
, "content" .= postContent
, "at" .= postAt
, "count" .= postCount
, "idParent" .= postParentID
]
instance Aeson.ToJSON Post where
toJSON post@Post{..} = Aeson.object
("idUser" .= postUserID : postPairs post)
gravatar :: Text -> Text
gravatar = decodeUtf8 . Hex.encode . MD5.hash . encodeUtf8
instance Aeson.ToJSON User where
toJSON User{..} = Aeson.object
[ "id" .= userID
, "name" .= userName
, "face" .= Aeson.object ["gravatar" .= gravatar userEmail]
]
instance Aeson.ToJSON ResolvedPost where
toJSON (ResolvedPost post@Post{..} user) = Aeson.object
("user" .= user : postPairs post)
instance Aeson.ToJSON ResolvedToken where
toJSON (ResolvedToken TokenRecord{..} user) = Aeson.object
[ "id" .= tokenID
, "token" .= tokenValue
, "user" .= user
]
type ID = Int
type Code = Text