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
    Constructor
    Description
     
  • Method Summary

    Modifier and Type
    Method
    Description
    org.springframework.http.ResponseEntity<ChallengeDTO>
    completeChallenge(Long id, org.springframework.security.core.userdetails.UserDetails userDetails)
    Completes a user challenge
    org.springframework.http.ResponseEntity<ChallengeDTO>
    createChallenge(ChallengeCreateDTO challengeCreateDTO, org.springframework.security.core.userdetails.UserDetails userDetails)
    Creates a saving challenge
    org.springframework.http.ResponseEntity<Void>
    deleteChallenge(@NotNull Long id, org.springframework.security.core.userdetails.UserDetails userDetails)
    Deletes a challenge
    org.springframework.http.ResponseEntity<org.springframework.core.io.Resource>
    findFile(String id, org.springframework.security.core.userdetails.UserDetails userDetails)
    Gets the image of a challenge
    org.springframework.http.ResponseEntity<List<ChallengeDTO>>
    generateChallenges(org.springframework.security.core.userdetails.UserDetails userDetails)
    Generates a list of challenges based on user's config
    org.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 challenges
    org.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 challenges
    org.springframework.http.ResponseEntity<ChallengeDTO>
    getUserChallenge(org.springframework.security.core.userdetails.UserDetails userDetails, Long id)
    Gets a specific user challenge
    org.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 challenges
    org.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 challenge
    org.springframework.http.ResponseEntity<ChallengeDTO>
    updateChallenge(Long id, ChallengeUpdateDTO challengeUpdateDTO, org.springframework.security.core.userdetails.UserDetails userDetails)
    Updates a challenge

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • 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 page
      userDetails - Current user
      Returns:
      A page of saving challenges
      Throws:
      ChallengeNotFoundException - If a challenge could not be found
      UserNotFoundException - 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 page
      userDetails - 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 object
      userDetails - 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 user
      id - Identifies a challenge
      Returns:
      Wrapper for challenge info
      Throws:
      ChallengeNotFoundException - If challenge could not be found
      UserNotFoundException - 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 info
      userDetails - Current user
      Returns:
      Wrapper for the created challenge data
      Throws:
      ChallengeNotFoundException - If challenge could not be found
      UserNotFoundException - If user could not be found
      BadInputException - If user input is invalid
      ObjectNotValidException - 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 challenge
      userDetails - 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 challenge
      challengeUpdateDTO - Wrapper for new challenge info
      userDetails - Current user
      Returns:
      Wrapper of the new challenge's data
      Throws:
      ChallengeNotFoundException - If challenge could not be found
      UserNotFoundException - If User could not be found
      BadInputException - On bad user input
      ObjectNotValidException - 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 challenge
      userDetails - Current user
      Returns:
      Nada
      Throws:
      ChallengeNotFoundException - If challenge could not be found
      UserNotFoundException - 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 object
      file - File to be uploaded
      userDetails - 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 challenge
      userDetails - Current user
      Returns:
      Resource wrapper for image
      Throws:
      IOException - For IO-errors