diff --git a/00-backend/src/main/java/de/htwsaar/webshop/config/ControllerPathConfig.java b/00-backend/src/main/java/de/htwsaar/webshop/config/ControllerPathConfig.java index e738b97..bd38453 100644 --- a/00-backend/src/main/java/de/htwsaar/webshop/config/ControllerPathConfig.java +++ b/00-backend/src/main/java/de/htwsaar/webshop/config/ControllerPathConfig.java @@ -25,8 +25,8 @@ public class ControllerPathConfig { //ArticleController public static final String ARTICLE_BASE = "/article"; - public static final String ARTICLE_GET_ALL = ARTICLE_BASE + "/all"; - public static final String ARTICLE_GET_ALL_WITH_IMAGE = ARTICLE_BASE + "/all/image"; + public static final String ARTICLE_GET_ALL = "/all"; + public static final String ARTICLE_GET_ALL_WITH_IMAGE = "/all/image"; //CustomerController public static final String CUSTOMER_BASE = "/customer"; @@ -43,12 +43,12 @@ public class ControllerPathConfig { //OrderController public static final String ORDER_BASE = "/order"; - public static final String ORDER_GET_ALL = ORDER_BASE + "/all"; - public static final String ORDER_GET_ALL_ADMIN = ORDER_BASE + "/all/all"; + public static final String ORDER_GET_ALL = "/all"; + public static final String ORDER_GET_ALL_ADMIN = "/all/all"; //ReviewController public static final String REVIEW_BASE = "/review"; - public static final String REVIEW_GET_ALL = REVIEW_BASE + "/all"; + public static final String REVIEW_GET_ALL = "/all"; //StatisticsController private static final String STATISTICS_BASE = "/statistics"; diff --git a/00-backend/src/main/java/de/htwsaar/webshop/controller/ArticleController.java b/00-backend/src/main/java/de/htwsaar/webshop/controller/ArticleController.java index a30c8fb..213e034 100644 --- a/00-backend/src/main/java/de/htwsaar/webshop/controller/ArticleController.java +++ b/00-backend/src/main/java/de/htwsaar/webshop/controller/ArticleController.java @@ -19,6 +19,7 @@ import static de.htwsaar.webshop.util.LoggerUtil.logRequest; @RestController @Slf4j +@RequestMapping(path = ARTICLE_BASE, produces = "application/json") public class ArticleController { private final ArticleService articleService; @@ -27,26 +28,26 @@ public class ArticleController { this.articleService = articleService; } - @RequestMapping(path = ARTICLE_GET_ALL, method = RequestMethod.GET, produces = "application/json") + @GetMapping(path = ARTICLE_GET_ALL) public ResponseEntity> getAll(HttpServletRequest request) { logRequest(request); return ResponseEntity.ok(articleService.from(articleService.findAll())); } - @RequestMapping(path = ARTICLE_GET_ALL_WITH_IMAGE, method = RequestMethod.GET, produces = "application/json") + @GetMapping(path = ARTICLE_GET_ALL_WITH_IMAGE) public ResponseEntity> getAllWithImageData(HttpServletRequest request) { logRequest(request); return ResponseEntity.ok(articleService.fromWithImage(articleService.findAll())); } - @RequestMapping(path = ARTICLE_BASE, method = RequestMethod.GET, produces = "application/json") + @GetMapping public ResponseEntity getByUUID(HttpServletRequest request, @RequestParam(value = PARAM_UUID) UUID uuid) { logRequest(request); return ResponseEntity.ok(articleService.from(articleService.findByUUID(uuid))); } - @RequestMapping(path = ARTICLE_BASE, method = RequestMethod.POST, produces = "application/json") + @PostMapping public ResponseEntity add(HttpServletRequest request, @RequestBody Article article) { logRequest(request); @@ -60,7 +61,7 @@ public class ArticleController { return ResponseEntity.ok(a != null); } - @RequestMapping(path = ARTICLE_BASE, method = RequestMethod.PUT, produces = "application/json") + @PutMapping public ResponseEntity update(HttpServletRequest request, @RequestParam(value = PARAM_UUID) UUID uuid, @RequestBody Article article) { @@ -80,7 +81,7 @@ public class ArticleController { return ResponseEntity.ok(true); } - @RequestMapping(path = ARTICLE_BASE, method = RequestMethod.DELETE, produces = "application/json") + @DeleteMapping public ResponseEntity delete(HttpServletRequest request, @RequestParam(value = PARAM_UUID) UUID uuid) { logRequest(request); diff --git a/00-backend/src/main/java/de/htwsaar/webshop/controller/OrderController.java b/00-backend/src/main/java/de/htwsaar/webshop/controller/OrderController.java index a2f284b..6605e89 100644 --- a/00-backend/src/main/java/de/htwsaar/webshop/controller/OrderController.java +++ b/00-backend/src/main/java/de/htwsaar/webshop/controller/OrderController.java @@ -22,6 +22,7 @@ import static de.htwsaar.webshop.util.LoggerUtil.logRequest; @RestController @Slf4j +@RequestMapping(path = ORDER_BASE, produces = "application/json") public class OrderController { private final OrderService orderService; @@ -35,7 +36,7 @@ public class OrderController { this.sessionService = sessionService; } - @RequestMapping(path = ORDER_GET_ALL, method = RequestMethod.GET, produces = "application/json") + @GetMapping(path = ORDER_GET_ALL) public ResponseEntity> getAll(HttpServletRequest request, @RequestParam(value = PARAM_CUSTOMER_ID) Long customerId) { logRequest(request); @@ -46,7 +47,7 @@ public class OrderController { return ResponseEntity.ok(orders.stream().map(Order::toModel).toList()); } - @RequestMapping(path = ORDER_GET_ALL_ADMIN, method = RequestMethod.GET, produces = "application/json") + @GetMapping(path = ORDER_GET_ALL_ADMIN) public ResponseEntity> getAll(HttpServletRequest request, @RequestParam(value = PARAM_EMAIL) String email, @RequestParam(value = PARAM_SESSION) UUID token) { @@ -59,7 +60,7 @@ public class OrderController { return ResponseEntity.ok(orderService.findAll().stream().map(Order::toModel).toList()); } - @RequestMapping(path = ORDER_BASE, method = RequestMethod.GET, produces = "application/json") + @GetMapping public ResponseEntity get(HttpServletRequest request, @RequestParam(value = PARAM_ID) Long orderId) { logRequest(request); @@ -70,7 +71,7 @@ public class OrderController { return ResponseEntity.ok(order.toModel()); } - @RequestMapping(path = ORDER_BASE, method = RequestMethod.POST, produces = "application/json") + @PostMapping public ResponseEntity add(HttpServletRequest request, @RequestBody OrderModel order) { logRequest(request); @@ -85,8 +86,8 @@ public class OrderController { return ResponseEntity.ok(saved != null); } - @RequestMapping(path = ORDER_BASE, method = RequestMethod.PATCH, produces = "application/json") - public ResponseEntity update(HttpServletRequest request, + @PatchMapping + public ResponseEntity patch(HttpServletRequest request, @RequestParam(value = PARAM_ID) Long orderId, @RequestParam(value = PARAM_STATUS) OrderStatus status) { logRequest(request); @@ -107,7 +108,7 @@ public class OrderController { return ResponseEntity.ok(orderService.saveNew(order) != null); } - @RequestMapping(path = ORDER_BASE, method = RequestMethod.DELETE, produces = "application/json") + @DeleteMapping public ResponseEntity delete(HttpServletRequest request, @RequestParam(value = PARAM_ID) Long orderId) { logRequest(request); diff --git a/00-backend/src/main/java/de/htwsaar/webshop/controller/ReviewController.java b/00-backend/src/main/java/de/htwsaar/webshop/controller/ReviewController.java index 22ec83b..0674b8e 100644 --- a/00-backend/src/main/java/de/htwsaar/webshop/controller/ReviewController.java +++ b/00-backend/src/main/java/de/htwsaar/webshop/controller/ReviewController.java @@ -1,3 +1,4 @@ + package de.htwsaar.webshop.controller; import de.htwsaar.webshop.model.ReviewModel; @@ -5,8 +6,13 @@ import de.htwsaar.webshop.repository.entities.Review; import de.htwsaar.webshop.service.ArticleService; import de.htwsaar.webshop.service.ReviewService; import jakarta.servlet.http.HttpServletRequest; +import jakarta.validation.Valid; +import jakarta.validation.constraints.Max; +import jakarta.validation.constraints.Min; +import jakarta.validation.constraints.NotBlank; import lombok.extern.slf4j.Slf4j; import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.http.HttpStatus; import org.springframework.http.ResponseEntity; import org.springframework.web.bind.annotation.*; @@ -19,6 +25,7 @@ import static de.htwsaar.webshop.config.ParameterConfig.*; import static de.htwsaar.webshop.util.LoggerUtil.logRequest; @RestController +@RequestMapping(value = REVIEW_BASE, produces = "application/json") @Slf4j public class ReviewController { @@ -31,70 +38,96 @@ public class ReviewController { this.articleService = articleService; } - @RequestMapping(path = REVIEW_GET_ALL, method = RequestMethod.GET, produces = "application/json") + @GetMapping(path = REVIEW_GET_ALL) public ResponseEntity> getAll(HttpServletRequest request, @RequestParam(value = PARAM_UUID) UUID uuid) { logRequest(request); - List review = reviewService.getAllByUUID(uuid).stream().map(reviewService::toModel).toList(); - if (review.isEmpty()) { + List reviews = reviewService.getAllByUUID(uuid) + .stream() + .map(reviewService::toModel) + .toList(); + + if (reviews.isEmpty()) { return ResponseEntity.noContent().build(); } - return ResponseEntity.ok(review); + return ResponseEntity.ok(reviews); } - @RequestMapping(path = REVIEW_BASE, method = RequestMethod.GET, produces = "application/json") - public ResponseEntity get(HttpServletRequest request, - @RequestParam(value = PARAM_ID) Long reviewId) { + @GetMapping + public ResponseEntity get(HttpServletRequest request, + @RequestParam(value = PARAM_ID) Long reviewId) { logRequest(request); Review review = reviewService.getReviewById(reviewId); + if (review == null) { + log.debug("Review with id {} not found", reviewId); return ResponseEntity.noContent().build(); } - return ResponseEntity.ok(review); + + return ResponseEntity.ok(reviewService.toModel(review)); } - @RequestMapping(path = REVIEW_BASE, method = RequestMethod.POST, produces = "application/json") - public ResponseEntity add(HttpServletRequest request, - @RequestParam(value = PARAM_UUID) UUID uuid, - @RequestParam(value = PARAM_RATING) int rating, - @RequestBody String review) { + @PostMapping + public ResponseEntity add(HttpServletRequest request, + @RequestParam(value = PARAM_UUID) UUID articleUuid, + @RequestParam(value = PARAM_RATING) + @Min(value = 0, message = "Rating must be at least 0") + @Max(value = 10, message = "Rating must be at most 10") + int rating, + @RequestBody @NotBlank(message = "Review text cannot be empty") String reviewText) { logRequest(request); - if (uuid == null || articleService.findByUUID(uuid) == null - || rating < 0 || rating > 10) { - log.warn("[{}] failed Validation, sending bad request", request.getRequestURI()); - return ResponseEntity.badRequest().body(false); + if (articleService.findByUUID(articleUuid) == null) { + log.warn("Article with UUID {} not found for review creation", articleUuid); + return ResponseEntity.badRequest().build(); } - Review saved = reviewService.save(uuid, rating, review); - return ResponseEntity.ok(saved != null); + Review savedReview = reviewService.save(articleUuid, rating, reviewText); + + if (savedReview == null) { + log.error("Failed to save review for article UUID {}", articleUuid); + return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build(); + } + + return ResponseEntity.status(HttpStatus.CREATED).body(reviewService.toModel(savedReview)); } - @RequestMapping(path = REVIEW_BASE, method = RequestMethod.PUT, produces = "application/json") - public ResponseEntity update(HttpServletRequest request, - @RequestParam(value = PARAM_ID) Long reviewId, - @RequestBody Review review) { + @PutMapping + public ResponseEntity update(HttpServletRequest request, + @RequestParam(value = PARAM_ID) Long reviewId, + @RequestBody @Valid Review review) { logRequest(request); - if (reviewId == null || reviewService.getReviewById(reviewId) == null) { - return ResponseEntity.badRequest().body(false); + + Review existingReview = reviewService.getReviewById(reviewId); + if (existingReview == null) { + log.warn("Review with id {} not found for update", reviewId); + return ResponseEntity.notFound().build(); } - review.setId(reviewService.getReviewById(reviewId).getId()); - return ResponseEntity.ok(reviewService.save(review) != null); + + // Ensure the ID matches + review.setId(reviewId); + Review updatedReview = reviewService.save(review); + + if (updatedReview == null) { + log.error("Failed to update review with id {}", reviewId); + return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build(); + } + + return ResponseEntity.ok(reviewService.toModel(updatedReview)); } - @RequestMapping(path = REVIEW_BASE, method = RequestMethod.DELETE, produces = "application/json") - public ResponseEntity delete(HttpServletRequest request, - @RequestParam(value = PARAM_ID) Long reviewId) { + @DeleteMapping + public ResponseEntity delete(HttpServletRequest request, + @RequestParam(value = PARAM_ID) Long reviewId) { logRequest(request); - if (reviewId == null) { - log.warn("[{}] got invalid imageId", request.getRequestURI()); - return ResponseEntity.badRequest().body(false); - } - if (reviewService.getReviewById(reviewId) != null) { - log.warn("[{}] got invalid imageId", request.getRequestURI()); - return ResponseEntity.badRequest().body(false); + + Review existingReview = reviewService.getReviewById(reviewId); + if (existingReview == null) { + log.warn("Review with id {} not found for deletion", reviewId); + return ResponseEntity.notFound().build(); } + reviewService.delete(reviewId); return ResponseEntity.ok().build(); } -} +} \ No newline at end of file