java.lang.Object
org.ntnu.idi.idatt2106.sparesti.sparestibackend.service.ChallengeService

@Service public class ChallengeService extends Object
Service class for managing challenges. Provides functionality for creating, updating, retrieving, generating and deleting challenges, as well as managing challenge-related operations such as cascading saves to goals and updating user stats.
Author:
Y.A. Marouga, H.L Xu and L.M.L Nilsen
  • Constructor Details

    • ChallengeService

      public ChallengeService()
  • Method Details

    • save

      Creates and persists a new challenge based on provided DTO and user details. Calculates initial savings impact and completes the challenge if the saved amount meets the target.
      Parameters:
      challengeCreateDTO - DTO containing challenge creation details.
      user - The user creating the challenge.
      Returns:
      A ChallengeDTO representing the newly created challenge.
      Throws:
      ChallengeNotFoundException - if the challenge configuration is not found.
      ObjectNotValidException - if the challenge details are not valid as per validation constraints.
    • updateChallenge

      public ChallengeDTO updateChallenge(Long id, ChallengeUpdateDTO challengeUpdateDTO, User user) throws ChallengeNotFoundException, ObjectNotValidException
      Updates an existing challenge with new data from a provided DTO if the challenge has not been completed.
      Parameters:
      id - The ID of the challenge to update.
      challengeUpdateDTO - DTO containing update details.
      user - The user associated with the challenge.
      Returns:
      Updated ChallengeDTO.
      Throws:
      ChallengeNotFoundException - if the challenge cannot be found.
      ObjectNotValidException - if the update details are not valid.
      ChallengeAlreadyCompletedException - if the challenge is already completed.
    • getChallenge

      public ChallengeDTO getChallenge(Long challengeId, User user) throws ChallengeNotFoundException
      Retrieves a specific challenge by ID and user.
      Parameters:
      challengeId - The ID of the challenge.
      user - The user associated with the challenge.
      Returns:
      ChallengeDTO of the retrieved challenge.
      Throws:
      ChallengeNotFoundException - if no such challenge exists.
    • deleteChallenge

      public void deleteChallenge(Long challengeId, User user) throws ChallengeNotFoundException
      Deletes a challenge by ID and user, ensuring it is no longer present in the database.
      Parameters:
      challengeId - The ID of the challenge to delete.
      user - The user associated with the challenge.
      Throws:
      ChallengeNotFoundException - if no such challenge exists to delete.
    • getChallengesByUser

      public org.springframework.data.domain.Page<ChallengeDTO> getChallengesByUser(User user, org.springframework.data.domain.Pageable pageable) throws ChallengeNotFoundException
      Retrieves all challenges associated with a specific user and paginates the results.
      Parameters:
      user - The user whose challenges are to be retrieved.
      pageable - Pagination and sorting details.
      Returns:
      A page of ChallengeDTOs representing the user's challenges.
      Throws:
      ChallengeNotFoundException - If no challenges are found for the user, this exception may be thrown.
    • getActiveChallenges

      public org.springframework.data.domain.Page<ChallengeDTO> getActiveChallenges(User user, org.springframework.data.domain.Pageable pageable)
      Retrieves all active (not completed) challenges for a specific user and paginates the results.
      Parameters:
      user - The user whose active challenges are to be retrieved.
      pageable - Pagination and sorting details.
      Returns:
      A page of ChallengeDTOs of active challenges.
    • getCompletedChallenges

      public org.springframework.data.domain.Page<ChallengeDTO> getCompletedChallenges(User user, org.springframework.data.domain.Pageable pageable)
      Retrieves all completed challenges for a specific user and paginates the results.
      Parameters:
      user - The user whose completed challenges are to be retrieved.
      pageable - Pagination and sorting details.
      Returns:
      A page of ChallengeDTOs of completed challenges.
    • completeChallenge

      public ChallengeDTO completeChallenge(Long challengeId, User user)
      Marks a challenge as completed, updates user statistics such as saved amounts and streaks, and persists these changes to the database.
      Parameters:
      challengeId - The ID of the challenge to complete.
      user - The user completing the challenge.
      Returns:
      ChallengeDTO representing the completed challenge.
      Throws:
      ChallengeAlreadyCompletedException - If the challenge is already marked as completed.
    • getGeneratedChallenges

      public List<ChallengeDTO> getGeneratedChallenges(User user)
      Gets list of generated challenges
      Parameters:
      user - The user who challenges are generated for.
      Returns:
      List of generated challenges