-
-
Notifications
You must be signed in to change notification settings - Fork 62
/
Copy pathSnippet.hs
151 lines (111 loc) · 3.59 KB
/
Snippet.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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
{-# LANGUAGE DeriveGeneric #-}
module Glot.Snippet
( SnippetPayload(..)
, toCodeSnippet
, FilePayload(..)
, toCodeFile
, newSlug
, titleFromText
, fileContentFromText
) where
import Import
import qualified GHC.Generics as GHC
import qualified Data.Aeson as Aeson
import qualified Data.Time.Clock.POSIX as PosixClock
import qualified Data.Time.Clock as Clock
import qualified Numeric
import qualified Data.List.NonEmpty as NonEmpty
import qualified Prelude
import qualified Glot.Language as Language
import Data.Function ((&))
import Prelude ((!!))
data SnippetPayload = SnippetPayload
{ language :: Language.Id
, title :: Title
, public :: Bool
, files :: NonEmpty.NonEmpty FilePayload
}
deriving (Show, GHC.Generic)
instance Aeson.FromJSON SnippetPayload
toCodeSnippet :: Text -> UTCTime -> UTCTime -> Maybe UserId -> SnippetPayload -> CodeSnippet
toCodeSnippet slug createdAt modifiedAt maybeUserId SnippetPayload{..} =
CodeSnippet
{ codeSnippetSlug = slug
, codeSnippetLanguage = language
, codeSnippetTitle = titleToText title
, codeSnippetPublic = public
, codeSnippetUserId = maybeUserId
, codeSnippetCreated = createdAt
, codeSnippetModified = modifiedAt
}
data FilePayload = FilePayload
{ name :: Title
, content :: FileContent
}
deriving (Show, GHC.Generic)
instance Aeson.FromJSON FilePayload
instance Aeson.ToJSON FilePayload
toCodeFile :: CodeSnippetId -> FilePayload -> CodeFile
toCodeFile snippetId FilePayload{..} =
CodeFile
{ codeFileCodeSnippetId = snippetId
, codeFileName = titleToText name
, codeFileContent = encodeUtf8 (fileContentToText content)
}
newSlug :: UTCTime -> Text
newSlug time =
intToBase36 (microsecondsSinceEpoch time)
microsecondsSinceEpoch :: UTCTime -> Int64
microsecondsSinceEpoch time =
time
& PosixClock.utcTimeToPOSIXSeconds
& Clock.nominalDiffTimeToSeconds
& (1e6 *)
& floor
intToBase36 :: Int64 -> Text
intToBase36 number =
let
chars =
['0'..'9'] <> ['a'..'z']
intToChar n =
chars !! n
in
pack (Numeric.showIntAtBase 36 intToChar number "")
newtype Title = Title Text
deriving (Show, Aeson.ToJSON)
instance Aeson.FromJSON Title where
parseJSON = Aeson.withText "Title" $ \text ->
case titleFromText text of
Right title ->
pure title
Left msg ->
Prelude.fail $ unpack msg
titleFromText :: Text -> Either Text Title
titleFromText text =
if length text < 1 then
Left "field must contain at least one character"
else if length text > 100 then
Left "field must contain 100 characters or less"
else
Right (Title text)
titleToText :: Title -> Text
titleToText (Title title) = title
newtype FileContent = FileContent Text
deriving (Show, Aeson.ToJSON)
instance Aeson.FromJSON FileContent where
parseJSON = Aeson.withText "FileContent" $ \text ->
case fileContentFromText text of
Right content ->
pure content
Left msg ->
Prelude.fail $ unpack msg
fileContentFromText :: Text -> Either Text FileContent
fileContentFromText text =
if length text < 1 then
Left "field must contain at least one character"
else if length text > 1000000 then
Left "field must contain 1000000 characters or less"
else
Right (FileContent text)
fileContentToText :: FileContent -> Text
fileContentToText (FileContent content) = content