diff --git a/src/main/java/idatt2106/systemutvikling/sparesti/controller/AchievementController.java b/src/main/java/idatt2106/systemutvikling/sparesti/controller/AchievementController.java index 2fe04cfb1d869a2cf48367399af25810421926bf..3c96ee5adb6714797cf9160bea1bee9514b9aae8 100644 --- a/src/main/java/idatt2106/systemutvikling/sparesti/controller/AchievementController.java +++ b/src/main/java/idatt2106/systemutvikling/sparesti/controller/AchievementController.java @@ -3,6 +3,10 @@ package idatt2106.systemutvikling.sparesti.controller; import idatt2106.systemutvikling.sparesti.dto.AchievementDTO; import idatt2106.systemutvikling.sparesti.service.AchievementService; import idatt2106.systemutvikling.sparesti.service.CurrentUserService; +import io.swagger.v3.oas.annotations.Operation; +import io.swagger.v3.oas.annotations.media.Content; +import io.swagger.v3.oas.annotations.media.Schema; +import io.swagger.v3.oas.annotations.responses.ApiResponse; import lombok.AllArgsConstructor; import org.springframework.http.ResponseEntity; import org.springframework.web.bind.annotation.GetMapping; @@ -23,26 +27,40 @@ public class AchievementController { private final Logger logger = Logger.getLogger(AchievementController.class.getName()); private final AchievementService achievementService; - /** - * Method for getting all achievements that have yet to be completed by the user. - * - * @return all locked achievements - */ + @Operation( + summary = "Get all locked achievements", + description = "Get all locked achievements for the current user." + ) + @ApiResponse( + responseCode = "200", + description = "Locked achievements returned", + content = { + @Content(mediaType = "application/json", + schema = @Schema(implementation = AchievementDTO.class)) + } + ) @GetMapping("/locked") public ResponseEntity<List<AchievementDTO>> getLockedAchievements() { logger.info("Received request to get locked achievements."); return ResponseEntity.ok( - achievementService.getLockedAchievementsAsDTOS(CurrentUserService.getCurrentUsername())); + achievementService.getLockedAchievementsAsDTOS(CurrentUserService.getCurrentUsername())); } - /** - * Method for getting all achievements that have been completed by the user. - * - * @return all unlocked achievements - */ + @Operation( + summary = "Get all unlocked achievements", + description = "Get all unlocked achievements for the current user." + ) + @ApiResponse( + responseCode = "200", + description = "Unlocked achievements returned", + content = { + @Content(mediaType = "application/json", + schema = @Schema(implementation = AchievementDTO.class)) + } + ) @GetMapping("/newUnlocked") public ResponseEntity<List<AchievementDTO>> getNewUnlockedAchievements() { return ResponseEntity.ok( - achievementService.checkForUnlockedAchievements(CurrentUserService.getCurrentUsername())); + achievementService.checkForUnlockedAchievements(CurrentUserService.getCurrentUsername())); } } diff --git a/src/main/java/idatt2106/systemutvikling/sparesti/controller/BankAccountController.java b/src/main/java/idatt2106/systemutvikling/sparesti/controller/BankAccountController.java index 98965dfc78d889901dc79c67afd3c393fee02ad8..fb70eca4212801d28d02e000e01d3d4c0237f2fd 100644 --- a/src/main/java/idatt2106/systemutvikling/sparesti/controller/BankAccountController.java +++ b/src/main/java/idatt2106/systemutvikling/sparesti/controller/BankAccountController.java @@ -6,6 +6,11 @@ import idatt2106.systemutvikling.sparesti.mapper.BankAccountMapper; import idatt2106.systemutvikling.sparesti.service.BankAccountService; import idatt2106.systemutvikling.sparesti.model.BankAccount; import java.util.logging.Logger; + +import io.swagger.v3.oas.annotations.Operation; +import io.swagger.v3.oas.annotations.media.Content; +import io.swagger.v3.oas.annotations.media.Schema; +import io.swagger.v3.oas.annotations.responses.ApiResponse; import lombok.AllArgsConstructor; import org.springframework.http.ResponseEntity; import org.springframework.web.bind.annotation.GetMapping; @@ -25,11 +30,19 @@ public class BankAccountController { private final Logger logger = Logger.getLogger(BankAccountController.class.getName()); private BankAccountService srvAccount; - /** - * Method for getting all bank accounts for the current user. - * - * @return all bank accounts for the current user - */ + + @Operation( + summary = "Get all bank accounts", + description = "Get all bank accounts for the current user" + ) + @ApiResponse( + responseCode = "200", + description = "Bank accounts found", + content = { + @Content(mediaType = "application/json", + schema = @Schema(implementation = BankAccountDTO.class)) + } + ) @GetMapping public ResponseEntity<?> getAllBankAccounts() { List<BankAccount> accounts = srvAccount.getAllAccountsForCurrentUser(); diff --git a/src/main/java/idatt2106/systemutvikling/sparesti/controller/ChallengeController.java b/src/main/java/idatt2106/systemutvikling/sparesti/controller/ChallengeController.java index 764987759a60779ccdf793550b33e54dd3e75183..09e9f58eb1b36eee3c4e6723aace411a40e43626 100644 --- a/src/main/java/idatt2106/systemutvikling/sparesti/controller/ChallengeController.java +++ b/src/main/java/idatt2106/systemutvikling/sparesti/controller/ChallengeController.java @@ -5,6 +5,12 @@ import idatt2106.systemutvikling.sparesti.mapper.ChallengeMapper; import idatt2106.systemutvikling.sparesti.service.ChallengeService; import idatt2106.systemutvikling.sparesti.service.CurrentUserService; import idatt2106.systemutvikling.sparesti.service.MilestoneService; +import io.swagger.v3.oas.annotations.Operation; +import io.swagger.v3.oas.annotations.Parameter; +import io.swagger.v3.oas.annotations.media.Content; +import io.swagger.v3.oas.annotations.media.Schema; +import io.swagger.v3.oas.annotations.responses.ApiResponse; +import io.swagger.v3.oas.annotations.responses.ApiResponses; import lombok.AllArgsConstructor; import org.springframework.data.domain.Page; import org.springframework.data.domain.Pageable; @@ -26,12 +32,24 @@ public class ChallengeController { private final MilestoneService milestoneService; private final Logger logger = Logger.getLogger(ChallengeController.class.getName()); - /** - * Method for getting active challenges. - * - * @param pageable The page to get - * @return the active challenges - */ + @Operation( + summary = "Get all active challenges", + description = "Get all active challenges for the current user" + ) + @ApiResponses(value = { + @ApiResponse( + responseCode = "200", + description = "Challenges found", + content = { + @io.swagger.v3.oas.annotations.media.Content(mediaType = "application/json") + } + ), + @ApiResponse( + responseCode = "400", + description = "Invalid input for page number or page size", + content = @io.swagger.v3.oas.annotations.media.Content + )} + ) @GetMapping("/paginated/active") @ResponseBody public ResponseEntity<Page<ChallengeDTO>> getActiveChallenges(Pageable pageable) { @@ -39,15 +57,34 @@ public class ChallengeController { throw new IllegalArgumentException("Pageable is not valid"); } return ResponseEntity.ok().body( - challengeService.getActiveChallenges(CurrentUserService.getCurrentUsername(), pageable)); + challengeService.getActiveChallenges(CurrentUserService.getCurrentUsername(), pageable)); } - /** - * Method for getting inactive challenges. - * - * @param pageable The page to get - * @return the inactive challenges - */ + @Operation( + summary = "Get all inactive challenges", + description = "Get all inactive challenges for the current user" + ) + @ApiResponses(value = { + @ApiResponse( + responseCode = "200", + description = "Challenges found", + content = { + @io.swagger.v3.oas.annotations.media.Content(mediaType = "application/json") + } + ), + @ApiResponse( + responseCode = "400", + description = "Invalid input for page number or page size", + content = @io.swagger.v3.oas.annotations.media.Content + )} + ) + @Parameter( + name = "pageable", + description = "The pageable object containing page number and page size", + content = { + @io.swagger.v3.oas.annotations.media.Content(mediaType = "application/json") + } + ) @GetMapping("/paginated/inactive") @ResponseBody public ResponseEntity<Page<ChallengeDTO>> getInactiveChallenges(Pageable pageable) { @@ -55,15 +92,34 @@ public class ChallengeController { return ResponseEntity.badRequest().build(); } return ResponseEntity.ok().body( - challengeService.getInactiveChallenges(CurrentUserService.getCurrentUsername(), pageable)); + challengeService.getInactiveChallenges(CurrentUserService.getCurrentUsername(), pageable)); } - /** - * Method for getting a challenge. - * - * @param challengeId The id of the challenge to get - * @return the challenge - */ + @Operation( + summary = "Get challenge by id", + description = "Get challenge by id" + ) + @ApiResponses(value = { + @ApiResponse( + responseCode = "200", + description = "Challenge found", + content = { + @io.swagger.v3.oas.annotations.media.Content(mediaType = "application/json") + } + ), + @ApiResponse( + responseCode = "400", + description = "Invalid input for challenge id", + content = @io.swagger.v3.oas.annotations.media.Content + )} + ) + @Parameter( + name = "challengeId", + description = "The id of the challenge", + content = { + @io.swagger.v3.oas.annotations.media.Content(mediaType = "application/json") + } + ) @GetMapping("/{challengeId}") @ResponseBody public ResponseEntity<ChallengeDTO> getChallenge(@PathVariable Long challengeId) { @@ -72,18 +128,40 @@ public class ChallengeController { } if (!challengeService.getChallenge(challengeId).getUsername() - .equals(CurrentUserService.getCurrentUsername())) { + .equals(CurrentUserService.getCurrentUsername())) { return ResponseEntity.badRequest().body(challengeService.getChallenge(challengeId)); } return ResponseEntity.ok().body(challengeService.getChallenge(challengeId)); } - /** - * Method for creating a challenge. - * - * @param challengeDTO the challenge to create - */ + @Operation( + summary = "Create challenge", + description = "Create a new challenge" + ) + @ApiResponses(value = { + @ApiResponse( + responseCode = "201", + description = "Challenge created", + content = { + @Content(mediaType = "application/json", + schema = @Schema(implementation = ChallengeDTO.class)) + } + ), + @ApiResponse( + responseCode = "400", + description = "Invalid input for challenge", + content = @Content + )} + ) + @Parameter( + name = "challengeDTO", + description = "The challenge to be created", + content = { + @Content(mediaType = "application/json", + schema = @Schema(implementation = ChallengeDTO.class)) + } + ) @PostMapping("/create") @ResponseBody public ResponseEntity<ChallengeDTO> createChallenge(@RequestBody ChallengeDTO challengeDTO) { @@ -91,15 +169,35 @@ public class ChallengeController { return ResponseEntity.badRequest().build(); } return ResponseEntity.status(HttpStatus.CREATED) - .body(ChallengeMapper.toDTO(challengeService.createChallenge(challengeDTO))); + .body(ChallengeMapper.toDTO(challengeService.createChallenge(challengeDTO))); } - /** - * Method for activating a challenge. - * - * @param challengeId The id of the challenge to activate - * @return the activated challenge - */ + @Operation( + summary = "Activate challenge", + description = "Activate a challenge" + ) + @ApiResponses(value = { + @ApiResponse( + responseCode = "200", + description = "Challenge activated", + content = { + @Content(mediaType = "application/json", + schema = @Schema(implementation = ChallengeDTO.class)) + } + ), + @ApiResponse( + responseCode = "400", + description = "Invalid input for challenge id", + content = @io.swagger.v3.oas.annotations.media.Content + )} + ) + @Parameter( + name = "challengeId", + description = "The id of the challenge", + content = { + @Content(mediaType = "application/json") + } + ) @PostMapping("/activate/{challengeId}") @ResponseBody public ResponseEntity<ChallengeDTO> activateChallenge(@PathVariable Long challengeId) { @@ -113,31 +211,53 @@ public class ChallengeController { } if (!challengeService.getChallenge(challengeId).getUsername() - .equals(CurrentUserService.getCurrentUsername())) { + .equals(CurrentUserService.getCurrentUsername())) { return ResponseEntity.badRequest().body(challengeService.getChallenge(challengeId)); } return ResponseEntity.ok() - .body(ChallengeMapper.toDTO(challengeService.activateChallenge(challengeId))); + .body(ChallengeMapper.toDTO(challengeService.activateChallenge(challengeId))); } - /** - * Method for completing a challenge. - * - * @param challengeId The id of the challenge to complete - * @param milestoneId The id of the milestone to complete - * @return the completed challenge - */ + @Operation( + summary = "Complete challenge", + description = "Complete a challenge and transfer money to milestone, and log the challenge" + ) + @ApiResponses(value = { + @ApiResponse( + responseCode = "200", + description = "Challenge completed", + content = { + @Content(mediaType = "application/json", + schema = @Schema(implementation = String.class) + ) + } + ), + @ApiResponse( + responseCode = "400", + description = "Invalid input for challenge id or milestone id", + content = @io.swagger.v3.oas.annotations.media.Content + )} + ) + @Parameter( + name = "challengeId", + description = "The id of the challenge", + content = { + @Content(mediaType = "application/json", + schema = @Schema(implementation = Long.class) + ) + } + ) @PostMapping("/complete") @ResponseBody public ResponseEntity<String> completeChallenge(@RequestParam("challengeId") Long challengeId, - @RequestParam("milestoneId") Long milestoneId) { + @RequestParam("milestoneId") Long milestoneId) { if (challengeId == null) { return ResponseEntity.badRequest().build(); } if (!challengeService.getChallenge(challengeId).getUsername() - .equals(CurrentUserService.getCurrentUsername())) { + .equals(CurrentUserService.getCurrentUsername())) { return ResponseEntity.badRequest().body("You are not the owner of this challenge"); } @@ -146,19 +266,19 @@ public class ChallengeController { } if (!milestoneService.getMilestoneDTOById(milestoneId).getUsername() - .equals(CurrentUserService.getCurrentUsername())) { + .equals(CurrentUserService.getCurrentUsername())) { return ResponseEntity.badRequest().body("You are not the owner of this milestone"); } Long achievedSum = challengeService.getChallenge(challengeId).getGoalSum(); Long milestoneCurrentSum = milestoneService.getMilestoneDTOById(milestoneId) - .getMilestoneCurrentSum(); + .getMilestoneCurrentSum(); long targetSum = achievedSum + milestoneCurrentSum; milestoneService.increaseMilestonesCurrentSum(milestoneId, achievedSum); if (targetSum > milestoneService.getMilestoneDTOById(milestoneId) - .getMilestoneCurrentSum()) { + .getMilestoneCurrentSum()) { return ResponseEntity.badRequest().body("Could not transfer money to milestone"); } @@ -167,12 +287,32 @@ public class ChallengeController { return ResponseEntity.ok().body("Challenge completed"); } - /** - * Method for deleting a challenge. - * - * @param challengeId The id of the challenge to delete - * @return the deleted challenge - */ + @Operation( + summary = "Delete challenge", + description = "Delete a challenge" + ) + @ApiResponses(value = { + @ApiResponse( + responseCode = "200", + description = "Challenge deleted", + content = { + @Content(mediaType = "application/json", + schema = @Schema(implementation = ChallengeDTO.class)) + } + ), + @ApiResponse( + responseCode = "400", + description = "Invalid input for challenge id", + content = @Content + )} + ) + @Parameter( + name = "challengeId", + description = "The id of the challenge", + content = { + @Content(mediaType = "application/json", + schema = @Schema(implementation = ChallengeDTO.class) + )}) @DeleteMapping("/delete/{challengeId}") @ResponseBody public ResponseEntity<String> moveChallengeToLog(@PathVariable Long challengeId) { @@ -181,7 +321,7 @@ public class ChallengeController { } if (!challengeService.getChallenge(challengeId).getUsername() - .equals(CurrentUserService.getCurrentUsername())) { + .equals(CurrentUserService.getCurrentUsername())) { return ResponseEntity.badRequest().body("You are not the owner of this challenge"); } diff --git a/src/main/java/idatt2106/systemutvikling/sparesti/controller/MilestoneController.java b/src/main/java/idatt2106/systemutvikling/sparesti/controller/MilestoneController.java index bb33b4a01a710e0edac79baa3d2f00cb31c3a99f..26f9e1a8d371530748695afdfe80bf2532e62332 100644 --- a/src/main/java/idatt2106/systemutvikling/sparesti/controller/MilestoneController.java +++ b/src/main/java/idatt2106/systemutvikling/sparesti/controller/MilestoneController.java @@ -5,6 +5,11 @@ import idatt2106.systemutvikling.sparesti.dao.MilestoneDAO; import idatt2106.systemutvikling.sparesti.dto.ManualSavingDTO; import idatt2106.systemutvikling.sparesti.dto.MilestoneDTO; import idatt2106.systemutvikling.sparesti.service.*; +import io.swagger.v3.oas.annotations.Operation; +import io.swagger.v3.oas.annotations.Parameter; +import io.swagger.v3.oas.annotations.media.Content; +import io.swagger.v3.oas.annotations.media.Schema; +import io.swagger.v3.oas.annotations.responses.ApiResponse; import lombok.AllArgsConstructor; import org.springframework.boot.autoconfigure.EnableAutoConfiguration; import org.springframework.data.domain.Page; @@ -36,105 +41,227 @@ public class MilestoneController { private final JWTService jwtService; - /** - * Method for getting the milestones for the current user. - * - * @param pageable The page to get - * @return the milestones for the current user - */ + @Operation( + summary = "Get all milestones", + description = "Get all milestones for the current user" + ) + @ApiResponse( + responseCode = "200", + description = "Milestones found", + content = { + @Content(mediaType = "application/json", + schema = @Schema(implementation = MilestoneDTO.class)) + } + ) + @Parameter( + name = "pageable", + description = "Pageable object for pagination", + required = true, + content = { + @Content(mediaType = "application/json", + schema = @Schema(implementation = Pageable.class)) + } + ) @GetMapping("/user/paginated") public ResponseEntity<Page<MilestoneDTO>> getUserMilestonesPaginated(Pageable pageable) { logger.info("Received request to get paginated list of user milestones."); return ResponseEntity.ok(milestoneService.getActiveMilestonesDTOsByUsernamePaginated(CurrentUserService.getCurrentUsername(), pageable)); } - /** - * Method for getting the milestones for the current user. - * - * @return the milestones for the current user - */ + @Operation( + summary = "Get all milestones", + description = "Get all milestones for the current user" + ) + @ApiResponse( + responseCode = "200", + description = "Milestones found", + content = { + @Content(mediaType = "application/json", + schema = @Schema(implementation = MilestoneDTO.class)) + } + ) @GetMapping("/user") public ResponseEntity<List<MilestoneDTO>> getUserMilestones() { logger.info("Received request to get list of user milestones."); return ResponseEntity.ok().body(milestoneService.getActiveMilestonesDTOsByUsername(CurrentUserService.getCurrentUsername())); } - /** - * Method for creating a milestone. - * - * @param milestoneDTO the milestone to create - */ + @Operation( + summary = "Get all milestones", + description = "Get all milestones for the current user" + ) + @ApiResponse( + responseCode = "200", + description = "Milestones found", + content = { + @Content(mediaType = "application/json", + schema = @Schema(implementation = MilestoneDTO.class)) + } + ) + @Parameter( + name = "pageable", + description = "Pageable object for pagination", + required = true, + content = { + @Content(mediaType = "application/json", + schema = @Schema(implementation = Pageable.class)) + } + ) @PostMapping("/create") public void createMilestone(@RequestBody MilestoneDTO milestoneDTO) { logger.info("Received request to create milestone."); milestoneService.createMilestoneDTO(CurrentUserService.getCurrentUsername(), milestoneDTO); } - /** - * Method for getting a milestone by id. - * - * @param id the id of the milestone to get - * @return the milestone - */ + @Operation( + summary = "Get milestone by id", + description = "Get milestone by id" + ) + @ApiResponse( + responseCode = "200", + description = "Milestone found", + content = { + @Content(mediaType = "application/json", + schema = @Schema(implementation = MilestoneDTO.class)) + } + ) + @Parameter( + name = "id", + description = "Id of the milestone to get", + required = true, + content = { + @Content(mediaType = "application/json", + schema = @Schema(implementation = Long.class)) + } + ) @GetMapping("/{id}") public ResponseEntity<MilestoneDTO> getMilestoneById(@PathVariable Long id) { logger.info("Received request to get milestone by id."); return ResponseEntity.ok(milestoneService.getMilestoneDTOById(id)); } - /** - * Method for completing a milestone. - * - * @param milestoneId the id of the milestone to complete - */ + @Operation( + summary = "Complete milestone", + description = "Complete milestone" + ) + @ApiResponse( + responseCode = "200", + description = "Milestone completed", + content = { + @Content(mediaType = "application/json", + schema = @Schema(implementation = MilestoneDTO.class)) + } + ) + @Parameter( + name = "milestoneId", + description = "Id of the milestone to complete", + required = true, + content = { + @Content(mediaType = "application/json", + schema = @Schema(implementation = Long.class)) + } + ) @PostMapping("/complete") public void completeMilestone(@RequestBody Long milestoneId) { logger.info("Received request to complete milestone."); milestoneService.completeMilestone(CurrentUserService.getCurrentUsername(), milestoneId); } - /** - * Method to update a milestone. - * @param milestoneDTO the milestone to update - * @return the updated milestone - */ + @Operation( + summary = "Update milestone", + description = "Update milestone" + ) + @ApiResponse( + responseCode = "200", + description = "Milestone updated", + content = { + @Content(mediaType = "application/json", + schema = @Schema(implementation = MilestoneDTO.class)) + } + ) + @Parameter( + name = "milestoneDTO", + description = "Milestone to update", + required = true, + content = { + @Content(mediaType = "application/json", + schema = @Schema(implementation = MilestoneDTO.class)) + } + ) @PostMapping("/update") public ResponseEntity<MilestoneDTO> updateMilestone(@RequestBody MilestoneDTO milestoneDTO) { logger.info("Received request to update milestone."); return ResponseEntity.ok(milestoneService.updateMilestoneDTO(CurrentUserService.getCurrentUsername(), milestoneDTO)); } - /** - * Method to edit a milestone. - * - * @param milestoneDTO the milestone to edit - * @return the edited milestone - */ + @Operation( + summary = "Edit milestone", + description = "Edit milestone" + ) + @ApiResponse( + responseCode = "200", + description = "Milestone edited", + content = { + @Content(mediaType = "application/json", + schema = @Schema(implementation = MilestoneDTO.class)) + } + ) + @Parameter( + name = "milestoneDTO", + description = "Milestone to edit", + required = true, + content = { + @Content(mediaType = "application/json", + schema = @Schema(implementation = MilestoneDTO.class)) + } + ) @PutMapping("/edit") public ResponseEntity<MilestoneDTO> editMilestone(@RequestBody MilestoneDTO milestoneDTO){ logger.info("Received request to edit milestone"); return ResponseEntity.ok(milestoneService.editMilestone(CurrentUserService.getCurrentUsername(), milestoneDTO)); } - /** - * Method to delete a milestone by the id of the milestone. - * - * @param id the id of the milstone - */ + @Operation( + summary = "Delete milestone", + description = "Delete milestone with given id" + ) + @ApiResponse( + responseCode = "200", + description = "Milestone deleted" + ) + @Parameter( + name = "id", + description = "Id of the milestone to delete", + required = true, + content = { + @Content(mediaType = "application/json", + schema = @Schema(implementation = Long.class)) + } + ) @DeleteMapping("/delete/{id}") public void deleteMilestone(@PathVariable Long id) { logger.info("Received request to delete milestone."); milestoneService.deleteMilestone(CurrentUserService.getCurrentUsername(), id); } - /** - * Method to manually inject money into a milestone. This will create a new manual saving record, update the milestone and perform a transaction. - * If any of these steps fail, the changes will be rolled back. The user will be notified of the failure. - * If successful, the user will be notified of the success. - * - * @param dto the manual saving dto - * @return response entity - */ + + @Operation( + summary = "Inject manual saving into milestone", + description = "Inject manual saving into milestone" + ) + @ApiResponse( + responseCode = "200", + description = "Manual saving injected" + ) + @Parameter( + name = "dto", + description = "Manual saving dto", + required = true, + content = { + @Content(mediaType = "application/json", + schema = @Schema(implementation = ManualSavingDTO.class)) + } + ) @PostMapping("/inject") public ResponseEntity<?> manualInjectionIntoMilestone(@RequestBody ManualSavingDTO dto) { diff --git a/src/main/java/idatt2106/systemutvikling/sparesti/controller/MilestoneLogController.java b/src/main/java/idatt2106/systemutvikling/sparesti/controller/MilestoneLogController.java index 66e3ad89b1aad8d05dd614d1bc2182c8cc214eed..ddf04403728e30cf48dedf5c3dd52b4d6e991513 100644 --- a/src/main/java/idatt2106/systemutvikling/sparesti/controller/MilestoneLogController.java +++ b/src/main/java/idatt2106/systemutvikling/sparesti/controller/MilestoneLogController.java @@ -4,6 +4,11 @@ import idatt2106.systemutvikling.sparesti.dto.MilestoneDTO; import idatt2106.systemutvikling.sparesti.service.CurrentUserService; import idatt2106.systemutvikling.sparesti.service.JWTService; import idatt2106.systemutvikling.sparesti.service.MilestoneLogService; +import io.swagger.v3.oas.annotations.Operation; +import io.swagger.v3.oas.annotations.Parameter; +import io.swagger.v3.oas.annotations.media.Content; +import io.swagger.v3.oas.annotations.media.Schema; +import io.swagger.v3.oas.annotations.responses.ApiResponse; import org.springframework.boot.autoconfigure.EnableAutoConfiguration; import org.springframework.data.domain.Page; import org.springframework.data.domain.Pageable; @@ -27,15 +32,30 @@ public class MilestoneLogController { public MilestoneLogController(MilestoneLogService milestoneLogService, JWTService jwtService) { this.milestoneLogService = milestoneLogService; - this.jwtService = jwtService; + this.jwtService = jwtService; } - /** - * Method for getting the milestones for the current user. - * - * @param pageable The page to get - * @return the milestones for the current user - */ + @Operation( + summary = "Get user milestones", + description = "Get all milestones for the current user" + ) + @ApiResponse( + responseCode = "200", + description = "Milestones found", + content = { + @Content(mediaType = "application/json", + schema = @Schema(implementation = MilestoneDTO.class)) + } + ) + @Parameter( + name = "pageable", + description = "The pageable object", + content = { + @Content(mediaType = "application/json", + schema = @Schema(implementation = Pageable.class) + ) + } + ) @GetMapping("/user") public ResponseEntity<Page<MilestoneDTO>> getUserMilestones(Pageable pageable) { String username = CurrentUserService.getCurrentUsername(); @@ -43,12 +63,27 @@ public class MilestoneLogController { return ResponseEntity.ok(milestoneLogService.getMilestoneLogsByUsernamePaginated(username, pageable)); } - /** - * Method for getting the milestones for the current user. - * - * @param milestoneLogId The id of the milestone to get - * @return the milestone for the current user - */ + @Operation( + summary = "Get milestone by id", + description = "Get a milestone by its id" + ) + @ApiResponse( + responseCode = "200", + description = "Milestone found", + content = { + @Content(mediaType = "application/json", + schema = @Schema(implementation = MilestoneDTO.class)) + } + ) + @Parameter( + name = "milestoneLogId", + description = "The id of the milestone", + content = { + @Content(mediaType = "application/json", + schema = @Schema(implementation = Long.class) + ) + } + ) @GetMapping("/id") public ResponseEntity<MilestoneDTO> getMilestoneLogById(@RequestBody Long milestoneLogId) { logger.info("Received request to get milestone by id."); diff --git a/src/main/java/idatt2106/systemutvikling/sparesti/controller/TokenController.java b/src/main/java/idatt2106/systemutvikling/sparesti/controller/TokenController.java index bfd1a2fb2b06fe6116cd7e2f0a1346885e7ec4e4..6586c730d5a50392cc58812a8cb77dd0ab14c90c 100644 --- a/src/main/java/idatt2106/systemutvikling/sparesti/controller/TokenController.java +++ b/src/main/java/idatt2106/systemutvikling/sparesti/controller/TokenController.java @@ -10,6 +10,11 @@ import idatt2106.systemutvikling.sparesti.service.CurrentUserService; import idatt2106.systemutvikling.sparesti.service.CustomerServiceInterface; import idatt2106.systemutvikling.sparesti.service.JWTService; import idatt2106.systemutvikling.sparesti.service.PasswordService; +import io.swagger.v3.oas.annotations.Operation; +import io.swagger.v3.oas.annotations.media.Content; +import io.swagger.v3.oas.annotations.media.Schema; +import io.swagger.v3.oas.annotations.responses.ApiResponse; +import io.swagger.v3.oas.annotations.responses.ApiResponses; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.autoconfigure.EnableAutoConfiguration; import org.springframework.http.HttpStatus; @@ -35,21 +40,31 @@ public class TokenController { @Autowired public TokenController(PasswordService passwordService, JWTService jwtService, - UserRepository userRepository) { + UserRepository userRepository) { this.passwordService = passwordService; this.jwtService = jwtService; this.userRepository = userRepository; } - /** - * Endpoint for letting the user login. If login is successful, returns a JWT for use with secured - * endpoints. The user can log in by providing the correct login credentials. A user is considered - * as logged in when it has a token. - * - * @param loginRequest A DTO containing a correct username and password combination. Only the - * fields "username" and "password" is required. - * @return A JWT to use with secured endpoints. - */ + @Operation( + summary = "Login", + description = "Login with username and password" + ) + @ApiResponses(value = { + @ApiResponse( + responseCode = "201", + description = "Login successful", + content = { + @Content(mediaType = "application/json", + schema = @Schema(implementation = String.class)) + } + ), + @ApiResponse( + responseCode = "401", + description = "Access denied, wrong credentials", + content = @Content + ) + }) @PostMapping(value = "/login") @ResponseStatus(value = HttpStatus.CREATED) public ResponseEntity<String> login(final @RequestBody UserCredentialsDTO loginRequest) { @@ -60,26 +75,26 @@ public class TokenController { if (userRepository.findByUsername(loginRequest.getUsername()) == null) { logger.warning("Access denied, wrong credentials: User does not exist."); return ResponseEntity.status(HttpStatus.UNAUTHORIZED) - .body("Access denied, wrong credentials: User does not exist."); + .body("Access denied, wrong credentials: User does not exist."); } } catch (Exception e) { logger.warning("Access denied, wrong credentials: " + e.getMessage()); return ResponseEntity.status(HttpStatus.UNAUTHORIZED) - .body("Access denied, wrong credentials"); + .body("Access denied, wrong credentials"); } try { success = passwordService.correctPassword(loginRequest.getUsername(), - loginRequest.getPassword()); + loginRequest.getPassword()); } catch (Exception e) { logger.warning("Access denied, wrong credentials: " + e.getMessage()); return ResponseEntity.status(HttpStatus.UNAUTHORIZED) - .body("Access denied, wrong credentials"); + .body("Access denied, wrong credentials"); } if (!success) { return ResponseEntity.status(HttpStatus.UNAUTHORIZED) - .body("Access denied, wrong credentials"); + .body("Access denied, wrong credentials"); } String token = jwtService.generateToken(loginRequest.getUsername()); @@ -87,21 +102,18 @@ public class TokenController { return ResponseEntity.ok().body(token); } - /** - * Delete the token for the user. - */ - @DeleteMapping - @ResponseStatus(value = HttpStatus.OK) - public void deleteToken() { - - logger.info("Received request to delete token."); - } - - /** - * Refresh the JWT token. - * - * @return the refreshed token - */ + @Operation( + summary = "Refresh token", + description = "Refresh" + ) + @ApiResponse( + responseCode = "201", + description = "Token refreshed", + content = { + @Content(mediaType = "application/json", + schema = @Schema(implementation = String.class)) + } + ) @GetMapping(value = "/refresh") @ResponseStatus(value = HttpStatus.CREATED) public ResponseEntity<String> refreshToken() { @@ -109,6 +121,5 @@ public class TokenController { return ResponseEntity.ok().body(jwtService.generateToken(CurrentUserService.getCurrentUsername())); } - } diff --git a/src/main/java/idatt2106/systemutvikling/sparesti/controller/TransactionController.java b/src/main/java/idatt2106/systemutvikling/sparesti/controller/TransactionController.java index b20cbb6f791d78042086aed20728e6cbb79dd0c5..2910a8854d27fcda3a33a17b3bcfdfb40f00b745 100644 --- a/src/main/java/idatt2106/systemutvikling/sparesti/controller/TransactionController.java +++ b/src/main/java/idatt2106/systemutvikling/sparesti/controller/TransactionController.java @@ -4,6 +4,11 @@ import idatt2106.systemutvikling.sparesti.dto.TransactionDTO; import idatt2106.systemutvikling.sparesti.mapper.TransactionMapper; import idatt2106.systemutvikling.sparesti.model.Transaction; import idatt2106.systemutvikling.sparesti.service.TransactionService; +import io.swagger.v3.oas.annotations.Operation; +import io.swagger.v3.oas.annotations.media.Content; +import io.swagger.v3.oas.annotations.media.Schema; +import io.swagger.v3.oas.annotations.responses.ApiResponse; +import io.swagger.v3.oas.annotations.responses.ApiResponses; import lombok.AllArgsConstructor; import org.springframework.http.HttpStatus; import org.springframework.http.ResponseEntity; @@ -22,11 +27,25 @@ public class TransactionController { private final TransactionService transactionService; - /** - * Method for getting the latest transactions for the current user. - * - * @return the latest transactions for the current user - */ + @Operation( + summary = "Get latest transactions", + description = "Get the last 30 days of transactions for the current user" + ) + @ApiResponses(value = { + @ApiResponse( + responseCode = "200", + description = "Transactions found", + content = { + @Content(mediaType = "application/json", + schema = @Schema(implementation = TransactionDTO.class)) + } + ), + @ApiResponse( + responseCode = "404", + description = "No transactions found", + content = @Content + ) + }) @GetMapping("/30-day-expenses") public ResponseEntity<List<TransactionDTO>> getLatestExpenses_LastMonth_Categorized() { List<Transaction> transactions = transactionService.getLatestExpensesForCurrentUser_CheckingAccount_Categorized(); diff --git a/src/main/java/idatt2106/systemutvikling/sparesti/controller/UserController.java b/src/main/java/idatt2106/systemutvikling/sparesti/controller/UserController.java index 139fe7477262308bda3feec8597499d0e2c97d8f..2fd28d219c6c7f7fa80a1d9f1ef454e0e942edfc 100644 --- a/src/main/java/idatt2106/systemutvikling/sparesti/controller/UserController.java +++ b/src/main/java/idatt2106/systemutvikling/sparesti/controller/UserController.java @@ -4,6 +4,11 @@ import idatt2106.systemutvikling.sparesti.dto.UserDTO; import java.util.logging.Logger; +import io.swagger.v3.oas.annotations.Operation; +import io.swagger.v3.oas.annotations.media.Content; +import io.swagger.v3.oas.annotations.media.Schema; +import io.swagger.v3.oas.annotations.responses.ApiResponse; +import io.swagger.v3.oas.annotations.responses.ApiResponses; import jakarta.transaction.Transactional; import idatt2106.systemutvikling.sparesti.service.CurrentUserService; import org.springframework.beans.factory.annotation.Autowired; @@ -28,11 +33,27 @@ public class UserController { this.userService = userService; } - /** - * Method for getting total savings for all users. - * - * @return total savings for all users - */ + @Operation( + summary = "Get total savings for all users", + description = "Get the total amount saved by all users" + ) + @ApiResponses( + value = { + @ApiResponse( + responseCode = "200", + description = "Total savings found", + content = { + @Content(mediaType = "application/json", + schema = @Schema(implementation = Long.class)) + } + ), + @ApiResponse( + responseCode = "500", + description = "Internal server error", + content = @Content + ) + } + ) @GetMapping("/get/totalSavings") public ResponseEntity<Long> getTotalSavingsForAllUsers() { logger.info("Received request to get total savings for all users."); @@ -42,11 +63,27 @@ public class UserController { return ResponseEntity.ok(savings); } - /** - * Method for getting total savings for a user. - * - * @return total savings for a user - */ + @Operation( + summary = "Get user total savings", + description = "Get the total amount saved by the current user" + ) + @ApiResponses( + value = { + @ApiResponse( + responseCode = "200", + description = "User savings found", + content = { + @Content(mediaType = "application/json", + schema = @Schema(implementation = Long.class)) + } + ), + @ApiResponse( + responseCode = "500", + description = "Internal server error", + content = @Content + ) + } + ) @GetMapping("/get/savings") public ResponseEntity<Long> getUserTotalSavings() { logger.info("Received request to get user total savings."); @@ -56,22 +93,46 @@ public class UserController { return ResponseEntity.ok(savings); } - /** - * Method for getting user information. - * - * @return user information - */ + @Operation( + summary = "Get user information", + description = "Get the information of the current user" + ) + @ApiResponses( + value = { + @ApiResponse( + responseCode = "200", + description = "User information found", + content = { + @Content(mediaType = "application/json", + schema = @Schema(implementation = UserDTO.class)) + } + ) + } + ) @GetMapping("/get") public ResponseEntity<UserDTO> getUserDTO() { logger.info("Received request to get user information."); return ResponseEntity.ok(userService.getUserDTO(CurrentUserService.getCurrentUsername())); } - /** - * Method for deleting user information. - * - * @return response entity - */ + @Operation( + summary = "Delete user information", + description = "Delete the information of the current user" + ) + @ApiResponses( + value = { + @ApiResponse( + responseCode = "200", + description = "User deleted", + content = @Content + ), + @ApiResponse( + responseCode = "404", + description = "No user found", + content = @Content + ) + } + ) @Transactional @DeleteMapping("/delete") public ResponseEntity<String> deleteUserDTO() { diff --git a/src/main/java/idatt2106/systemutvikling/sparesti/controller/UserCredentialsController.java b/src/main/java/idatt2106/systemutvikling/sparesti/controller/UserCredentialsController.java index 1db689a1acbae25ff53c9fc6f49169c827936635..b12bde56c13c6e76b2c6370ce97ab148dc51f0ad 100644 --- a/src/main/java/idatt2106/systemutvikling/sparesti/controller/UserCredentialsController.java +++ b/src/main/java/idatt2106/systemutvikling/sparesti/controller/UserCredentialsController.java @@ -3,7 +3,14 @@ package idatt2106.systemutvikling.sparesti.controller; import idatt2106.systemutvikling.sparesti.dto.UserCredentialsDTO; import idatt2106.systemutvikling.sparesti.dto.UserDTO; import idatt2106.systemutvikling.sparesti.service.UserService; + import java.util.logging.Logger; + +import io.swagger.v3.oas.annotations.Operation; +import io.swagger.v3.oas.annotations.Parameter; +import io.swagger.v3.oas.annotations.media.Content; +import io.swagger.v3.oas.annotations.media.Schema; +import io.swagger.v3.oas.annotations.responses.ApiResponse; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.http.ResponseEntity; import org.springframework.web.bind.annotation.*; @@ -24,28 +31,59 @@ public class UserCredentialsController { this.userService = userService; } - /** - * Method for creating a user. - * - * @param user the user to create - * @return the created user - */ + @Operation( + summary = "Create user", + description = "Create a new user" + ) + @ApiResponse( + responseCode = "200", + description = "User created", + content = { + @Content(mediaType = "application/json", + schema = @Schema(implementation = UserDTO.class)) + } + ) + @Parameter( + name = "user", + description = "The user to create", + content = { + @Content(mediaType = "application/json", + schema = @Schema(implementation = UserCredentialsDTO.class) + ) + } + ) @RequestMapping("/create") public ResponseEntity<UserDTO> createUser(@RequestBody UserCredentialsDTO user) { logger.info("Received request to create user with username: " + user.getUsername() + "."); return ResponseEntity.ok(userService.createUser(user)); } - /** - * Method for updating a user. - * - * @param userCredentialsDTO the user to update - * @return the updated user - */ + @Operation( + summary = "Update password", + description = "Update the password for a user" + ) + @ApiResponse( + responseCode = "200", + description = "Password updated", + content = { + @Content(mediaType = "application/json", + schema = @Schema(implementation = String.class) + ) + } + ) + @Parameter( + name = "userCredentialsDTO", + description = "The user credentials to update", + content = { + @Content(mediaType = "application/json", + schema = @Schema(implementation = UserCredentialsDTO.class) + ) + } + ) @PutMapping("/updatePassword") public ResponseEntity<String> updatePassword(@RequestBody UserCredentialsDTO userCredentialsDTO) { logger.info( - "Received request to update password for user with username: " + userCredentialsDTO.getUsername() + "."); + "Received request to update password for user with username: " + userCredentialsDTO.getUsername() + "."); return ResponseEntity.ok(userService.updatePassword(userCredentialsDTO)); }