Skip to content
Snippets Groups Projects
Commit 32623dfb authored by vildemv's avatar vildemv
Browse files

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

Updated and finalized GET-methods for trivio retrieval and added a new end point. Did also update the service layer accordingly.
parent 30d34aa7
No related branches found
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;
...@@ -38,7 +39,7 @@ public class TrivioController { ...@@ -38,7 +39,7 @@ public class TrivioController {
* @param token token to authenticate and identify the user. * @param token token to authenticate and identify the user.
* @param category category for filtering the trivios. * @param category category for filtering the trivios.
* @param difficulty difficulty for filtering the trivios. * @param difficulty difficulty for filtering the trivios.
* @param tags tags for filtering the trivios. * @param tagString tags for filtering the trivios.
* @param pageable page of trivios to retrieve. * @param pageable page of trivios to retrieve.
* @return page with filtered trivios by the specific users. * @return page with filtered trivios by the specific users.
*/ */
...@@ -47,13 +48,21 @@ public class TrivioController { ...@@ -47,13 +48,21 @@ public class TrivioController {
@RequestHeader("Authorization") String token, @RequestHeader("Authorization") String token,
@RequestParam(required = false) String category, @RequestParam(required = false) String category,
@RequestParam(required = false) String difficulty, @RequestParam(required = false) String difficulty,
@RequestParam(required = false) List<String> tags, @RequestParam(required = false) String tagString,
Pageable pageable) { Pageable pageable) {
List<String> tags = null;
if (tagString != null) {
tags = Arrays.asList(tagString.split(","));
logger.info(tags.toString());
}
logger.info("ok"); logger.info("ok");
long userId = Long.parseLong(jwtService.extractSubjectFromHeader(token)); long userId = Long.parseLong(jwtService.extractSubjectFromHeader(token));
logger.info("ok"); logger.info("ok");
Page<Trivio> trivios = trivioService.getFilteredTriviosByUser( Page<Trivio> trivios = trivioService.getFilteredTriviosByUser(
userId,category, difficulty, tags, pageable); userId,category, difficulty, tags, pageable);
logger.info("ok");
return new ResponseEntity<>(trivios, HttpStatus.OK); return new ResponseEntity<>(trivios, HttpStatus.OK);
} }
...@@ -62,7 +71,7 @@ public class TrivioController { ...@@ -62,7 +71,7 @@ public class TrivioController {
* @param token token to authenticate and identify the user. * @param token token to authenticate and identify the user.
* @param category category for filtering the trivios. * @param category category for filtering the trivios.
* @param difficulty difficulty for filtering the trivios. * @param difficulty difficulty for filtering the trivios.
* @param tags tags for filtering the trivios. * @param tagString tags for filtering the trivios.
* @param pageable page of trivios to retrieve. * @param pageable page of trivios to retrieve.
* @return page with filtered public trivios by other users. * @return page with filtered public trivios by other users.
*/ */
...@@ -71,16 +80,49 @@ public class TrivioController { ...@@ -71,16 +80,49 @@ public class TrivioController {
@RequestHeader("Authorization") String token, @RequestHeader("Authorization") String token,
@RequestParam(required = false) String category, @RequestParam(required = false) String category,
@RequestParam(required = false) String difficulty, @RequestParam(required = false) String difficulty,
@RequestParam(required = false) List<String> tags, @RequestParam(required = false) String tagString,
Pageable pageable) { Pageable pageable) {
long userId = Long.parseLong(jwtService.extractSubjectFromHeader(token)); long userId = Long.parseLong(jwtService.extractSubjectFromHeader(token));
String visibility = "public"; 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( Page<Trivio> trivios = trivioService.getFilteredPublicTriviosByOtherUsers(
userId,category, difficulty, tags, visibility,pageable); userId,category, difficulty, tags, visibility,pageable);
return new ResponseEntity<>(trivios, HttpStatus.OK); 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") // @GetMapping(path = "/discovery")
// public ResponseEntity<List<Trivio>> getAllPublicTriviosFromOtherUsers(@RequestHeader("Authorization") String token) { // public ResponseEntity<List<Trivio>> getAllPublicTriviosFromOtherUsers(@RequestHeader("Authorization") String token) {
......
...@@ -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);
}
} }
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment