-
Notifications
You must be signed in to change notification settings - Fork 16
/
Queries.scala
326 lines (252 loc) · 11.5 KB
/
Queries.scala
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
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
package com.gu.contentapi.client.model
import com.gu.contentapi.client.utils.QueryStringParams
import com.gu.contentapi.client.{Parameter, Parameters}
sealed trait ContentApiQuery {
def parameters: Map[String, String]
def pathSegment: String
override def toString = s"""${getClass.getSimpleName}(${getUrl("")})"""
private def url(location: String, parameters: Map[String, String]): String = {
require(!location.contains('?'), "must not specify parameters in URL")
location + QueryStringParams(parameters)
}
def getUrl(targetUrl: String, customParameters: Map[String, String] = Map.empty): String =
url(s"$targetUrl/${pathSegment}", parameters ++ customParameters)
}
trait SearchQueryBase[Self <: SearchQueryBase[Self]]
extends ContentApiQuery
with ShowParameters[Self]
with ShowReferencesParameters[Self]
with OrderByParameter[Self]
with UseDateParameter[Self]
with PaginationParameters[Self]
with FilterParameters[Self]
with FilterExtendedParameters[Self]
with FilterSearchParameters[Self] {
this: Self =>
}
case class ItemQuery(id: String, parameterHolder: Map[String, Parameter] = Map.empty)
extends ContentApiQuery
with EditionParameters[ItemQuery]
with ShowParameters[ItemQuery]
with ShowReferencesParameters[ItemQuery]
with ShowExtendedParameters[ItemQuery]
with PaginationParameters[ItemQuery]
with OrderByParameter[ItemQuery]
with UseDateParameter[ItemQuery]
with FilterParameters[ItemQuery]
with FilterExtendedParameters[ItemQuery]
with FilterSearchParameters[ItemQuery] {
def withParameters(parameterMap: Map[String, Parameter]) = copy(id, parameterMap)
def itemId(contentId: String): ItemQuery =
copy(id = contentId)
override def pathSegment: String = id
}
case class SearchQuery(parameterHolder: Map[String, Parameter] = Map.empty)
extends SearchQueryBase[SearchQuery] {
def withParameters(parameterMap: Map[String, Parameter]): SearchQuery = copy(parameterMap)
override def pathSegment: String = "search"
}
case class RemovedContentQuery(parameterHolder: Map[String, Parameter] = Map.empty)
extends ContentApiQuery
with RemovedReasonParameters[RemovedContentQuery]
with PaginationParameters[RemovedContentQuery]
with OrderByParameter[RemovedContentQuery]
with UseDateParameter[RemovedContentQuery]{
def withParameters(parameterMap: Map[String, Parameter]) = copy(parameterMap)
def pathSegment: String = "content/removed"
}
case class TagsQuery(parameterHolder: Map[String, Parameter] = Map.empty)
extends ContentApiQuery
with ShowReferencesParameters[TagsQuery]
with PaginationParameters[TagsQuery]
with FilterParameters[TagsQuery]
with FilterTagParameters[TagsQuery]
with FilterSearchParameters[TagsQuery] {
def withParameters(parameterMap: Map[String, Parameter]) = copy(parameterMap)
override def pathSegment: String = "tags"
}
case class SectionsQuery(parameterHolder: Map[String, Parameter] = Map.empty)
extends ContentApiQuery
with FilterSearchParameters[SectionsQuery]
with FilterSectionParameters[SectionsQuery]{
def withParameters(parameterMap: Map[String, Parameter]) = copy(parameterMap)
override def pathSegment: String = "sections"
}
case class EditionsQuery(parameterHolder: Map[String, Parameter] = Map.empty)
extends ContentApiQuery
with FilterSearchParameters[EditionsQuery] {
def withParameters(parameterMap: Map[String, Parameter]) = copy(parameterMap)
override def pathSegment: String = "editions"
}
case class VideoStatsQuery(
edition: Option[String] = None,
section: Option[String] = None,
parameterHolder: Map[String, Parameter] = Map.empty)
extends ContentApiQuery
with FilterSearchParameters[VideoStatsQuery] {
def withParameters(parameterMap: Map[String, Parameter]) = copy(edition, section, parameterMap)
override def pathSegment: String = Seq(Some("stats/videos"), edition, section).flatten.mkString("/")
}
case class AtomsQuery(parameterHolder: Map[String, Parameter] = Map.empty)
extends ContentApiQuery
with AtomsParameters[AtomsQuery]
with PaginationParameters[AtomsQuery]
with OrderByParameter[AtomsQuery]
with FilterSearchParameters[AtomsQuery] {
def withParameters(parameterMap: Map[String, Parameter]) = copy(parameterMap)
override def pathSegment: String = "atoms"
}
case class RecipesQuery(parameterHolder: Map[String, Parameter] = Map.empty)
extends ContentApiQuery
with PaginationParameters[RecipesQuery]
with RecipeParameters[RecipesQuery] {
def withParameters(parameterMap: Map[String, Parameter]) = copy(parameterMap)
override def pathSegment: String = "atoms/recipes"
}
case class ReviewsQuery(parameterHolder: Map[String, Parameter] = Map.empty)
extends ContentApiQuery
with PaginationParameters[ReviewsQuery]
with ReviewSpecificParameters[ReviewsQuery] {
def withParameters(parameterMap: Map[String, Parameter]) = copy(parameterMap)
override def pathSegment: String = "atoms/reviews"
}
case class GameReviewsQuery(parameterHolder: Map[String, Parameter] = Map.empty)
extends ContentApiQuery
with ReviewSpecificParameters[GameReviewsQuery]
with PaginationParameters[GameReviewsQuery]
with GameParameters[GameReviewsQuery] {
def withParameters(parameterMap: Map[String, Parameter]) = copy(parameterMap)
override def pathSegment: String = "atoms/reviews/game"
}
case class RestaurantReviewsQuery(parameterHolder: Map[String, Parameter] = Map.empty)
extends ContentApiQuery
with ReviewSpecificParameters[RestaurantReviewsQuery]
with PaginationParameters[RestaurantReviewsQuery]
with RestaurantParameters[RestaurantReviewsQuery] {
def withParameters(parameterMap: Map[String, Parameter]) = copy(parameterMap)
override def pathSegment: String = "atoms/reviews/restaurant"
}
case class FilmReviewsQuery(parameterHolder: Map[String, Parameter] = Map.empty)
extends ContentApiQuery
with ReviewSpecificParameters[FilmReviewsQuery]
with PaginationParameters[FilmReviewsQuery]
with FilmParameters[FilmReviewsQuery] {
def withParameters(parameterMap: Map[String, Parameter]) = copy(parameterMap)
override def pathSegment: String = "atoms/reviews/film"
}
case class StoriesQuery(parameterHolder: Map[String, Parameter] = Map.empty)
extends ContentApiQuery
with StoryParameters[StoriesQuery] {
def withParameters(parameterMap: Map[String, Parameter]) = copy(parameterMap)
override def pathSegment: String = "stories"
}
case class NextQuery[Q <: PaginatedApiQuery[Q]](originalQuery: Q, contentId: String)
extends ContentApiQuery {
def parameters: Map[String, String] = originalQuery.parameters.filterKeys(not(isPaginationParameter))
override def pathSegment: String = s"""content/${contentId}/next"""
}
trait StoryParameters[Owner <: Parameters[Owner]] extends Parameters[Owner] { this: Owner =>
def showAtoms = StringParameter("show-atoms")
def name = StringParameter("name")
}
trait EditionParameters[Owner <: Parameters[Owner]] extends Parameters[Owner] { this: Owner =>
def edition = StringParameter("edition")
}
trait ShowParameters[Owner <: Parameters[Owner]] extends Parameters[Owner] { this: Owner =>
def showFields = StringParameter("show-fields")
def showTags = StringParameter("show-tags")
def showElements = StringParameter("show-elements")
def showRights = StringParameter("show-rights")
def showBlocks = StringParameter("show-blocks")
def showAtoms = StringParameter("show-atoms")
def showSection = BoolParameter("show-section")
def showStats = BoolParameter("show-stats")
}
trait ShowReferencesParameters[Owner <: Parameters[Owner]] extends Parameters[Owner] { this: Owner =>
def showReferences = StringParameter("show-references")
}
trait ShowExtendedParameters[Owner <: Parameters[Owner]] extends Parameters[Owner] { this: Owner =>
def showStoryPackage = BoolParameter("show-story-package")
def showRelated = BoolParameter("show-related")
def showMostViewed = BoolParameter("show-most-viewed")
def showEditorsPicks = BoolParameter("show-editors-picks")
def showPackages = BoolParameter("show-packages")
}
trait PaginationParameters[Owner <: Parameters[Owner]] extends Parameters[Owner] { this: Owner =>
def page = IntParameter("page")
def pageSize = IntParameter("page-size")
}
trait OrderByParameter[Owner <: Parameters[Owner]] extends Parameters[Owner] { this: Owner =>
def orderBy = StringParameter("order-by")
}
trait UseDateParameter[Owner <: Parameters[Owner]] extends Parameters[Owner] { this: Owner =>
def useDate = StringParameter("use-date")
}
trait FilterParameters[Owner <: Parameters[Owner]] extends Parameters[Owner] { this: Owner =>
def section = StringParameter("section")
def reference = StringParameter("reference")
def referenceType = StringParameter("reference-type")
def productionOffice = StringParameter("production-office")
}
trait FilterExtendedParameters[Owner <: Parameters[Owner]] extends Parameters[Owner] { this: Owner =>
def tag = StringParameter("tag")
def ids = StringParameter("ids")
def rights = StringParameter("rights")
def leadContent = StringParameter("lead-content")
def fromDate = DateParameter("from-date")
def toDate = DateParameter("to-date")
def contentType = StringParameter("type")
def lang = StringParameter("lang")
def starRating = IntParameter("star-rating")
def membershipAccess = StringParameter("membership-access")
def containsElement = StringParameter("contains-element")
def commentable = BoolParameter("commentable")
def filename = StringParameter("filename")
}
trait FilterTagParameters[Owner <: Parameters[Owner]] extends Parameters[Owner] { this: Owner =>
def tagType = StringParameter("type")
def sponsorshipType = StringParameter("sponsorship-type")
}
trait FilterSectionParameters[Owner <: Parameters[Owner]] extends Parameters[Owner] { this: Owner =>
def sponsorshipType = StringParameter("sponsorship-type")
}
trait FilterSearchParameters[Owner <: Parameters[Owner]] extends Parameters[Owner] { this: Owner =>
def q = StringParameter("q")
}
// Supports values gone, expired and takendown.
trait RemovedReasonParameters[Owner <: Parameters[Owner]] extends Parameters[Owner] { this: Owner =>
def reason = StringParameter("reason")
}
trait AtomsParameters[Owner <: Parameters[Owner]] extends Parameters[Owner] { this: Owner =>
def types = StringParameter("types")
def searchFields = StringParameter("searchFields")
def fromDate = DateParameter("from-date")
def toDate = DateParameter("to-date")
}
trait RecipeParameters[Owner <: Parameters[Owner]] extends Parameters[Owner] { this: Owner =>
def title = StringParameter("title")
def credits = StringParameter("credits")
def categories = StringParameter("category")
def cuisines = StringParameter("cuisine")
def dietary = StringParameter("dietary")
def celebration = StringParameter("celebration")
def ingredients = StringParameter("ingredients")
def maxTime = IntParameter("max-time")
}
trait ReviewSpecificParameters[Owner <: Parameters[Owner]] extends Parameters[Owner] { this: Owner =>
def reviewer = StringParameter("reviewer")
def maxRating = IntParameter("max-rating")
def minRating = IntParameter("min-rating")
}
trait FilmParameters[Owner <: Parameters[Owner]] extends Parameters[Owner] { this: Owner =>
def name = StringParameter("name")
def genres = StringParameter("genres")
def actors = StringParameter("actors")
def directors = StringParameter("directors")
}
trait GameParameters[Owner <: Parameters[Owner]] extends Parameters[Owner] { this: Owner =>
def gameName = StringParameter("name")
}
trait RestaurantParameters[Owner <: Parameters[Owner]] extends Parameters[Owner] { this: Owner =>
def restaurantName = StringParameter("name")
}