Class ChallengeController
java.lang.Object
org.ntnu.idi.idatt2106.sparesti.sparestibackend.controller.ChallengeController
@RestController
@CrossOrigin
@RequestMapping("/challenges")
public class ChallengeController
extends Object
Controller for managing endpoints for saving challenges
- Since:
- 22.4.24
- Version:
- 1.0
- Author:
- Yasin A.M.
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionorg.springframework.http.ResponseEntity
<ChallengeDTO> completeChallenge
(Long id, org.springframework.security.core.userdetails.UserDetails userDetails) Completes a user challengeorg.springframework.http.ResponseEntity
<ChallengeDTO> createChallenge
(ChallengeCreateDTO challengeCreateDTO, org.springframework.security.core.userdetails.UserDetails userDetails) Creates a saving challengeorg.springframework.http.ResponseEntity
<Void> deleteChallenge
(@NotNull Long id, org.springframework.security.core.userdetails.UserDetails userDetails) Deletes a challengeorg.springframework.http.ResponseEntity
<org.springframework.core.io.Resource> Gets the image of a challengeorg.springframework.http.ResponseEntity
<List<ChallengeDTO>> generateChallenges
(org.springframework.security.core.userdetails.UserDetails userDetails) Generates a list of challenges based on user's configorg.springframework.http.ResponseEntity
<org.springframework.data.domain.Page<ChallengeDTO>> getActiveChallenges
(org.springframework.data.domain.Pageable pageable, org.springframework.security.core.userdetails.UserDetails userDetails) Gets a page of a user's active challengesorg.springframework.http.ResponseEntity
<org.springframework.data.domain.Page<ChallengeDTO>> getCompletedChallenges
(org.springframework.data.domain.Pageable pageable, org.springframework.security.core.userdetails.UserDetails userDetails) Gets a list of completed challengesorg.springframework.http.ResponseEntity
<ChallengeDTO> getUserChallenge
(org.springframework.security.core.userdetails.UserDetails userDetails, Long id) Gets a specific user challengeorg.springframework.http.ResponseEntity
<org.springframework.data.domain.Page<ChallengeDTO>> getUserChallenges
(org.springframework.data.domain.Pageable pageable, org.springframework.security.core.userdetails.UserDetails userDetails) Gets a page of a user's saving challengesorg.springframework.http.ResponseEntity
<String> handleFileUpload
(String id, org.springframework.web.multipart.MultipartFile file, org.springframework.security.core.userdetails.UserDetails userDetails) Uploads a file to be used for a challengeorg.springframework.http.ResponseEntity
<ChallengeDTO> updateChallenge
(Long id, ChallengeUpdateDTO challengeUpdateDTO, org.springframework.security.core.userdetails.UserDetails userDetails) Updates a challenge
-
Constructor Details
-
ChallengeController
public ChallengeController()
-
-
Method Details
-
getUserChallenges
@GetMapping public org.springframework.http.ResponseEntity<org.springframework.data.domain.Page<ChallengeDTO>> getUserChallenges(org.springframework.data.domain.Pageable pageable, @AuthenticationPrincipal org.springframework.security.core.userdetails.UserDetails userDetails) throws ChallengeNotFoundException, UserNotFoundException Gets a page of a user's saving challenges- Parameters:
pageable
- The pageable object that configures the pageuserDetails
- Current user- Returns:
- A page of saving challenges
- Throws:
ChallengeNotFoundException
- If a challenge could not be foundUserNotFoundException
- if the user could not be found
-
getActiveChallenges
@GetMapping("/active") public org.springframework.http.ResponseEntity<org.springframework.data.domain.Page<ChallengeDTO>> getActiveChallenges(org.springframework.data.domain.Pageable pageable, @AuthenticationPrincipal org.springframework.security.core.userdetails.UserDetails userDetails) Gets a page of a user's active challenges- Parameters:
pageable
- Configuration parameters for the pageuserDetails
- Current user- Returns:
- Page of active challenges
-
getCompletedChallenges
@GetMapping("/completed") public org.springframework.http.ResponseEntity<org.springframework.data.domain.Page<ChallengeDTO>> getCompletedChallenges(org.springframework.data.domain.Pageable pageable, @AuthenticationPrincipal org.springframework.security.core.userdetails.UserDetails userDetails) Gets a list of completed challenges- Parameters:
pageable
- Configuration for page objectuserDetails
- Current user- Returns:
- List of completed challenges
-
getUserChallenge
@GetMapping("/{id}") public org.springframework.http.ResponseEntity<ChallengeDTO> getUserChallenge(@AuthenticationPrincipal org.springframework.security.core.userdetails.UserDetails userDetails, @PathVariable Long id) throws ChallengeNotFoundException, UserNotFoundException Gets a specific user challenge- Parameters:
userDetails
- Current userid
- Identifies a challenge- Returns:
- Wrapper for challenge info
- Throws:
ChallengeNotFoundException
- If challenge could not be foundUserNotFoundException
- If user could not be found
-
createChallenge
@PostMapping public org.springframework.http.ResponseEntity<ChallengeDTO> createChallenge(@RequestBody ChallengeCreateDTO challengeCreateDTO, @AuthenticationPrincipal org.springframework.security.core.userdetails.UserDetails userDetails) throws ChallengeNotFoundException, UserNotFoundException, BadInputException, ObjectNotValidException Creates a saving challenge- Parameters:
challengeCreateDTO
- Wrapper for saving challenge infouserDetails
- Current user- Returns:
- Wrapper for the created challenge data
- Throws:
ChallengeNotFoundException
- If challenge could not be foundUserNotFoundException
- If user could not be foundBadInputException
- If user input is invalidObjectNotValidException
- If ChallengeCreateDTO fields are invalid
-
completeChallenge
@PutMapping("/{id}/complete") public org.springframework.http.ResponseEntity<ChallengeDTO> completeChallenge(@PathVariable Long id, @AuthenticationPrincipal org.springframework.security.core.userdetails.UserDetails userDetails) Completes a user challenge- Parameters:
id
- Used to identify the challengeuserDetails
- Current user- Returns:
- The completed challenge
-
updateChallenge
@PutMapping("/{id}") public org.springframework.http.ResponseEntity<ChallengeDTO> updateChallenge(@PathVariable Long id, @RequestBody ChallengeUpdateDTO challengeUpdateDTO, @AuthenticationPrincipal org.springframework.security.core.userdetails.UserDetails userDetails) throws ChallengeNotFoundException, UserNotFoundException, BadInputException, ObjectNotValidException Updates a challenge- Parameters:
id
- Identifies the challengechallengeUpdateDTO
- Wrapper for new challenge infouserDetails
- Current user- Returns:
- Wrapper of the new challenge's data
- Throws:
ChallengeNotFoundException
- If challenge could not be foundUserNotFoundException
- If User could not be foundBadInputException
- On bad user inputObjectNotValidException
- If challengeUpdateDTO has invalid fields
-
deleteChallenge
@DeleteMapping("/{id}") public org.springframework.http.ResponseEntity<Void> deleteChallenge(@NotNull @PathVariable @NotNull Long id, @AuthenticationPrincipal org.springframework.security.core.userdetails.UserDetails userDetails) throws ChallengeNotFoundException, UserNotFoundException Deletes a challenge- Parameters:
id
- Identifies the challengeuserDetails
- Current user- Returns:
- Nada
- Throws:
ChallengeNotFoundException
- If challenge could not be foundUserNotFoundException
- If user could not be found
-
generateChallenges
@GetMapping("/generate") public org.springframework.http.ResponseEntity<List<ChallengeDTO>> generateChallenges(@AuthenticationPrincipal org.springframework.security.core.userdetails.UserDetails userDetails) Generates a list of challenges based on user's config- Parameters:
userDetails
- Current user- Returns:
- List of generated challenges
-
handleFileUpload
@PostMapping("/picture") public org.springframework.http.ResponseEntity<String> handleFileUpload(@RequestParam String id, @RequestParam("file") org.springframework.web.multipart.MultipartFile file, @AuthenticationPrincipal org.springframework.security.core.userdetails.UserDetails userDetails) throws IOException Uploads a file to be used for a challenge- Parameters:
id
- Identifies the objectfile
- File to be uploadeduserDetails
- Current user- Returns:
- OK-string
- Throws:
IOException
- For IO-errors
-
findFile
@GetMapping("/picture") @ResponseBody public org.springframework.http.ResponseEntity<org.springframework.core.io.Resource> findFile(@RequestParam String id, @AuthenticationPrincipal org.springframework.security.core.userdetails.UserDetails userDetails) throws IOException Gets the image of a challenge- Parameters:
id
- Identifies challengeuserDetails
- Current user- Returns:
- Resource wrapper for image
- Throws:
IOException
- For IO-errors
-