diff --git a/pom.xml b/pom.xml index 80b085177fa433d331332ae2d0eabe16cc040e82..9a8405f1b42dcca74ecdf0f3ff12b90aec586081 100644 --- a/pom.xml +++ b/pom.xml @@ -80,6 +80,16 @@ <artifactId>springdoc-openapi-starter-webmvc-ui</artifactId> <version>2.2.0</version> </dependency> + <dependency> + <groupId>io.springfox</groupId> + <artifactId>springfox-swagger2</artifactId> + <version>3.0.0</version> + </dependency> + <dependency> + <groupId>io.springfox</groupId> + <artifactId>springfox-swagger-ui</artifactId> + <version>3.0.0</version> + </dependency> </dependencies> <build> diff --git a/src/main/java/hdm/mi/growbros/controllers/AuthenticationController.java b/src/main/java/hdm/mi/growbros/controllers/AuthenticationController.java index 9684e861dfab17106a7f31be44ddc4e686fc44fe..19c14a517f7054edf0d3c24a0284c327effd8f75 100644 --- a/src/main/java/hdm/mi/growbros/controllers/AuthenticationController.java +++ b/src/main/java/hdm/mi/growbros/controllers/AuthenticationController.java @@ -6,10 +6,12 @@ import hdm.mi.growbros.auth.AuthenticationService; import hdm.mi.growbros.auth.RegisterRequest; import hdm.mi.growbros.exceptions.EmailAlreadyExistsException; import hdm.mi.growbros.exceptions.GrowBrosException; +import io.swagger.annotations.ApiOperation; +import io.swagger.annotations.ApiResponse; +import io.swagger.annotations.ApiResponses; import lombok.RequiredArgsConstructor; -import org.springframework.http.HttpStatusCode; +import org.springframework.http.HttpStatus; import org.springframework.http.ResponseEntity; -import org.springframework.web.ErrorResponse; import org.springframework.web.bind.annotation.PostMapping; import org.springframework.web.bind.annotation.RequestBody; import org.springframework.web.bind.annotation.RequestMapping; @@ -19,18 +21,44 @@ import org.springframework.web.bind.annotation.RestController; @RequestMapping("/api/v1/auth") @RequiredArgsConstructor public class AuthenticationController { + private final AuthenticationService authenticationService; + @PostMapping("/register") + @ApiOperation(value = "Register user", response = AuthenticationResponse.class) + @ApiResponses(value = { + @ApiResponse(code = 200, message = "Successfully registered", response = AuthenticationResponse.class), + @ApiResponse(code = 409, message = "Email already exists"), + @ApiResponse(code = 500, message = "Internal Server Error") + }) public ResponseEntity<AuthenticationResponse> register( - @RequestBody RegisterRequest request + @RequestBody RegisterRequest request ) { - return ResponseEntity.ok(authenticationService.register(request)); + try { + AuthenticationResponse response = authenticationService.register(request); + return new ResponseEntity<>(response, HttpStatus.OK); + } catch (EmailAlreadyExistsException e) { + return new ResponseEntity<>(HttpStatus.CONFLICT); + } catch (GrowBrosException e) { + return new ResponseEntity<>(HttpStatus.INTERNAL_SERVER_ERROR); + } } @PostMapping("/authenticate") + @ApiOperation(value = "Authenticate user", response = AuthenticationResponse.class) + @ApiResponses(value = { + @ApiResponse(code = 200, message = "Successfully authenticated", response = AuthenticationResponse.class), + @ApiResponse(code = 401, message = "Unauthorized"), + @ApiResponse(code = 500, message = "Internal Server Error") + }) public ResponseEntity<AuthenticationResponse> authenticate( @RequestBody AuthenticationRequest request ) { - return ResponseEntity.ok(authenticationService.authenticate(request)); + try { + AuthenticationResponse response = authenticationService.authenticate(request); + return new ResponseEntity<>(response, HttpStatus.OK); + } catch (GrowBrosException e) { + return new ResponseEntity<>(HttpStatus.UNAUTHORIZED); + } } } diff --git a/src/main/java/hdm/mi/growbros/controllers/GardenController.java b/src/main/java/hdm/mi/growbros/controllers/GardenController.java index 4f42d399be5fc2def5fae2badeec5a61baed9497..d9fc37cdc99f486e936e9b0141b5a15bc7a8c218 100644 --- a/src/main/java/hdm/mi/growbros/controllers/GardenController.java +++ b/src/main/java/hdm/mi/growbros/controllers/GardenController.java @@ -1,12 +1,18 @@ package hdm.mi.growbros.controllers; +import hdm.mi.growbros.exceptions.PlantNotFoundException; +import hdm.mi.growbros.exceptions.GrandmaException; import hdm.mi.growbros.models.GardenEntry; import hdm.mi.growbros.models.plant.Plant; import hdm.mi.growbros.models.user.User; import hdm.mi.growbros.service.GardenService; +import io.swagger.annotations.ApiOperation; +import io.swagger.annotations.ApiResponse; +import io.swagger.annotations.ApiResponses; import lombok.RequiredArgsConstructor; import org.slf4j.Logger; import org.slf4j.LoggerFactory; +import org.springframework.http.HttpStatus; import org.springframework.http.ResponseEntity; import org.springframework.security.core.annotation.AuthenticationPrincipal; import org.springframework.web.bind.annotation.*; @@ -22,52 +28,146 @@ public class GardenController { private final GardenService gardenService; @GetMapping + @ApiOperation(value = "Get plants in the garden", response = Plant.class, responseContainer = "List") + @ApiResponses(value = { + @ApiResponse(code = 200, message = "Successfully retrieved plants"), + @ApiResponse(code = 401, message = "Unauthorized"), + @ApiResponse(code = 403, message = "Forbidden"), + @ApiResponse(code = 404, message = "Plant not found"), + @ApiResponse(code = 500, message = "Internal Server Error") + }) public ResponseEntity<List<Plant>> getPlantsInGarden( @RequestParam(value = "sort", required = false) String sort, @AuthenticationPrincipal User user ) { - return ResponseEntity.ok(gardenService.getUserPlants(user, sort)); + try { + return ResponseEntity.ok(gardenService.getUserPlants(user, sort)); + } catch (PlantNotFoundException e) { + return ResponseEntity.status(HttpStatus.NOT_FOUND).body(null); + } catch (Exception e) { + log.error("Error while retrieving plants from the garden", e); + return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(null); + } } @GetMapping("/count") - public int getGardenSize( + @ApiOperation(value = "Get the size of the garden") + @ApiResponses(value = { + @ApiResponse(code = 200, message = "Successfully retrieved garden size"), + @ApiResponse(code = 401, message = "Unauthorized"), + @ApiResponse(code = 500, message = "Internal Server Error") + }) + public ResponseEntity<Integer> getGardenSize( @AuthenticationPrincipal User user ) { - return gardenService.countByUser(user); + try { + return ResponseEntity.ok(gardenService.countByUser(user)); + } catch (Exception e) { + log.error("Error while retrieving garden size", e); + return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(null); + } } @PostMapping("/add/{plantId}") + @ApiOperation(value = "Add a plant to the garden", response = Plant.class) + @ApiResponses(value = { + @ApiResponse(code = 201, message = "Successfully added plant to the garden"), + @ApiResponse(code = 401, message = "Unauthorized"), + @ApiResponse(code = 403, message = "Forbidden"), + @ApiResponse(code = 404, message = "Plant not found"), + @ApiResponse(code = 500, message = "Internal Server Error") + }) public ResponseEntity<Plant> addPlantToGarden( @PathVariable Long plantId, @AuthenticationPrincipal User user ) { - final Plant plantAdded = gardenService.addPlantToGarden(plantId, user); - return ResponseEntity.status(201).body(plantAdded); + try { + final Plant plantAdded = gardenService.addPlantToGarden(plantId, user); + return ResponseEntity.status(HttpStatus.CREATED).body(plantAdded); + } catch (PlantNotFoundException e) { + return ResponseEntity.status(HttpStatus.NOT_FOUND).body(null); + } catch (GrandmaException e) { + return ResponseEntity.status(HttpStatus.FORBIDDEN).body(null); + } catch (Exception e) { + log.error("Error while adding plant to the garden", e); + return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(null); + } } @DeleteMapping("/remove/{entryId}") + @ApiOperation(value = "Remove a plant from the garden") + @ApiResponses(value = { + @ApiResponse(code = 204, message = "Successfully removed plant from the garden"), + @ApiResponse(code = 401, message = "Unauthorized"), + @ApiResponse(code = 403, message = "Forbidden"), + @ApiResponse(code = 500, message = "Internal Server Error") + }) public ResponseEntity<Void> removePlantFromGarden( @PathVariable Long entryId, @AuthenticationPrincipal User user ) { - gardenService.removeFromGarden(entryId, user); - return ResponseEntity.status(204).build(); + try { + gardenService.removeFromGarden(entryId, user); + return ResponseEntity.status(HttpStatus.NO_CONTENT).build(); + } catch (GrandmaException e) { + return ResponseEntity.status(HttpStatus.FORBIDDEN).build(); + } catch (Exception e) { + log.error("Error while removing plant from the garden", e); + return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build(); + } } @DeleteMapping("/remove/all") - public int removeAllPlantsFromGarden( + @ApiOperation(value = "Remove all plants from the garden") + @ApiResponses(value = { + @ApiResponse(code = 200, message = "Successfully removed all plants from the garden"), + @ApiResponse(code = 401, message = "Unauthorized"), + @ApiResponse(code = 403, message = "Forbidden"), + @ApiResponse(code = 500, message = "Internal Server Error") + }) + public ResponseEntity<Integer> removeAllPlantsFromGarden( @AuthenticationPrincipal User user ) { - return gardenService.clearGarden(user); + try { + return ResponseEntity.ok(gardenService.clearGarden(user)); + } catch (GrandmaException e) { + return ResponseEntity.status(HttpStatus.FORBIDDEN).body(null); + } catch (Exception e) { + log.error("Error while removing all plants from the garden", e); + return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(null); + } } - - @GetMapping ("/getPlantsNeedingWatering") - public ResponseEntity<String> getPlantsNeedingWatring (@AuthenticationPrincipal User user){ - return ResponseEntity.ok(gardenService.notifyAboutPlantsNeedingWatering(user)); + @GetMapping("/getPlantsNeedingWatering") + @ApiOperation(value = "Get plants needing watering", response = String.class) + @ApiResponses(value = { + @ApiResponse(code = 200, message = "Successfully retrieved plants needing watering"), + @ApiResponse(code = 401, message = "Unauthorized"), + @ApiResponse(code = 500, message = "Internal Server Error") + }) + public ResponseEntity<String> getPlantsNeedingWatring(@AuthenticationPrincipal User user) { + try { + return ResponseEntity.ok(gardenService.notifyAboutPlantsNeedingWatering(user)); + } catch (Exception e) { + log.error("Error while retrieving plants needing watering", e); + return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(null); + } } + @PostMapping("/setNewWateringFrequency") - public void setNewWateringService(@RequestParam("wateringFrequency") int wateringFrequency , @RequestBody GardenEntry gardenEntry){ - gardenEntry.setWateringFrequency(wateringFrequency); + @ApiOperation(value = "Set new watering frequency for a plant") + @ApiResponses(value = { + @ApiResponse(code = 204, message = "Successfully set new watering frequency"), + @ApiResponse(code = 401, message = "Unauthorized"), + @ApiResponse(code = 500, message = "Internal Server Error") + }) + public ResponseEntity<Void> setNewWateringService( @RequestParam("wateringFrequency") int wateringFrequency, @RequestBody GardenEntry gardenEntry) { + try { + gardenEntry.setWateringFrequency(wateringFrequency); + return ResponseEntity.status(HttpStatus.NO_CONTENT).build(); + } catch (Exception e) { + log.error("Error while setting new watering frequency", e); + return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build(); + } } } diff --git a/src/main/java/hdm/mi/growbros/controllers/GrandmaController.java b/src/main/java/hdm/mi/growbros/controllers/GrandmaController.java index 9b692b8ae2646fdb2f5358f7140dbab58da1acd8..5fe2e3f735f12fc88d415889732d129a7775cf7a 100644 --- a/src/main/java/hdm/mi/growbros/controllers/GrandmaController.java +++ b/src/main/java/hdm/mi/growbros/controllers/GrandmaController.java @@ -1,8 +1,12 @@ package hdm.mi.growbros.controllers; - +import hdm.mi.growbros.exceptions.GrandmaException; import hdm.mi.growbros.models.user.User; import hdm.mi.growbros.service.GrandmaService; +import io.swagger.annotations.ApiOperation; +import io.swagger.annotations.ApiResponse; +import io.swagger.annotations.ApiResponses; +import org.springframework.http.HttpStatus; import org.springframework.http.ResponseEntity; import org.springframework.security.core.annotation.AuthenticationPrincipal; import org.springframework.web.bind.annotation.GetMapping; @@ -15,17 +19,41 @@ public class GrandmaController { private final GrandmaService grandmaService; - public GrandmaController(GrandmaService grandmaService){ + public GrandmaController(GrandmaService grandmaService) { this.grandmaService = grandmaService; } - @GetMapping ("/getGrowingTips") + @GetMapping("/getGrowingTips") + @ApiOperation(value = "Get growing tip of the day", response = String.class) + @ApiResponses(value = { + @ApiResponse(code = 200, message = "Successfully retrieved growing tip"), + @ApiResponse(code = 401, message = "Unauthorized"), + @ApiResponse(code = 500, message = "Internal Server Error") + }) public ResponseEntity<String> getGrowingTipDesTages(@AuthenticationPrincipal User user) { - return ResponseEntity.ok(grandmaService.growingTippDesTages(user)); + try { + return ResponseEntity.ok(grandmaService.growingTippDesTages(user)); + } catch (GrandmaException e) { + return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body("Grandma Error"); + } catch (Exception e) { + return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body("Internal Server Error"); + } } - @GetMapping ("/getGrowablePlants") - public ResponseEntity<String> getPlantsReadyToGrow(@AuthenticationPrincipal User user){ - return ResponseEntity.ok(grandmaService.notifyAboutPlantsReadyToGrow(user)); + @GetMapping("/getGrowablePlants") + @ApiOperation(value = "Get plants ready to grow", response = String.class) + @ApiResponses(value = { + @ApiResponse(code = 200, message = "Successfully retrieved plants ready to grow"), + @ApiResponse(code = 401, message = "Unauthorized"), + @ApiResponse(code = 500, message = "Internal Server Error") + }) + public ResponseEntity<String> getPlantsReadyToGrow(@AuthenticationPrincipal User user) { + try { + return ResponseEntity.ok(grandmaService.notifyAboutPlantsReadyToGrow(user)); + } catch (GrandmaException e) { + return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body("Grandma Error"); + } catch (Exception e) { + return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body("Internal Server Error"); + } } } diff --git a/src/main/java/hdm/mi/growbros/controllers/PlantsController.java b/src/main/java/hdm/mi/growbros/controllers/PlantsController.java index 97e0554fd38f9d723e246e14dc4eaff58a429047..a605dab9a2fac098dd2db3db692462b2dff1d249 100644 --- a/src/main/java/hdm/mi/growbros/controllers/PlantsController.java +++ b/src/main/java/hdm/mi/growbros/controllers/PlantsController.java @@ -7,8 +7,13 @@ import hdm.mi.growbros.models.dto.PlantUpdateRequest; import hdm.mi.growbros.models.plant.Plant; import hdm.mi.growbros.models.search.SearchRequest; import hdm.mi.growbros.service.PlantsService; +import io.swagger.annotations.ApiOperation; +import io.swagger.annotations.ApiResponse; +import io.swagger.annotations.ApiResponses; import jakarta.validation.Valid; import lombok.RequiredArgsConstructor; +import org.springframework.http.HttpStatus; +import org.springframework.http.ResponseEntity; import org.springframework.web.bind.annotation.*; import java.util.Collection; @@ -21,40 +26,124 @@ public class PlantsController { private final PlantsService plantsService; @GetMapping - public CustomPageDto<Plant> getAllPlants( + @ApiOperation(value = "Get all plants", response = CustomPageDto.class) + @ApiResponses(value = { + @ApiResponse(code = 200, message = "Successfully retrieved plants"), + @ApiResponse(code = 500, message = "Internal Server Error") + }) + public ResponseEntity<CustomPageDto<Plant>> getAllPlants( @RequestParam(value = "page", defaultValue = "0") int page, @RequestParam(value = "pageSize", defaultValue = "25") int pageSize ) { - return plantsService.getPlants(page, pageSize); + try { + CustomPageDto<Plant> plants = plantsService.getPlants(page, pageSize); + return ResponseEntity.ok(plants); + } catch (Exception e) { + return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build(); + } } @GetMapping("/{id}") - public Plant getPlant(@PathVariable long id) { - return plantsService.findById(id).orElseThrow(() -> new PlantNotFoundException(id)); + @ApiOperation(value = "Get a specific plant by ID", response = Plant.class) + @ApiResponses(value = { + @ApiResponse(code = 200, message = "Successfully retrieved the plant"), + @ApiResponse(code = 404, message = "Plant not found"), + @ApiResponse(code = 500, message = "Internal Server Error") + }) + public ResponseEntity<Plant> getPlant(@PathVariable long id) { + try { + Plant plant = plantsService.findById(id).orElseThrow(() -> new PlantNotFoundException(id)); + return ResponseEntity.ok(plant); + } catch (PlantNotFoundException e) { + return ResponseEntity.status(HttpStatus.NOT_FOUND).build(); + } catch (Exception e) { + return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build(); + } } @GetMapping("/randomPlants/{numberOfPlants}") - public Collection<Plant> getRandomPlants(@PathVariable Integer numberOfPlants) { - return plantsService.getRandomPlants(numberOfPlants); + @ApiOperation(value = "Get a collection of random plants", response = Collection.class) + @ApiResponses(value = { + @ApiResponse(code = 200, message = "Successfully retrieved random plants"), + @ApiResponse(code = 500, message = "Internal Server Error") + }) + public ResponseEntity<Collection<Plant>> getRandomPlants(@PathVariable Integer numberOfPlants) { + try { + Collection<Plant> randomPlants = plantsService.getRandomPlants(numberOfPlants); + return ResponseEntity.ok(randomPlants); + } catch (Exception e) { + return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build(); + } } @GetMapping("/search") - public Collection<Plant> getPlantsSearchedFor(@Valid SearchRequest searchRequest) { - return plantsService.getPlantsSearchedFor(searchRequest); + @ApiOperation(value = "Search for plants", response = Collection.class) + @ApiResponses(value = { + @ApiResponse(code = 200, message = "Successfully retrieved searched plants"), + @ApiResponse(code = 404, message = "Plant not found"), + @ApiResponse(code = 500, message = "Internal Server Error") + }) + public ResponseEntity<Collection<Plant>> getPlantsSearchedFor(@Valid SearchRequest searchRequest) { + try { + Collection<Plant> searchedPlants = plantsService.getPlantsSearchedFor(searchRequest); + return ResponseEntity.ok(searchedPlants); + } catch (PlantNotFoundException e) { + return ResponseEntity.status(HttpStatus.NOT_FOUND).build(); + } catch (Exception e) { + return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build(); + } } @PostMapping - public void createPlant(@RequestBody @Valid Plant plant) { - plantsService.createPlant(plant); + @ApiOperation(value = "Create a new plant") + @ApiResponses(value = { + @ApiResponse(code = 201, message = "Successfully created the plant"), + @ApiResponse(code = 400, message = "Bad Request"), + @ApiResponse(code = 500, message = "Internal Server Error") + }) + public ResponseEntity<Void> createPlant(@RequestBody @Valid Plant plant) { + try { + plantsService.createPlant(plant); + return ResponseEntity.status(HttpStatus.CREATED).build(); + } catch (Exception e) { + return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build(); + } } @DeleteMapping("/{id}") - public DeletedPlantDto deletePlant(@PathVariable long id) { - return plantsService.deletePlant(id); + @ApiOperation(value = "Delete a plant by ID", response = DeletedPlantDto.class) + @ApiResponses(value = { + @ApiResponse(code = 200, message = "Successfully deleted the plant"), + @ApiResponse(code = 404, message = "Plant not found"), + @ApiResponse(code = 500, message = "Internal Server Error") + }) + public ResponseEntity<DeletedPlantDto> deletePlant(@PathVariable long id) { + try { + DeletedPlantDto deletedPlant = plantsService.deletePlant(id); + return ResponseEntity.ok(deletedPlant); + } catch (PlantNotFoundException e) { + return ResponseEntity.status(HttpStatus.NOT_FOUND).build(); + } catch (Exception e) { + return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build(); + } } @PatchMapping("/{id}") - public Plant updatePlant(@PathVariable long id, @RequestBody PlantUpdateRequest updateRequest) { - return plantsService.updatePlant(id, updateRequest); + @ApiOperation(value = "Update a plant by ID", response = Plant.class) + @ApiResponses(value = { + @ApiResponse(code = 200, message = "Successfully updated the plant"), + @ApiResponse(code = 400, message = "Bad Request"), + @ApiResponse(code = 404, message = "Plant not found"), + @ApiResponse(code = 500, message = "Internal Server Error") + }) + public ResponseEntity<Plant> updatePlant(@PathVariable long id, @RequestBody PlantUpdateRequest updateRequest) { + try { + Plant updatedPlant = plantsService.updatePlant(id, updateRequest); + return ResponseEntity.ok(updatedPlant); + } catch (PlantNotFoundException e) { + return ResponseEntity.status(HttpStatus.NOT_FOUND).build(); + } catch (Exception e) { + return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build(); + } } } diff --git a/src/main/java/hdm/mi/growbros/controllers/WishListController.java b/src/main/java/hdm/mi/growbros/controllers/WishListController.java index 54f406664c88b466a67f1b1936bfb821def8c055..de8cab81df4d2a53217da2b9026e6b51ee250d39 100644 --- a/src/main/java/hdm/mi/growbros/controllers/WishListController.java +++ b/src/main/java/hdm/mi/growbros/controllers/WishListController.java @@ -1,8 +1,13 @@ package hdm.mi.growbros.controllers; +import hdm.mi.growbros.exceptions.GrowBrosException; +import hdm.mi.growbros.exceptions.PlantNotFoundException; import hdm.mi.growbros.models.plant.Plant; import hdm.mi.growbros.models.user.User; import hdm.mi.growbros.service.WishListService; +import io.swagger.annotations.ApiOperation; +import io.swagger.annotations.ApiResponse; +import io.swagger.annotations.ApiResponses; import lombok.RequiredArgsConstructor; import org.springframework.http.HttpStatus; import org.springframework.http.ResponseEntity; @@ -20,34 +25,88 @@ public class WishListController { private final WishListService wishListService; @GetMapping - public Collection<Plant> getAllPlantsInWishList(@RequestParam(value = "sort", required = false) String sort, - @AuthenticationPrincipal User user) { - return wishListService.getWishedPlants(user, sort); + @ApiOperation(value = "Get all plants in wishlist") + @ApiResponses(value = { + @ApiResponse(code = 200, message = "Successfully retrieved wishlist plants", response = Plant.class, responseContainer = "List"), + @ApiResponse(code = 500, message = "Internal Server Error") + }) + public ResponseEntity<Collection<Plant>> getAllPlantsInWishList( + @RequestParam(value = "sort", required = false) String sort, + @AuthenticationPrincipal User user) { + try { + Collection<Plant> wishedPlants = wishListService.getWishedPlants(user, sort); + return ResponseEntity.ok(wishedPlants); + } catch (GrowBrosException e) { + return new ResponseEntity<>(HttpStatus.INTERNAL_SERVER_ERROR); + } } @GetMapping("/count") - public int getWishListSize( + @ApiOperation(value = "Get the size of the wishlist") + @ApiResponses(value = { + @ApiResponse(code = 200, message = "Successfully retrieved wishlist size"), + @ApiResponse(code = 500, message = "Internal Server Error") + }) + public ResponseEntity<Integer> getWishListSize( @AuthenticationPrincipal User user) { - return wishListService.countByUser(user); + try { + int wishlistSize = wishListService.countByUser(user); + return ResponseEntity.ok(wishlistSize); + } catch (GrowBrosException e) { + return new ResponseEntity<>(HttpStatus.INTERNAL_SERVER_ERROR); + } } - @PostMapping("/add/{plantId}") + @ApiOperation(value = "Add plant to wishlist") + @ApiResponses(value = { + @ApiResponse(code = 201, message = "Successfully added to wishlist"), + @ApiResponse(code = 404, message = "Plant not found"), + @ApiResponse(code = 500, message = "Internal Server Error") + }) public ResponseEntity<Integer> addPlantToWishlist(@PathVariable Long plantId, @AuthenticationPrincipal User user) { - wishListService.addPlantToWishList(plantId, user); - return ResponseEntity.status(201).build(); + try { + wishListService.addPlantToWishList(plantId, user); + return ResponseEntity.status(201).build(); + } catch (PlantNotFoundException e) { + return new ResponseEntity<>(HttpStatus.NOT_FOUND); + } catch (GrowBrosException e) { + return new ResponseEntity<>(HttpStatus.INTERNAL_SERVER_ERROR); + } } @DeleteMapping("/remove/{entryId}") - @ResponseStatus(HttpStatus.NO_CONTENT) - public void removePlantFromWishlist(@PathVariable Long entryId, - @AuthenticationPrincipal User user) { - wishListService.removeFromWishList(entryId, user); + @ApiOperation(value = "Remove plant from wishlist") + @ApiResponses(value = { + @ApiResponse(code = 204, message = "Successfully removed from wishlist"), + @ApiResponse(code = 404, message = "Wishlist entry not found"), + @ApiResponse(code = 500, message = "Internal Server Error") + }) + public ResponseEntity<Void> removePlantFromWishlist( + @PathVariable Long entryId, + @AuthenticationPrincipal User user) { + try { + wishListService.removeFromWishList(entryId, user); + return ResponseEntity.noContent().build(); + } catch (GrowBrosException e) { + return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build(); + } } + @DeleteMapping("/remove/all") - public int removeAllPlantsFromWishlist(@AuthenticationPrincipal User user) { - return wishListService.clearWishList(user); + @ApiOperation(value = "Remove all plants from wishlist") + @ApiResponses(value = { + @ApiResponse(code = 200, message = "Successfully removed all from wishlist"), + @ApiResponse(code = 500, message = "Internal Server Error") + }) + public ResponseEntity<Integer> removeAllPlantsFromWishlist(@AuthenticationPrincipal User user) { + try { + int count = wishListService.clearWishList(user); + return ResponseEntity.ok(count); + } catch (GrowBrosException e) { + return new ResponseEntity<>(HttpStatus.INTERNAL_SERVER_ERROR); + } } -} \ No newline at end of file +} diff --git a/src/main/java/hdm/mi/growbros/exceptions/GrandmaException.java b/src/main/java/hdm/mi/growbros/exceptions/GrandmaException.java new file mode 100644 index 0000000000000000000000000000000000000000..3372eaf931b36bd2cc04dd366bc705a15bb87364 --- /dev/null +++ b/src/main/java/hdm/mi/growbros/exceptions/GrandmaException.java @@ -0,0 +1,9 @@ +package hdm.mi.growbros.exceptions; + +import org.springframework.http.HttpStatus; + +public class GrandmaException extends GrowBrosException{ + public GrandmaException(HttpStatus httpStatus, String message) { + super(HttpStatus.INTERNAL_SERVER_ERROR, message); + } +} \ No newline at end of file diff --git a/src/main/java/hdm/mi/growbros/exceptions/UserPermissionException.java b/src/main/java/hdm/mi/growbros/exceptions/UserPermissionException.java new file mode 100644 index 0000000000000000000000000000000000000000..2fc068d245744607b1568cb9e5e5fca9236dbdd2 --- /dev/null +++ b/src/main/java/hdm/mi/growbros/exceptions/UserPermissionException.java @@ -0,0 +1,9 @@ +package hdm.mi.growbros.exceptions; + +import org.springframework.http.HttpStatus; + +public class UserPermissionException extends GrowBrosException{ + public UserPermissionException(HttpStatus httpStatus, String message) { + super(HttpStatus.FORBIDDEN, message); + } +}