forked from glotcode/glot-www
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathUserSnippets.hs
251 lines (221 loc) · 8.12 KB
/
UserSnippets.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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
module Handler.UserSnippets where
import Import
import Util.Handler (pageNo, titleConcat)
import Widget.Pagination (paginationWidget)
import qualified Glot.Pagination as Pagination
import qualified Util.Persistent as Persistent
import qualified Util.Snippet as Snippet
import qualified Util.Multiline as Multiline
import qualified Data.Time.Format.ISO8601 as ISO8601
import qualified Util.Handler as Handler
import qualified Glot.Language as Language
getUserSnippetsR :: Text -> Handler Html
getUserSnippetsR username = do
App{..} <- getYesod
Entity _ profile <- runDB $ getBy404 $ UniqueUsername username
maybeLoggedInUserId <- maybeAuthId
currentPage <- pageNo <$> lookupGetParam "page"
maybeLanguageParam <- lookupGetParam "language"
let snippetsPerPage = 20
let allowedUserSnippets = allowedUserSnippetsFromLoggedInUser (profileUserId profile) maybeLoggedInUserId
let limitOffset = Persistent.LimitOffset
{ limit = snippetsPerPage
, offset = (currentPage - 1) * snippetsPerPage
}
Persistent.EntitiesWithCount{..} <- Persistent.getEntitiesWithCount (getEntitiesQuery limitOffset allowedUserSnippets maybeLanguageParam)
let snippets = map entityVal entities :: [CodeSnippet]
let pagination = Pagination.fromPageData
Pagination.PageData
{ currentPage = currentPage
, totalEntries = entitiesCount
, entriesPerPage = snippetsPerPage
}
defaultLayout $ do
setTitle $ titleConcat ["Snippets by ", profileName profile]
setDescription $ concat ["Public code snippets by ", profileName profile]
Handler.setCanonicalUrl (UserSnippetsR username)
addScript $ StaticR js_date_js
$(widgetFile "user-snippets")
data AllowedUserSnippets
= OnlyPublic UserId
| All UserId
allowedUserSnippetsFromLoggedInUser :: UserId -> Maybe UserId -> AllowedUserSnippets
allowedUserSnippetsFromLoggedInUser wantedUserId loggedInUserId =
if Just wantedUserId == loggedInUserId then
All wantedUserId
else
OnlyPublic wantedUserId
getEntitiesQuery :: Persistent.LimitOffset -> AllowedUserSnippets -> Maybe Text -> Persistent.GetEntitiesWithCountQuery
getEntitiesQuery limitOffset allowedUserSnippets maybeLanguage =
case (maybeLanguage, allowedUserSnippets) of
(Just language, All userId) ->
getAllSnippetsByLanguageWithCountQuery language userId limitOffset
(Just language, OnlyPublic userId) ->
getPublicSnippetsByLanguageWithCountQuery language userId limitOffset
(Nothing, All userId) ->
getAllSnippetsWithCountQuery userId limitOffset
(Nothing, OnlyPublic userId) ->
getPublicSnippetsWithCountQuery userId limitOffset
getPublicSnippetsWithCountQuery :: UserId -> Persistent.LimitOffset -> Persistent.GetEntitiesWithCountQuery
getPublicSnippetsWithCountQuery userId Persistent.LimitOffset{..} =
Persistent.GetEntitiesWithCountQuery
{ getEntities = Persistent.RawQuery
{ query = [Multiline.multiline|
select
??
from
code_snippet
where
code_snippet.user_id = ?
and
code_snippet.public is true
order by
code_snippet.created desc
limit ?
offset ?
|]
, queryValues =
[ toPersistValue userId
, toPersistValue limit
, toPersistValue offset
]
}
, countEntities = Persistent.RawQuery
{ query = [Multiline.multiline|
select
count(*)
from
code_snippet
where
code_snippet.user_id = ?
and
code_snippet.public is true
|]
, queryValues =
[toPersistValue userId]
}
}
getAllSnippetsWithCountQuery :: UserId -> Persistent.LimitOffset -> Persistent.GetEntitiesWithCountQuery
getAllSnippetsWithCountQuery userId Persistent.LimitOffset{..} =
Persistent.GetEntitiesWithCountQuery
{ getEntities = Persistent.RawQuery
{ query = [Multiline.multiline|
select
??
from
code_snippet
where
code_snippet.user_id = ?
order by
code_snippet.created desc
limit ?
offset ?
|]
, queryValues =
[ toPersistValue userId
, toPersistValue limit
, toPersistValue offset
]
}
, countEntities = Persistent.RawQuery
{ query = [Multiline.multiline|
select
count(*)
from
code_snippet
where
code_snippet.user_id = ?
|]
, queryValues =
[toPersistValue userId
]
}
}
getPublicSnippetsByLanguageWithCountQuery :: Text -> UserId -> Persistent.LimitOffset -> Persistent.GetEntitiesWithCountQuery
getPublicSnippetsByLanguageWithCountQuery language userId Persistent.LimitOffset{..} =
Persistent.GetEntitiesWithCountQuery
{ getEntities = Persistent.RawQuery
{ query = [Multiline.multiline|
select
??
from
code_snippet
where
code_snippet.user_id = ?
and
code_snippet.public is true
and
code_snippet.language = ?
order by
code_snippet.created desc
limit ?
offset ?
|]
, queryValues =
[ toPersistValue userId
, toPersistValue language
, toPersistValue limit
, toPersistValue offset
]
}
, countEntities = Persistent.RawQuery
{ query = [Multiline.multiline|
select
count(*)
from
code_snippet
where
code_snippet.user_id = ?
and
code_snippet.public is true
and
code_snippet.language = ?
|]
, queryValues =
[ toPersistValue userId
, toPersistValue language
]
}
}
getAllSnippetsByLanguageWithCountQuery :: Text -> UserId -> Persistent.LimitOffset -> Persistent.GetEntitiesWithCountQuery
getAllSnippetsByLanguageWithCountQuery language userId Persistent.LimitOffset{..} =
Persistent.GetEntitiesWithCountQuery
{ getEntities = Persistent.RawQuery
{ query = [Multiline.multiline|
select
??
from
code_snippet
where
code_snippet.user_id = ?
and
code_snippet.language = ?
order by
code_snippet.created desc
limit ?
offset ?
|]
, queryValues =
[ toPersistValue userId
, toPersistValue language
, toPersistValue limit
, toPersistValue offset
]
}
, countEntities = Persistent.RawQuery
{ query = [Multiline.multiline|
select
count(*)
from
code_snippet
where
code_snippet.user_id = ?
and
code_snippet.language = ?
|]
, queryValues =
[ toPersistValue userId
, toPersistValue language
]
}
}