forked from glotcode/glot-www
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSnippets.hs
177 lines (155 loc) · 5.69 KB
/
Snippets.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
module Handler.Snippets where
import Import
import Util.Handler (pageNo, title)
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
getSnippetsR :: Handler Html
getSnippetsR = do
App{..} <- getYesod
currentPage <- pageNo <$> lookupGetParam "page"
maybeLanguageParam <- lookupGetParam "language"
let snippetsPerPage = 20
let limitOffset = Persistent.LimitOffset
{ limit = snippetsPerPage
, offset = (currentPage - 1) * snippetsPerPage
}
Persistent.EntitiesWithCount{..} <- Persistent.getEntitiesWithCount (getEntitiesQuery limitOffset maybeLanguageParam)
let SnippetEntriesWithPagination{..} = SnippetEntriesWithPagination
{ entries = map (uncurry snippetEntryFromEntity) entities
, pagination = Pagination.fromPageData
Pagination.PageData
{ currentPage = currentPage
, totalEntries = entitiesCount
, entriesPerPage = snippetsPerPage
}
}
defaultLayout $ do
setTitle $ title "Public snippets"
setDescription "List of public code snippets"
Handler.setCanonicalUrl SnippetsR
addScript $ StaticR js_date_js
$(widgetFile "snippets")
getEntitiesQuery :: Persistent.LimitOffset -> Maybe Text -> Persistent.GetEntitiesWithCountQuery
getEntitiesQuery limitOffset maybeLanguage =
case maybeLanguage of
Just language ->
getSnippetsByLanguageWithCountQuery language limitOffset
Nothing ->
getSnippetsWithCountQuery limitOffset
getSnippetsWithCountQuery :: Persistent.LimitOffset -> Persistent.GetEntitiesWithCountQuery
getSnippetsWithCountQuery Persistent.LimitOffset{..} =
Persistent.GetEntitiesWithCountQuery
{ getEntities = Persistent.RawQuery
{ query = [Multiline.multiline|
select
??, ??
from
code_snippet
left join
profile on code_snippet.user_id = profile.user_id
where
code_snippet.public is true
and
code_snippet.user_id is not null
and
code_snippet.language <> 'plaintext'
and
lower(code_snippet.title) <> 'untitled'
order by
code_snippet.created desc
limit ?
offset ?
|]
, queryValues =
[ toPersistValue limit
, toPersistValue offset
]
}
, countEntities = Persistent.RawQuery
{ query = [Multiline.multiline|
select
count(*)
from
code_snippet
where
code_snippet.public is true
and
code_snippet.user_id is not null
and
code_snippet.language <> 'plaintext'
and
lower(code_snippet.title) <> 'untitled'
|]
, queryValues = []
}
}
getSnippetsByLanguageWithCountQuery :: Text -> Persistent.LimitOffset -> Persistent.GetEntitiesWithCountQuery
getSnippetsByLanguageWithCountQuery language Persistent.LimitOffset{..} =
Persistent.GetEntitiesWithCountQuery
{ getEntities = Persistent.RawQuery
{ query = [Multiline.multiline|
select
??, ??
from
code_snippet
left join
profile on code_snippet.user_id = profile.user_id
where
code_snippet.public is true
and
code_snippet.user_id is not null
and
code_snippet.language = ?
and
lower(code_snippet.title) <> 'untitled'
order by
code_snippet.created desc
limit ?
offset ?
|]
, queryValues =
[ toPersistValue language
, toPersistValue limit
, toPersistValue offset
]
}
, countEntities = Persistent.RawQuery
{ query = [Multiline.multiline|
select
count(*)
from
code_snippet
where
code_snippet.public is true
and
code_snippet.user_id is not null
and
code_snippet.language = ?
and
lower(code_snippet.title) <> 'untitled'
|]
, queryValues =
[ toPersistValue language
]
}
}
data SnippetEntry = SnippetEntry
{ entrySnippet :: CodeSnippet
, entryProfile :: Maybe Profile
}
snippetEntryFromEntity :: Entity CodeSnippet -> Maybe (Entity Profile) -> SnippetEntry
snippetEntryFromEntity codeSnippetEntity profileEntity =
SnippetEntry
{ entrySnippet = entityVal codeSnippetEntity
, entryProfile = fmap entityVal profileEntity
}
data SnippetEntriesWithPagination = SnippetEntriesWithPagination
{ entries :: [SnippetEntry]
, pagination :: Pagination.Pagination
}