Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
A
ASE_Y2S1_CP_G4
Manage
Activity
Members
Labels
Plan
Issues
0
Issue boards
Milestones
Wiki
Requirements
Code
Merge requests
0
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Locked files
Build
Pipelines
Jobs
Pipeline schedules
Test cases
Artifacts
Deploy
Releases
Package Registry
Container Registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Code review analytics
Issue analytics
Insights
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Joshua Gill
ASE_Y2S1_CP_G4
Commits
002ab17d
Commit
002ab17d
authored
3 years ago
by
John Watkins
Browse files
Options
Downloads
Patches
Plain Diff
Added pagination to the shop results
parent
c879bf65
No related branches found
No related tags found
3 merge requests
!114
LoggingService service class, new method to add a log to the "Logs" table when...
,
!106
Branch Update
,
!105
Issue thirty
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
src/main/java/com/example/clientproject/web/restControllers/ShopSearch.java
+65
-3
65 additions, 3 deletions
...example/clientproject/web/restControllers/ShopSearch.java
with
65 additions
and
3 deletions
src/main/java/com/example/clientproject/web/restControllers/ShopSearch.java
+
65
−
3
View file @
002ab17d
...
@@ -2,13 +2,14 @@ package com.example.clientproject.web.restControllers;
...
@@ -2,13 +2,14 @@ package com.example.clientproject.web.restControllers;
import
com.example.clientproject.data.shops.Shops
;
import
com.example.clientproject.data.shops.Shops
;
import
com.example.clientproject.data.shops.ShopsRepo
;
import
com.example.clientproject.data.shops.ShopsRepo
;
import
com.example.clientproject.data.tags.Tags
;
import
com.example.clientproject.data.tags.TagsRepo
;
import
org.springframework.beans.factory.annotation.Autowired
;
import
org.springframework.beans.factory.annotation.Autowired
;
import
org.springframework.web.bind.annotation.GetMapping
;
import
org.springframework.web.bind.annotation.GetMapping
;
import
org.springframework.web.bind.annotation.RequestParam
;
import
org.springframework.web.bind.annotation.RequestParam
;
import
org.springframework.web.bind.annotation.RestController
;
import
org.springframework.web.bind.annotation.RestController
;
import
java.util.List
;
import
java.util.*
;
import
java.util.Locale
;
import
java.util.stream.Collectors
;
import
java.util.stream.Collectors
;
@RestController
@RestController
...
@@ -16,11 +17,14 @@ public class ShopSearch {
...
@@ -16,11 +17,14 @@ public class ShopSearch {
@Autowired
@Autowired
ShopsRepo
shopsRepo
;
ShopsRepo
shopsRepo
;
@Autowired
TagsRepo
tagsRepo
;
@GetMapping
(
"/shop/search"
)
@GetMapping
(
"/shop/search"
)
public
String
searchShops
(
@RequestParam
(
value
=
"q"
,
required
=
false
)
String
query
,
public
String
searchShops
(
@RequestParam
(
value
=
"q"
,
required
=
false
)
String
query
,
@RequestParam
(
value
=
"p"
,
required
=
false
)
Integer
page
,
@RequestParam
(
value
=
"p"
,
required
=
false
)
Integer
page
,
@RequestParam
(
value
=
"t"
,
required
=
false
)
List
<
String
>
tags
){
@RequestParam
(
value
=
"t"
,
required
=
false
)
List
<
String
>
tags
){
final
Integer
itemsPerPage
=
6
;
final
Integer
ITEMS_PER_PAGE
=
6
;
//Get all the active shops
//Get all the active shops
List
<
Shops
>
allShops
=
shopsRepo
.
findActiveShops
();
List
<
Shops
>
allShops
=
shopsRepo
.
findActiveShops
();
...
@@ -34,12 +38,70 @@ public class ShopSearch {
...
@@ -34,12 +38,70 @@ public class ShopSearch {
}
}
//Filter using the tags provided
//Filter using the tags provided
if
(
tags
!=
null
){
List
<
Long
>
validTagIds
=
new
ArrayList
<>();
for
(
String
t
:
tags
){
Optional
<
Tags
>
tagsOptional
=
tagsRepo
.
findByTagNameIgnoreCase
(
t
);
if
(
tagsOptional
.
isPresent
()){
Long
tagId
=
tagsOptional
.
get
().
getTagId
();
if
(!
validTagIds
.
contains
(
tagId
)){
validTagIds
.
add
(
tagId
);
}
}
}
List
<
Shops
>
validShops
=
new
ArrayList
<>();
for
(
Shops
s
:
allShops
){
boolean
match
=
false
;
for
(
Tags
t
:
s
.
getShopTags
()){
if
(
validTagIds
.
contains
(
t
.
getTagId
())){
match
=
true
;
break
;
}
}
if
(
match
){
validShops
.
add
(
s
);
}
}
allShops
=
validShops
;
}
//Paginate
//Paginate
boolean
hasNextPage
=
false
;
if
(
allShops
.
size
()
>
ITEMS_PER_PAGE
){
if
(
page
==
null
){
page
=
1
;
}
List
<
List
<
Shops
>>
pages
=
getPages
(
allShops
,
ITEMS_PER_PAGE
);
if
(
page
>
pages
.
size
()){
page
=
1
;
}
if
(
pages
.
size
()
>=
page
){
allShops
=
pages
.
get
(
page
-
1
);
}
if
(
pages
.
size
()
>=
page
+
1
){
hasNextPage
=
true
;
}
}
//Convert to required format
//Convert to required format
//Add details such as the amount of rewards
return
allShops
.
toString
();
return
allShops
.
toString
();
}
}
public
<
T
>
List
<
List
<
T
>>
getPages
(
Collection
<
T
>
c
,
Integer
pageSize
)
{
if
(
c
==
null
)
return
Collections
.
emptyList
();
List
<
T
>
list
=
new
ArrayList
<
T
>(
c
);
if
(
pageSize
==
null
||
pageSize
<=
0
||
pageSize
>
list
.
size
())
pageSize
=
list
.
size
();
int
numPages
=
(
int
)
Math
.
ceil
((
double
)
list
.
size
()
/
(
double
)
pageSize
);
List
<
List
<
T
>>
pages
=
new
ArrayList
<
List
<
T
>>(
numPages
);
for
(
int
pageNum
=
0
;
pageNum
<
numPages
;)
pages
.
add
(
list
.
subList
(
pageNum
*
pageSize
,
Math
.
min
(++
pageNum
*
pageSize
,
list
.
size
())));
return
pages
;
}
}
}
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment