diff --git a/src/main/java/ntnu/idatt2105/group44/trivioServer/controller/TrivioController.java b/src/main/java/ntnu/idatt2105/group44/trivioServer/controller/TrivioController.java index 92cf03262af231b7d0a18aa671f77a38cbd4e880..7538298171be53e98f91ee4dcab866bd0b5aacbf 100644 --- a/src/main/java/ntnu/idatt2105/group44/trivioServer/controller/TrivioController.java +++ b/src/main/java/ntnu/idatt2105/group44/trivioServer/controller/TrivioController.java @@ -1,5 +1,6 @@ package ntnu.idatt2105.group44.trivioServer.controller; +import java.util.Arrays; import java.util.List; import java.util.logging.Logger; @@ -32,78 +33,120 @@ public class TrivioController { public List<Trivio> getAllTrivio(){ return trivioService.getAllTrivios(); } -// -// /** -// * GET-method to retrieve all trivios by a specific user with filters if necessary. -// * @param token token to authenticate and identify the user. -// * @param category category for filtering the trivios. -// * @param difficulty difficulty for filtering the trivios. -// * @param tags tags for filtering the trivios. -// * @param pageable page of trivios to retrieve. -// * @return page with filtered trivios by the specific users. -// */ -// @GetMapping("/user") -// public ResponseEntity<Page<Trivio>> getFilteredTriviosByUser( -// @RequestHeader("Authorization") String token, -// @RequestParam(required = false) String category, -// @RequestParam(required = false) String difficulty, -// @RequestParam(required = false) List<String> tags, -// Pageable pageable) { -// -// Long userId = Long.parseLong(jwtService.extractSubject(token)); -// Page<Trivio> trivios = trivioService.getFilteredTriviosByUser( -// userId,category, difficulty, tags, pageable); -// return new ResponseEntity<>(trivios, HttpStatus.OK); -// } -// -// /** -// * GET-method to retrieve all public trivios by other users with filters if necessary. -// * @param token token to authenticate and identify the user. -// * @param category category for filtering the trivios. -// * @param difficulty difficulty for filtering the trivios. -// * @param tags tags for filtering the trivios. -// * @param pageable page of trivios to retrieve. -// * @return page with filtered public trivios by other users. -// */ -// @GetMapping("/discover") -// public ResponseEntity<Page<Trivio>> getFilteredPublicTriviosByOtherUsers( -// @RequestHeader("Authorization") String token, -// @RequestParam(required = false) String category, -// @RequestParam(required = false) String difficulty, -// @RequestParam(required = false) List<String> tags, -// Pageable pageable) { -// -// Long userId = Long.parseLong(jwtService.extractSubject(token)); -// String visibility = "public"; -// -// Page<Trivio> trivios = trivioService.getFilteredPublicTriviosByOtherUsers( -// userId,category, difficulty, tags, visibility,pageable); -// return new ResponseEntity<>(trivios, HttpStatus.OK); -// } - @GetMapping(path = "/discovery") - public ResponseEntity<List<Trivio>> getAllPublicTriviosFromOtherUsers(@RequestHeader("Authorization") String token) { - try { - long userId = Long.parseLong(jwtService.extractSubjectFromHeader(token)); - List<Trivio> trivios = trivioService.getAllPublicTriviosFromOtherUsers(userId); - return ResponseEntity.ok(trivios); - } catch (Exception e) { - // Handle any exceptions (e.g., token parsing errors) - return ResponseEntity.status(HttpStatus.UNAUTHORIZED).build(); + /** + * GET-method to retrieve all trivios by a specific user with filters if necessary. + * @param token token to authenticate and identify the user. + * @param category category for filtering the trivios. + * @param difficulty difficulty for filtering the trivios. + * @param tagString tags for filtering the trivios. + * @param pageable page of trivios to retrieve. + * @return page with filtered trivios by the specific users. + */ + @GetMapping("/user") + public ResponseEntity<Page<Trivio>> getFilteredTriviosByUser( + @RequestHeader("Authorization") String token, + @RequestParam(required = false) String category, + @RequestParam(required = false) String difficulty, + @RequestParam(required = false) String tagString, + Pageable pageable) { + + List<String> tags = null; + if (tagString != null) { + tags = Arrays.asList(tagString.split(",")); + logger.info(tags.toString()); } + + logger.info("ok"); + long userId = Long.parseLong(jwtService.extractSubjectFromHeader(token)); + logger.info("ok"); + Page<Trivio> trivios = trivioService.getFilteredTriviosByUser( + userId,category, difficulty, tags, pageable); + logger.info("ok"); + return new ResponseEntity<>(trivios, HttpStatus.OK); } - @GetMapping(path = "/user") - public ResponseEntity<List<Trivio>> getTriviosByUserID(@RequestHeader("Authorization") String token) { - try { - long userId = Long.parseLong(jwtService.extractSubjectFromHeader(token)); - List<Trivio> trivios = trivioService.getTriviosByUserId(userId); - return ResponseEntity.ok(trivios); - } catch (Exception e) { - return ResponseEntity.status(HttpStatus.UNAUTHORIZED).build(); + /** + * GET-method to retrieve all public trivios by other users with filters if necessary. + * @param token token to authenticate and identify the user. + * @param category category for filtering the trivios. + * @param difficulty difficulty for filtering the trivios. + * @param tagString tags for filtering the trivios. + * @param pageable page of trivios to retrieve. + * @return page with filtered public trivios by other users. + */ + @GetMapping("/discover") + public ResponseEntity<Page<Trivio>> getFilteredPublicTriviosByOtherUsers( + @RequestHeader("Authorization") String token, + @RequestParam(required = false) String category, + @RequestParam(required = false) String difficulty, + @RequestParam(required = false) String tagString, + Pageable pageable) { + + long userId = Long.parseLong(jwtService.extractSubjectFromHeader(token)); + String visibility = "public"; + logger.info(tagString); + logger.info(category); + + List<String> tags = null; + if (tagString != null) { + tags = Arrays.asList(tagString.split(",")); + logger.info(tags.toString()); } + + Page<Trivio> trivios = trivioService.getFilteredPublicTriviosByOtherUsers( + userId,category, difficulty, tags, visibility,pageable); + return new ResponseEntity<>(trivios, HttpStatus.OK); } + @GetMapping("/shared") + public ResponseEntity<Page<Trivio>> getSharedTrivios( + @RequestHeader("Authorization") String token, + @RequestParam(required = false) String category, + @RequestParam(required = false) String difficulty, + @RequestParam(required = false) String tagString, + Pageable pageable) { + + long userId = Long.parseLong(jwtService.extractSubjectFromHeader(token)); + logger.info(tagString); + logger.info(category); + + List<String> tags = null; + if (tagString != null) { + tags = Arrays.asList(tagString.split(",")); + logger.info(tags.toString()); + } + + Page<Trivio> trivios = trivioService.getFilteredSharedTriviosByUser( + userId,category,difficulty, tags,pageable); + return new ResponseEntity<>(trivios, HttpStatus.OK); + } + + +// +// @GetMapping(path = "/discovery") +// public ResponseEntity<List<Trivio>> getAllPublicTriviosFromOtherUsers(@RequestHeader("Authorization") String token) { +// try { +// long userId = Long.parseLong(jwtService.extractSubjectFromHeader(token)); +// List<Trivio> trivios = trivioService.getAllPublicTriviosFromOtherUsers(userId); +// return ResponseEntity.ok(trivios); +// } catch (Exception e) { +// // Handle any exceptions (e.g., token parsing errors) +// return ResponseEntity.status(HttpStatus.UNAUTHORIZED).build(); +// } +// } +// +// @GetMapping(path = "/user") +// public ResponseEntity<List<Trivio>> getTriviosByUserID(@RequestHeader("Authorization") String token) { +// try { +// long userId = Long.parseLong(jwtService.extractSubjectFromHeader(token)); +// List<Trivio> trivios = trivioService.getTriviosByUserId(userId); +// return ResponseEntity.ok(trivios); +// } catch (Exception e) { +// return ResponseEntity.status(HttpStatus.UNAUTHORIZED).build(); +// } +// } + @GetMapping(path = "{trivioId}") public ResponseEntity<Trivio> getTrivio(@RequestHeader("Authorization") String token, @PathVariable Long trivioId ){ try { diff --git a/src/main/java/ntnu/idatt2105/group44/trivioServer/service/TrivioService.java b/src/main/java/ntnu/idatt2105/group44/trivioServer/service/TrivioService.java index a886179812e1c8f83b925b8a876b89dae8bdea0e..3028f03a4dcdaa2b58675c1a5231ea5038799f92 100644 --- a/src/main/java/ntnu/idatt2105/group44/trivioServer/service/TrivioService.java +++ b/src/main/java/ntnu/idatt2105/group44/trivioServer/service/TrivioService.java @@ -252,6 +252,27 @@ public class TrivioService { return trivioRepository.findAll(spec, pageable); } + + public Page<Trivio> getFilteredSharedTriviosByUser(Long userId, String category, String difficulty, List<String> tags, Pageable pageable) { + Specification<Trivio> spec = TrivioSpecifications.filterByUserInSharedList(userId); + + //If no category is chosen + if (category != null) { + spec = spec.and(TrivioSpecifications.filterByCategory(category)); + } + + //If no difficulty is chosen + if (difficulty != null) { + spec = spec.and(TrivioSpecifications.filterByDifficulty(difficulty)); + } + + //If no tags are chosen. + if (tags != null && !tags.isEmpty()) { + spec = spec.and(TrivioSpecifications.filterByTags(tags)); + } + + return trivioRepository.findAll(spec, pageable); + } } diff --git a/src/main/java/ntnu/idatt2105/group44/trivioServer/service/TrivioSpecifications.java b/src/main/java/ntnu/idatt2105/group44/trivioServer/service/TrivioSpecifications.java index a7d361b2b7892bea887f145dd86fa957f4696cef..4a14ce6e76b065d9ce3e0990e7d88d3170f45e88 100644 --- a/src/main/java/ntnu/idatt2105/group44/trivioServer/service/TrivioSpecifications.java +++ b/src/main/java/ntnu/idatt2105/group44/trivioServer/service/TrivioSpecifications.java @@ -14,12 +14,26 @@ public class TrivioSpecifications { criteriaBuilder.equal(root.get("user").get("id"), userId); } - //Method to filter out trivios by user-id + //Method to filter out user-id public static Specification<Trivio> filterByNotUserId(Long userId) { return (root, query, criteriaBuilder) -> criteriaBuilder.notEqual(root.get("user").get("id"), userId); } + // Method to filter trivios by user being in the shared list + public static Specification<Trivio> filterByUserInSharedList(Long userId) { + return (root, query, criteriaBuilder) -> { + // Join the Trivio entity with the usersThatCanEdit collection + Join<Object, Object> usersJoin = root.join("usersThatCanEdit", JoinType.INNER); + + // Create a predicate to check if the user ID matches + Predicate userInSharedListPredicate = criteriaBuilder.equal(usersJoin.get("id"), userId); + + // Return the predicate + return userInSharedListPredicate; + }; + } + //Method to filter trivios by visibility public static Specification<Trivio> filterByVisibility(String visibility){ return (root, query, criteriaBuilder) -> @@ -60,7 +74,6 @@ public class TrivioSpecifications { )); tagPredicates[i] = criteriaBuilder.greaterThan(subquery, 0L); } - // Combine all tag predicates with AND operator return criteriaBuilder.and(tagPredicates); };