From ff7cd52dcb0ecfdeb6c88d05feae644e42c77246 Mon Sep 17 00:00:00 2001 From: zeynep-baydemir Date: Sat, 23 Dec 2023 17:56:22 +0300 Subject: [PATCH 1/2] changed group recommendations logic --- .../gamereview/config/SpringdocConfig.java | 36 +++--- .../controller/GroupController.java | 14 ++- .../controller/ImageController.java | 112 +++++++++--------- .../app/gamereview/service/GroupService.java | 52 ++++++-- app/backend/src/main/resources/.env.example | 22 ++-- 5 files changed, 140 insertions(+), 96 deletions(-) diff --git a/app/backend/src/main/java/com/app/gamereview/config/SpringdocConfig.java b/app/backend/src/main/java/com/app/gamereview/config/SpringdocConfig.java index 8109ede7..2f9679fc 100644 --- a/app/backend/src/main/java/com/app/gamereview/config/SpringdocConfig.java +++ b/app/backend/src/main/java/com/app/gamereview/config/SpringdocConfig.java @@ -1,18 +1,18 @@ -package com.app.gamereview.config; - -import io.swagger.v3.oas.annotations.OpenAPIDefinition; -import io.swagger.v3.oas.models.OpenAPI; -import io.swagger.v3.oas.models.info.Info; -import org.springframework.context.annotation.Bean; -import org.springframework.context.annotation.Configuration; - -@OpenAPIDefinition -@Configuration -public class SpringdocConfig { - - @Bean - public OpenAPI baseOpenAPI() { - return new OpenAPI().info(new Info().title("Spring Doc").version("1.0.0").description("Spring doc")); - } - -} +//package com.app.gamereview.config; +// +//import io.swagger.v3.oas.annotations.OpenAPIDefinition; +//import io.swagger.v3.oas.models.OpenAPI; +//import io.swagger.v3.oas.models.info.Info; +//import org.springframework.context.annotation.Bean; +//import org.springframework.context.annotation.Configuration; +// +//@OpenAPIDefinition +//@Configuration +//public class SpringdocConfig { +// +// @Bean +// public OpenAPI baseOpenAPI() { +// return new OpenAPI().info(new Info().title("Spring Doc").version("1.0.0").description("Spring doc")); +// } +// +//} diff --git a/app/backend/src/main/java/com/app/gamereview/controller/GroupController.java b/app/backend/src/main/java/com/app/gamereview/controller/GroupController.java index 20dbfe5e..f2ee86e2 100644 --- a/app/backend/src/main/java/com/app/gamereview/controller/GroupController.java +++ b/app/backend/src/main/java/com/app/gamereview/controller/GroupController.java @@ -22,6 +22,7 @@ import org.springframework.web.bind.annotation.*; import java.util.List; +import java.util.Optional; @RestController @RequestMapping("/api/group") @@ -184,11 +185,16 @@ public ResponseEntity> listApplications(@Reque return ResponseEntity.ok(applications); } - @AuthorizationRequired @GetMapping("/get-recommendations") - public ResponseEntity> getRecommendedGroups(@RequestHeader String Authorization, HttpServletRequest request) { - User user = (User) request.getAttribute("authenticatedUser"); - List groups = groupService.getRecommendedGroups(user); + public ResponseEntity> getRecommendedGroups(@RequestHeader(name = HttpHeaders.AUTHORIZATION, + required = false) String Authorization) { + String email = ""; + if(Authorization == null){ + return ResponseEntity.ok(groupService.getRecommendedGroups(email)); + } + if (JwtUtil.validateToken(Authorization)) + email = JwtUtil.extractSubject(Authorization); + List groups = groupService.getRecommendedGroups(email); return ResponseEntity.ok(groups); } } diff --git a/app/backend/src/main/java/com/app/gamereview/controller/ImageController.java b/app/backend/src/main/java/com/app/gamereview/controller/ImageController.java index e8036fda..10d420f8 100644 --- a/app/backend/src/main/java/com/app/gamereview/controller/ImageController.java +++ b/app/backend/src/main/java/com/app/gamereview/controller/ImageController.java @@ -1,56 +1,56 @@ -package com.app.gamereview.controller; - -import com.app.gamereview.service.FileStorageService; -import org.springframework.beans.factory.annotation.Value; -import org.springframework.core.io.FileSystemResource; -import org.springframework.http.MediaType; -import org.springframework.http.ResponseEntity; -import org.springframework.stereotype.Controller; -import org.springframework.web.bind.annotation.*; -import org.springframework.core.io.Resource; -import org.springframework.web.multipart.MultipartFile; - -import java.io.File; -import java.io.IOException; - -@RestController -public class ImageController { - - private final FileStorageService fileService; - @Value("${image.base-directory}") - private String imageBaseDirectory; - - public ImageController(FileStorageService fileService) { - this.fileService = fileService; - } - - @GetMapping("/api/{folder}/{fileName:.+}") - public ResponseEntity serveImage(@PathVariable String folder, @PathVariable String fileName) { - try { - File imageFile = new File(imageBaseDirectory + folder + File.separator + fileName); - - if (imageFile.exists() && imageFile.isFile()) { - Resource resource = new FileSystemResource(imageFile); - return ResponseEntity.ok() - .contentLength(imageFile.length()) - .contentType(MediaType.IMAGE_PNG) // Set appropriate content type - // (e.g., IMAGE_PNG, IMAGE_JPEG) - .body(resource); - } - else { - // Handle resource not found error - return ResponseEntity.notFound().build(); - } - } - catch (Exception e) { - // Handle exceptions, e.g., file not found - return ResponseEntity.status(500).build(); - } - } - - @PostMapping("/api/image/upload") - public ResponseEntity uploadImage(@RequestParam String folder, @RequestPart MultipartFile image) throws IOException { - return ResponseEntity.ok(folder + "/" + fileService.storeFile(image, folder)); - } - -} +//package com.app.gamereview.controller; +// +//import com.app.gamereview.service.FileStorageService; +//import org.springframework.beans.factory.annotation.Value; +//import org.springframework.core.io.FileSystemResource; +//import org.springframework.http.MediaType; +//import org.springframework.http.ResponseEntity; +//import org.springframework.stereotype.Controller; +//import org.springframework.web.bind.annotation.*; +//import org.springframework.core.io.Resource; +//import org.springframework.web.multipart.MultipartFile; +// +//import java.io.File; +//import java.io.IOException; +// +//@RestController +//public class ImageController { +// +// private final FileStorageService fileService; +// @Value("${image.base-directory}") +// private String imageBaseDirectory; +// +// public ImageController(FileStorageService fileService) { +// this.fileService = fileService; +// } +// +// @GetMapping("/api/{folder}/{fileName:.+}") +// public ResponseEntity serveImage(@PathVariable String folder, @PathVariable String fileName) { +// try { +// File imageFile = new File(imageBaseDirectory + folder + File.separator + fileName); +// +// if (imageFile.exists() && imageFile.isFile()) { +// Resource resource = new FileSystemResource(imageFile); +// return ResponseEntity.ok() +// .contentLength(imageFile.length()) +// .contentType(MediaType.IMAGE_PNG) // Set appropriate content type +// // (e.g., IMAGE_PNG, IMAGE_JPEG) +// .body(resource); +// } +// else { +// // Handle resource not found error +// return ResponseEntity.notFound().build(); +// } +// } +// catch (Exception e) { +// // Handle exceptions, e.g., file not found +// return ResponseEntity.status(500).build(); +// } +// } +// +// @PostMapping("/api/image/upload") +// public ResponseEntity uploadImage(@RequestParam String folder, @RequestPart MultipartFile image) throws IOException { +// return ResponseEntity.ok(folder + "/" + fileService.storeFile(image, folder)); +// } +// +//} diff --git a/app/backend/src/main/java/com/app/gamereview/service/GroupService.java b/app/backend/src/main/java/com/app/gamereview/service/GroupService.java index e78333a5..551af5d6 100644 --- a/app/backend/src/main/java/com/app/gamereview/service/GroupService.java +++ b/app/backend/src/main/java/com/app/gamereview/service/GroupService.java @@ -639,29 +639,65 @@ private GroupApplicationResponseDto contertToDto(GroupApplication application) { return dto; } - public List getRecommendedGroups(User user) { - Optional findProfile = profileRepository.findByUserIdAndIsDeletedFalse(user.getId()); + public List getRecommendedGroups(String email) { + Optional optUser = userRepository.findByEmailAndIsDeletedFalse(email); + + if(optUser.isEmpty()){ + Query query = new Query(); + query.addCriteria(Criteria.where("isDeleted").is(false)); + query.limit(10); + return mongoTemplate.find(query, Group.class); + } + User user = optUser.get(); + Optional findProfile = profileRepository.findByUserIdAndIsDeletedFalse(user.getId()); if (findProfile.isEmpty()) { throw new ResourceNotFoundException("Profile of the user is not found, unexpected error has occurred"); } - + List recommendations = new ArrayList<>(); List memberGroups = groupRepository.findUserGroups(user.getId()); + List userGames = findProfile.get().getGames(); if (memberGroups.isEmpty()) { - return Collections.emptyList(); + if (userGames.isEmpty()){ + Query query = new Query(); + query.addCriteria(Criteria.where("isDeleted").is(false)); + query.limit(10); + return mongoTemplate.find(query, Group.class); + }else{ + List recommendationGameGroups = new ArrayList<>(); + for(String gameId : userGames){ + Query query = new Query(); + query.addCriteria(Criteria.where("isDeleted").is(false)); + query.addCriteria(Criteria.where("gameId").is(gameId)); + query.limit(2); + List gameGroups = mongoTemplate.find(query, Group.class); + recommendationGameGroups.addAll(gameGroups); + } + return recommendationGameGroups; + } } TreeSet recommendedGroups = new TreeSet<>(Comparator.reverseOrder()); for (Group group : memberGroups) { String groupId = group.getId(); recommendedGroups.addAll(recommendationByGroupId(groupId)); } - - List recommendations = new ArrayList<>(); - + if(!userGames.isEmpty()){ + List recommendedGameGroups = new ArrayList<>(); + for(String gameId : userGames){ + Query query = new Query(); + query.addCriteria(Criteria.where("isDeleted").is(false)); + query.addCriteria(Criteria.where("gameId").is(gameId)); + query.limit(2); + List gameGroups = mongoTemplate.find(query, Group.class); + recommendedGameGroups.addAll(gameGroups); + } + recommendations.addAll(recommendedGameGroups); + } for (RecommendGroupDto groupDto : recommendedGroups) { recommendations.add(groupDto.getGroup()); } + return recommendations; } @@ -696,7 +732,7 @@ public TreeSet recommendationByGroupId(String groupId) { } } - Query allGroupsQuery = new Query(); // all games except the base game + Query allGroupsQuery = new Query(); allGroupsQuery.addCriteria(Criteria.where("isDeleted").is(false)); allGroupsQuery.addCriteria(Criteria.where("_id").nin(idList)); diff --git a/app/backend/src/main/resources/.env.example b/app/backend/src/main/resources/.env.example index 6eb48ecf..c67b54dd 100644 --- a/app/backend/src/main/resources/.env.example +++ b/app/backend/src/main/resources/.env.example @@ -1,10 +1,12 @@ -MONGO_DATABASE= -MONGO_USER= -MONGO_PASSWORD= -MONGO_CLUSTER= -MAIL_HOST= -MAIL_PORT= -MAIL_USERNAME= -MAIL_PASSWORD= -MAIL_AUTH= -MAIL_STARTTLS= \ No newline at end of file +MONGO_DATABASE="game-review-dev" +MONGO_USER="uzdurancan" +MONGO_PASSWORD="kQXN23hjiDo8WroF" +MONGO_CLUSTER="bounswe-group5-cluster.1uup3ah.mongodb.net" +SECRET_KEY="bounswe2023group5gamereview" +MAIL_HOST="smtp.gmail.com" +MAIL_PORT="587" +MAIL_USERNAME="bounswe2023.group5@gmail.com" +MAIL_PASSWORD="eevcddbrjtikrtcn" +MAIL_AUTH="true" +MAIL_STARTTLS="true" +IMAGE_DIR = "" \ No newline at end of file From a62f4ad21e919229fea6ef74a0eef5df523a99a8 Mon Sep 17 00:00:00 2001 From: zeynep-baydemir Date: Sat, 23 Dec 2023 18:07:03 +0300 Subject: [PATCH 2/2] fix --- .../gamereview/config/SpringdocConfig.java | 36 +++--- .../controller/ImageController.java | 112 +++++++++--------- app/backend/src/main/resources/.env.example | 22 ++-- 3 files changed, 84 insertions(+), 86 deletions(-) diff --git a/app/backend/src/main/java/com/app/gamereview/config/SpringdocConfig.java b/app/backend/src/main/java/com/app/gamereview/config/SpringdocConfig.java index 2f9679fc..8109ede7 100644 --- a/app/backend/src/main/java/com/app/gamereview/config/SpringdocConfig.java +++ b/app/backend/src/main/java/com/app/gamereview/config/SpringdocConfig.java @@ -1,18 +1,18 @@ -//package com.app.gamereview.config; -// -//import io.swagger.v3.oas.annotations.OpenAPIDefinition; -//import io.swagger.v3.oas.models.OpenAPI; -//import io.swagger.v3.oas.models.info.Info; -//import org.springframework.context.annotation.Bean; -//import org.springframework.context.annotation.Configuration; -// -//@OpenAPIDefinition -//@Configuration -//public class SpringdocConfig { -// -// @Bean -// public OpenAPI baseOpenAPI() { -// return new OpenAPI().info(new Info().title("Spring Doc").version("1.0.0").description("Spring doc")); -// } -// -//} +package com.app.gamereview.config; + +import io.swagger.v3.oas.annotations.OpenAPIDefinition; +import io.swagger.v3.oas.models.OpenAPI; +import io.swagger.v3.oas.models.info.Info; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; + +@OpenAPIDefinition +@Configuration +public class SpringdocConfig { + + @Bean + public OpenAPI baseOpenAPI() { + return new OpenAPI().info(new Info().title("Spring Doc").version("1.0.0").description("Spring doc")); + } + +} diff --git a/app/backend/src/main/java/com/app/gamereview/controller/ImageController.java b/app/backend/src/main/java/com/app/gamereview/controller/ImageController.java index 10d420f8..e8036fda 100644 --- a/app/backend/src/main/java/com/app/gamereview/controller/ImageController.java +++ b/app/backend/src/main/java/com/app/gamereview/controller/ImageController.java @@ -1,56 +1,56 @@ -//package com.app.gamereview.controller; -// -//import com.app.gamereview.service.FileStorageService; -//import org.springframework.beans.factory.annotation.Value; -//import org.springframework.core.io.FileSystemResource; -//import org.springframework.http.MediaType; -//import org.springframework.http.ResponseEntity; -//import org.springframework.stereotype.Controller; -//import org.springframework.web.bind.annotation.*; -//import org.springframework.core.io.Resource; -//import org.springframework.web.multipart.MultipartFile; -// -//import java.io.File; -//import java.io.IOException; -// -//@RestController -//public class ImageController { -// -// private final FileStorageService fileService; -// @Value("${image.base-directory}") -// private String imageBaseDirectory; -// -// public ImageController(FileStorageService fileService) { -// this.fileService = fileService; -// } -// -// @GetMapping("/api/{folder}/{fileName:.+}") -// public ResponseEntity serveImage(@PathVariable String folder, @PathVariable String fileName) { -// try { -// File imageFile = new File(imageBaseDirectory + folder + File.separator + fileName); -// -// if (imageFile.exists() && imageFile.isFile()) { -// Resource resource = new FileSystemResource(imageFile); -// return ResponseEntity.ok() -// .contentLength(imageFile.length()) -// .contentType(MediaType.IMAGE_PNG) // Set appropriate content type -// // (e.g., IMAGE_PNG, IMAGE_JPEG) -// .body(resource); -// } -// else { -// // Handle resource not found error -// return ResponseEntity.notFound().build(); -// } -// } -// catch (Exception e) { -// // Handle exceptions, e.g., file not found -// return ResponseEntity.status(500).build(); -// } -// } -// -// @PostMapping("/api/image/upload") -// public ResponseEntity uploadImage(@RequestParam String folder, @RequestPart MultipartFile image) throws IOException { -// return ResponseEntity.ok(folder + "/" + fileService.storeFile(image, folder)); -// } -// -//} +package com.app.gamereview.controller; + +import com.app.gamereview.service.FileStorageService; +import org.springframework.beans.factory.annotation.Value; +import org.springframework.core.io.FileSystemResource; +import org.springframework.http.MediaType; +import org.springframework.http.ResponseEntity; +import org.springframework.stereotype.Controller; +import org.springframework.web.bind.annotation.*; +import org.springframework.core.io.Resource; +import org.springframework.web.multipart.MultipartFile; + +import java.io.File; +import java.io.IOException; + +@RestController +public class ImageController { + + private final FileStorageService fileService; + @Value("${image.base-directory}") + private String imageBaseDirectory; + + public ImageController(FileStorageService fileService) { + this.fileService = fileService; + } + + @GetMapping("/api/{folder}/{fileName:.+}") + public ResponseEntity serveImage(@PathVariable String folder, @PathVariable String fileName) { + try { + File imageFile = new File(imageBaseDirectory + folder + File.separator + fileName); + + if (imageFile.exists() && imageFile.isFile()) { + Resource resource = new FileSystemResource(imageFile); + return ResponseEntity.ok() + .contentLength(imageFile.length()) + .contentType(MediaType.IMAGE_PNG) // Set appropriate content type + // (e.g., IMAGE_PNG, IMAGE_JPEG) + .body(resource); + } + else { + // Handle resource not found error + return ResponseEntity.notFound().build(); + } + } + catch (Exception e) { + // Handle exceptions, e.g., file not found + return ResponseEntity.status(500).build(); + } + } + + @PostMapping("/api/image/upload") + public ResponseEntity uploadImage(@RequestParam String folder, @RequestPart MultipartFile image) throws IOException { + return ResponseEntity.ok(folder + "/" + fileService.storeFile(image, folder)); + } + +} diff --git a/app/backend/src/main/resources/.env.example b/app/backend/src/main/resources/.env.example index c67b54dd..6eb48ecf 100644 --- a/app/backend/src/main/resources/.env.example +++ b/app/backend/src/main/resources/.env.example @@ -1,12 +1,10 @@ -MONGO_DATABASE="game-review-dev" -MONGO_USER="uzdurancan" -MONGO_PASSWORD="kQXN23hjiDo8WroF" -MONGO_CLUSTER="bounswe-group5-cluster.1uup3ah.mongodb.net" -SECRET_KEY="bounswe2023group5gamereview" -MAIL_HOST="smtp.gmail.com" -MAIL_PORT="587" -MAIL_USERNAME="bounswe2023.group5@gmail.com" -MAIL_PASSWORD="eevcddbrjtikrtcn" -MAIL_AUTH="true" -MAIL_STARTTLS="true" -IMAGE_DIR = "" \ No newline at end of file +MONGO_DATABASE= +MONGO_USER= +MONGO_PASSWORD= +MONGO_CLUSTER= +MAIL_HOST= +MAIL_PORT= +MAIL_USERNAME= +MAIL_PASSWORD= +MAIL_AUTH= +MAIL_STARTTLS= \ No newline at end of file