Skip to content
Snippets Groups Projects
Commit a04b373b authored by Vilde Min Vikan's avatar Vilde Min Vikan
Browse files

Merge branch 'updateMethods' into 'main'

Updated and finalized GET-methods for trivio retrieval and added a new end points.

See merge request !4
parents b8d26900 32623dfb
Branches
No related tags found
1 merge request!4Updated and finalized GET-methods for trivio retrieval and added a new end points.
package ntnu.idatt2105.group44.trivioServer.controller; package ntnu.idatt2105.group44.trivioServer.controller;
import java.util.Arrays;
import java.util.List; import java.util.List;
import java.util.logging.Logger; import java.util.logging.Logger;
...@@ -32,78 +33,120 @@ public class TrivioController { ...@@ -32,78 +33,120 @@ public class TrivioController {
public List<Trivio> getAllTrivio(){ public List<Trivio> getAllTrivio(){
return trivioService.getAllTrivios(); 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) { * GET-method to retrieve all trivios by a specific user with filters if necessary.
try { * @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)); long userId = Long.parseLong(jwtService.extractSubjectFromHeader(token));
List<Trivio> trivios = trivioService.getAllPublicTriviosFromOtherUsers(userId); logger.info("ok");
return ResponseEntity.ok(trivios); Page<Trivio> trivios = trivioService.getFilteredTriviosByUser(
} catch (Exception e) { userId,category, difficulty, tags, pageable);
// Handle any exceptions (e.g., token parsing errors) logger.info("ok");
return ResponseEntity.status(HttpStatus.UNAUTHORIZED).build(); 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 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(path = "/user") @GetMapping("/shared")
public ResponseEntity<List<Trivio>> getTriviosByUserID(@RequestHeader("Authorization") String token) { public ResponseEntity<Page<Trivio>> getSharedTrivios(
try { @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)); long userId = Long.parseLong(jwtService.extractSubjectFromHeader(token));
List<Trivio> trivios = trivioService.getTriviosByUserId(userId); logger.info(tagString);
return ResponseEntity.ok(trivios); logger.info(category);
} catch (Exception e) {
return ResponseEntity.status(HttpStatus.UNAUTHORIZED).build(); 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}") @GetMapping(path = "{trivioId}")
public ResponseEntity<Trivio> getTrivio(@RequestHeader("Authorization") String token, @PathVariable Long trivioId ){ public ResponseEntity<Trivio> getTrivio(@RequestHeader("Authorization") String token, @PathVariable Long trivioId ){
try { try {
......
...@@ -252,6 +252,27 @@ public class TrivioService { ...@@ -252,6 +252,27 @@ public class TrivioService {
return trivioRepository.findAll(spec, pageable); 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);
}
} }
...@@ -14,12 +14,26 @@ public class TrivioSpecifications { ...@@ -14,12 +14,26 @@ public class TrivioSpecifications {
criteriaBuilder.equal(root.get("user").get("id"), userId); 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) { public static Specification<Trivio> filterByNotUserId(Long userId) {
return (root, query, criteriaBuilder) -> return (root, query, criteriaBuilder) ->
criteriaBuilder.notEqual(root.get("user").get("id"), userId); 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 //Method to filter trivios by visibility
public static Specification<Trivio> filterByVisibility(String visibility){ public static Specification<Trivio> filterByVisibility(String visibility){
return (root, query, criteriaBuilder) -> return (root, query, criteriaBuilder) ->
...@@ -60,7 +74,6 @@ public class TrivioSpecifications { ...@@ -60,7 +74,6 @@ public class TrivioSpecifications {
)); ));
tagPredicates[i] = criteriaBuilder.greaterThan(subquery, 0L); tagPredicates[i] = criteriaBuilder.greaterThan(subquery, 0L);
} }
// Combine all tag predicates with AND operator // Combine all tag predicates with AND operator
return criteriaBuilder.and(tagPredicates); return criteriaBuilder.and(tagPredicates);
}; };
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment