From ffb477011fa4d3d530b87e55a35e00deab39b8fe Mon Sep 17 00:00:00 2001
From: John Watkins <watkinsj18@cardiff.ac.uk>
Date: Fri, 10 Dec 2021 11:32:10 +0000
Subject: [PATCH] Added filter of shops that people have stamps or have starred

---
 .../web/restControllers/ShopSearch.java       | 29 +++++++++++++++++++
 1 file changed, 29 insertions(+)

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 10e2013..65bd872 100644
--- a/src/main/java/com/example/clientproject/web/restControllers/ShopSearch.java
+++ b/src/main/java/com/example/clientproject/web/restControllers/ShopSearch.java
@@ -4,6 +4,9 @@ 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.data.users.Users;
+import com.example.clientproject.service.Utils.JWTUtils;
+import com.example.clientproject.services.DashboardStampLoader;
 import com.example.clientproject.services.RecommendationGenerator;
 import com.google.gson.Gson;
 import org.springframework.beans.factory.annotation.Autowired;
@@ -26,6 +29,12 @@ public class ShopSearch {
     @Autowired
     RecommendationGenerator recommendationGenerator;
 
+    @Autowired
+    DashboardStampLoader stampLoader;
+
+    @Autowired
+    JWTUtils jwtUtils;
+
     @GetMapping("/shop/search")
     public String searchShops(@RequestParam(value = "q", required = false) String query,
                               @RequestParam(value = "p", required = false) Integer page,
@@ -36,6 +45,26 @@ public class ShopSearch {
         //Get all the active shops
         List<Shops> allShops = shopsRepo.findActiveShops();
 
+        Optional<Users> user = jwtUtils.getLoggedInUserRow(session);
+        if(user.isPresent()){
+            Map<String,Object> userInfo = stampLoader.getData((int) user.get().getUserId());
+
+            //Filter off shops that the user has stamps in
+            List<Shops> purchased = (List<Shops>)userInfo.get("purchased");
+            allShops = allShops
+                    .stream()
+                    .filter(s -> !purchased.contains(s))
+                    .collect(Collectors.toList());
+
+            //Filter off shops the user has favourited
+            List<Shops> favourited = (List<Shops>)userInfo.get("favourited");
+            allShops = allShops
+                    .stream()
+                    .filter(s -> !favourited.contains(s))
+                    .collect(Collectors.toList());
+        }
+
+
         //Filter the shops using the query provided
         if(query != null){
             allShops = allShops
-- 
GitLab