diff --git a/build.gradle b/build.gradle index 2bc40119073eb28d5de1c42825e41e177a753f49..ace6ecf23ddb939e56918a48ca5e28f7aed824c6 100644 --- a/build.gradle +++ b/build.gradle @@ -23,6 +23,7 @@ repositories { } dependencies { + implementation 'com.google.code.gson:gson:2.8.9' implementation 'commons-io:commons-io:2.8.0' implementation 'io.jsonwebtoken:jjwt:0.9.1' implementation 'org.springframework.session:spring-session-jdbc:2.4.3' diff --git a/src/main/java/com/example/clientproject/web/restControllers/ShopSearch.java b/src/main/java/com/example/clientproject/web/restControllers/ShopSearch.java index 7a7dd08aef9d696289c2873a20566e9a403e72d0..87fec0d3b81013ced4b129304e1ca169dc29ad99 100644 --- a/src/main/java/com/example/clientproject/web/restControllers/ShopSearch.java +++ b/src/main/java/com/example/clientproject/web/restControllers/ShopSearch.java @@ -4,11 +4,14 @@ 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; @@ -20,10 +23,14 @@ public class ShopSearch { @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){ + @RequestParam(value = "t", required = false) List<String> tags, + HttpSession session) throws Exception { final Integer ITEMS_PER_PAGE = 6; //Get all the active shops @@ -83,12 +90,33 @@ public class ShopSearch { } } - //Convert to required format + //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); + } - //Add details such as the amount of rewards + Gson gson = new Gson(); + String json = gson.toJson(formattedShops); - return allShops.toString(); + return json; } diff --git a/src/main/resources/static/js/searchBar.js b/src/main/resources/static/js/searchBar.js index 560f8277c0d5057e81111734817fc1527b8fd171..95a20c04efb08d597edd9c6a4b8b357db27085f2 100644 --- a/src/main/resources/static/js/searchBar.js +++ b/src/main/resources/static/js/searchBar.js @@ -52,6 +52,6 @@ function doSearch(){ url += "&t=" + t.toString() } fetch(url) - .then(e=>e.text()) + .then(e=>e.json()) .then(data=>console.log(data)) } \ No newline at end of file