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
Merge requests
!106
Branch Update
Code
Review changes
Check out branch
Download
Patches
Plain diff
Merged
Branch Update
develop
into
issueThirtyNine
Overview
0
Commits
8
Pipelines
0
Changes
8
Merged
Joshua Gill
requested to merge
develop
into
issueThirtyNine
3 years ago
Overview
0
Commits
8
Pipelines
0
Changes
8
Expand
0
0
Merge request reports
Compare
issueThirtyNine
issueThirtyNine (base)
and
latest version
latest version
50506d2d
8 commits,
3 years ago
8 files
+
267
−
8
Side-by-side
Compare changes
Side-by-side
Inline
Show whitespace changes
Show one file at a time
Files
8
Search (e.g. *.vue) (Ctrl+P)
src/main/java/com/example/clientproject/web/restControllers/ShopSearch.java
0 → 100644
+
139
−
0
Options
package
com.example.clientproject.web.restControllers
;
import
com.example.clientproject.data.shops.Shops
;
import
com.example.clientproject.data.shops.ShopsRepo
;
import
com.example.clientproject.data.tags.Tags
;
import
com.example.clientproject.data.tags.TagsRepo
;
import
com.example.clientproject.services.RecommendationGenerator
;
import
com.google.gson.Gson
;
import
org.springframework.beans.factory.annotation.Autowired
;
import
org.springframework.web.bind.annotation.GetMapping
;
import
org.springframework.web.bind.annotation.RequestParam
;
import
org.springframework.web.bind.annotation.RestController
;
import
javax.servlet.http.HttpSession
;
import
java.util.*
;
import
java.util.stream.Collectors
;
@RestController
public
class
ShopSearch
{
@Autowired
ShopsRepo
shopsRepo
;
@Autowired
TagsRepo
tagsRepo
;
@Autowired
RecommendationGenerator
recommendationGenerator
;
@GetMapping
(
"/shop/search"
)
public
String
searchShops
(
@RequestParam
(
value
=
"q"
,
required
=
false
)
String
query
,
@RequestParam
(
value
=
"p"
,
required
=
false
)
Integer
page
,
@RequestParam
(
value
=
"t"
,
required
=
false
)
List
<
String
>
tags
,
HttpSession
session
)
throws
Exception
{
final
Integer
ITEMS_PER_PAGE
=
6
;
//Get all the active shops
List
<
Shops
>
allShops
=
shopsRepo
.
findActiveShops
();
//Filter the shops using the query provided
if
(
query
!=
null
){
allShops
=
allShops
.
stream
()
.
filter
(
s
->
s
.
getShopName
().
toLowerCase
(
Locale
.
ROOT
).
strip
().
contains
(
query
.
toLowerCase
(
Locale
.
ROOT
).
strip
()))
.
collect
(
Collectors
.
toList
());
}
//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
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
;
}
}
//Sort in order of relevance
allShops
=
recommendationGenerator
.
getRecommendations
(
session
,
allShops
);
//Convert to required format
List
<
HashMap
<
String
,
String
>>
formattedShops
=
new
ArrayList
<>();
for
(
Shops
shop
:
allShops
){
HashMap
<
String
,
String
>
data
=
new
HashMap
<>();
data
.
put
(
"name"
,
shop
.
getShopName
());
data
.
put
(
"banner"
,
shop
.
getShopBanner
());
data
.
put
(
"id"
,
String
.
valueOf
(
shop
.
getShopId
()));
data
.
put
(
"category"
,
shop
.
getCategory
().
getCategoryName
());
data
.
put
(
"website"
,
shop
.
getShopWebsite
());
Integer
reward_count
=
shop
.
getStampBoard
().
getRewards
().
size
();
data
.
put
(
"reward_count"
,
String
.
valueOf
(
reward_count
));
if
(
reward_count
!=
0
){
data
.
put
(
"next_reward_name"
,
shop
.
getStampBoard
().
getRewards
().
get
(
0
).
getRewardName
());
data
.
put
(
"next_reward_pos"
,
String
.
valueOf
(
shop
.
getStampBoard
().
getRewards
().
get
(
0
).
getRewardStampLocation
()));
}
else
{
data
.
put
(
"next_reward_name"
,
"No Rewards"
);
}
formattedShops
.
add
(
data
);
}
Map
<
String
,
Object
>
returnMap
=
new
HashMap
<>();
returnMap
.
put
(
"shops"
,
formattedShops
);
returnMap
.
put
(
"hasNextPage"
,
hasNextPage
);
Gson
gson
=
new
Gson
();
String
json
=
gson
.
toJson
(
returnMap
);
return
json
;
}
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
;
}
}
Loading