Skip to content
Snippets Groups Projects

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

Merged Vilde Min Vikan requested to merge updateMethods into main
3 files
+ 144
67
Compare changes
  • Side-by-side
  • Inline
Files
3
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 {
Loading