diff --git a/00-backend/datasource/database.sqlite b/00-backend/datasource/database.sqlite index 017712b..e0af80c 100644 Binary files a/00-backend/datasource/database.sqlite and b/00-backend/datasource/database.sqlite differ 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 1e5b4a2..cffa3c3 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 @@ -17,7 +17,7 @@ public class ControllerPathConfig { public static final String EMAIL_BASE = "/email"; //ArticleController - public static final String ARTICLE_BASE = "/item"; + public static final String ARTICLE_BASE = "/article"; public static final String ARTICLE_GET_ALL = ARTICLE_BASE + "/all"; //CustomerController @@ -34,4 +34,9 @@ public class ControllerPathConfig { //ReviewController public static final String REVIEW_BASE = "/review"; public static final String REVIEW_GET_ALL = REVIEW_BASE + "/all"; + + //ImageHardcodeController + + private static final String HARDCODE_IMAGE = "/images"; + public static final String HARDCODE_IMAGE_DPS_STICKER = HARDCODE_IMAGE + "/dps.webp"; } \ No newline at end of file diff --git a/00-backend/src/main/java/de/htwsaar/webshop/config/ParameterConfig.java b/00-backend/src/main/java/de/htwsaar/webshop/config/ParameterConfig.java index 4b453f6..07b9cf3 100644 --- a/00-backend/src/main/java/de/htwsaar/webshop/config/ParameterConfig.java +++ b/00-backend/src/main/java/de/htwsaar/webshop/config/ParameterConfig.java @@ -9,5 +9,6 @@ public class ParameterConfig { public static final String PARAM_EMAIL = "email"; public static final String PARAM_PASSWORD = "password"; public static final String PARAM_CUSTOMER_ID = "customerId"; - + public static final String PARAM_URI = "uri"; + public static final String PARAM_RATING = "rating"; } diff --git a/00-backend/src/main/java/de/htwsaar/webshop/controller/HealthController.java b/00-backend/src/main/java/de/htwsaar/webshop/controller/HealthController.java index 73cbf63..3516e76 100644 --- a/00-backend/src/main/java/de/htwsaar/webshop/controller/HealthController.java +++ b/00-backend/src/main/java/de/htwsaar/webshop/controller/HealthController.java @@ -3,6 +3,7 @@ package de.htwsaar.webshop.controller; import jakarta.servlet.http.HttpServletRequest; import lombok.extern.slf4j.Slf4j; import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RequestMethod; import org.springframework.web.bind.annotation.RestController; import static de.htwsaar.webshop.config.ControllerPathConfig.HEALTH; @@ -12,7 +13,7 @@ import static de.htwsaar.webshop.util.LoggerUtil.logRequest; @Slf4j public class HealthController { - @RequestMapping(HEALTH) + @RequestMapping(value = HEALTH, method = RequestMethod.GET) String getHealth(HttpServletRequest request) { logRequest(request); return "OK"; diff --git a/00-backend/src/main/java/de/htwsaar/webshop/controller/ImageController.java b/00-backend/src/main/java/de/htwsaar/webshop/controller/ImageController.java index a703361..525793c 100644 --- a/00-backend/src/main/java/de/htwsaar/webshop/controller/ImageController.java +++ b/00-backend/src/main/java/de/htwsaar/webshop/controller/ImageController.java @@ -2,8 +2,8 @@ package de.htwsaar.webshop.controller; import de.htwsaar.webshop.model.ImageModel; import de.htwsaar.webshop.repository.entities.Image; +import de.htwsaar.webshop.service.ArticleService; import de.htwsaar.webshop.service.ImageService; -import de.htwsaar.webshop.service.ValidatorService; import jakarta.servlet.http.HttpServletRequest; import lombok.extern.slf4j.Slf4j; import org.springframework.beans.factory.annotation.Autowired; @@ -12,6 +12,7 @@ import org.springframework.http.ResponseEntity; import org.springframework.web.bind.annotation.*; import java.util.List; +import java.util.UUID; import static de.htwsaar.webshop.config.ControllerPathConfig.IMAGE_BASE; import static de.htwsaar.webshop.config.ControllerPathConfig.IMAGE_GET_ALL; @@ -22,12 +23,12 @@ import static de.htwsaar.webshop.util.LoggerUtil.logRequest; @Slf4j public class ImageController { private final ImageService imageService; - private final ValidatorService validatorService; + private final ArticleService articleService; @Autowired - public ImageController(ImageService imageService, ValidatorService validatorService) { + public ImageController(ImageService imageService, ArticleService articleService) { this.imageService = imageService; - this.validatorService = validatorService; + this.articleService = articleService; } @RequestMapping(path = IMAGE_GET_ALL, method = RequestMethod.GET, produces = "application/json") @@ -54,15 +55,17 @@ public class ImageController { @RequestMapping(path = IMAGE_BASE, method = RequestMethod.POST, produces = "application/json") public ResponseEntity add(HttpServletRequest request, - @RequestBody Image image) { + @RequestParam(value = PARAM_UUID) UUID articleUuid, + @RequestParam(value = PARAM_URI) String uri) { logRequest(request); - if (validatorService.isInvalid(image)) { + if (articleUuid == null || articleService.findByUUID(articleUuid) == null + || uri == null || uri.isEmpty()) { log.warn("[{}] failed Validation, sending bad request", request.getRequestURI()); return ResponseEntity.badRequest().body(false); } - Image a = imageService.save(image); + Image a = imageService.save(articleUuid, uri); return ResponseEntity.ok(a != null); } diff --git a/00-backend/src/main/java/de/htwsaar/webshop/controller/ImageHardcodeController.java b/00-backend/src/main/java/de/htwsaar/webshop/controller/ImageHardcodeController.java new file mode 100644 index 0000000..d70e70b --- /dev/null +++ b/00-backend/src/main/java/de/htwsaar/webshop/controller/ImageHardcodeController.java @@ -0,0 +1,33 @@ +package de.htwsaar.webshop.controller; + +import jakarta.servlet.http.HttpServletRequest; +import lombok.extern.slf4j.Slf4j; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.core.io.Resource; +import org.springframework.core.io.ResourceLoader; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RequestMethod; +import org.springframework.web.bind.annotation.RestController; + +import java.io.InputStream; + +import static de.htwsaar.webshop.config.ControllerPathConfig.HARDCODE_IMAGE_DPS_STICKER; +import static de.htwsaar.webshop.config.ControllerPathConfig.HEALTH; +import static de.htwsaar.webshop.util.LoggerUtil.logRequest; + +@RestController +@Slf4j +public class ImageHardcodeController { + private final ResourceLoader resourceLoader; + + @Autowired + public ImageHardcodeController(ResourceLoader resourceLoader) { + this.resourceLoader = resourceLoader; + } + + @RequestMapping(value = HARDCODE_IMAGE_DPS_STICKER, method = RequestMethod.GET) + Resource dpsSticker(HttpServletRequest request) { + logRequest(request); + return resourceLoader.getResource("classpath:images/dps_sticker.webp"); + } +} 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 141d53e..00cb83d 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,6 +1,7 @@ package de.htwsaar.webshop.controller; import de.htwsaar.webshop.repository.entities.Review; +import de.htwsaar.webshop.service.ArticleService; import de.htwsaar.webshop.service.ReviewService; import de.htwsaar.webshop.service.ValidatorService; import jakarta.servlet.http.HttpServletRequest; @@ -10,10 +11,10 @@ import org.springframework.http.ResponseEntity; import org.springframework.web.bind.annotation.*; import java.util.List; +import java.util.UUID; import static de.htwsaar.webshop.config.ControllerPathConfig.*; -import static de.htwsaar.webshop.config.ParameterConfig.PARAM_ARTICLE_ID; -import static de.htwsaar.webshop.config.ParameterConfig.PARAM_ID; +import static de.htwsaar.webshop.config.ParameterConfig.*; import static de.htwsaar.webshop.util.LoggerUtil.logRequest; @RestController @@ -21,12 +22,12 @@ import static de.htwsaar.webshop.util.LoggerUtil.logRequest; public class ReviewController { private final ReviewService reviewService; - private final ValidatorService validatorService; + private final ArticleService articleService; @Autowired - public ReviewController(ReviewService reviewService, ValidatorService validatorService) { + public ReviewController(ReviewService reviewService, ArticleService articleService) { this.reviewService = reviewService; - this.validatorService = validatorService; + this.articleService = articleService; } @RequestMapping(path = REVIEW_GET_ALL, method = RequestMethod.GET, produces = "application/json") @@ -53,15 +54,18 @@ public class ReviewController { @RequestMapping(path = REVIEW_BASE, method = RequestMethod.POST, produces = "application/json") public ResponseEntity add(HttpServletRequest request, - @RequestBody Review order) { + @RequestParam(value = PARAM_UUID) UUID uuid, + @RequestParam(value = PARAM_RATING) int rating, + @RequestBody String review) { logRequest(request); - if (validatorService.isInvalid(order)) { + 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); } - Review saved = reviewService.save(order); + Review saved = reviewService.save(uuid, rating, review); return ResponseEntity.ok(saved != null); } diff --git a/00-backend/src/main/java/de/htwsaar/webshop/repository/entities/Image.java b/00-backend/src/main/java/de/htwsaar/webshop/repository/entities/Image.java index 090674a..d9ca6de 100644 --- a/00-backend/src/main/java/de/htwsaar/webshop/repository/entities/Image.java +++ b/00-backend/src/main/java/de/htwsaar/webshop/repository/entities/Image.java @@ -13,7 +13,7 @@ import lombok.*; public class Image { @Id @GeneratedValue(strategy = GenerationType.IDENTITY) - @Column(name = "id", nullable = false, insertable = false, updatable = false, unique = true) + @Column(name = "id", nullable = false, unique = true) private Long id; @ManyToOne(fetch = FetchType.EAGER) diff --git a/00-backend/src/main/java/de/htwsaar/webshop/service/ImageService.java b/00-backend/src/main/java/de/htwsaar/webshop/service/ImageService.java index 6de0fc5..8258539 100644 --- a/00-backend/src/main/java/de/htwsaar/webshop/service/ImageService.java +++ b/00-backend/src/main/java/de/htwsaar/webshop/service/ImageService.java @@ -4,6 +4,7 @@ import de.htwsaar.webshop.model.ImageModel; import de.htwsaar.webshop.repository.entities.Image; import java.util.List; +import java.util.UUID; public interface ImageService { List getImagesByArticleId(Long articleId); @@ -14,6 +15,8 @@ public interface ImageService { Image save(Image image); + Image save(UUID uuid, String uri); + void deleteById(Long imageId); ImageModel from(Image image); diff --git a/00-backend/src/main/java/de/htwsaar/webshop/service/ReviewService.java b/00-backend/src/main/java/de/htwsaar/webshop/service/ReviewService.java index 2422017..9b3b589 100644 --- a/00-backend/src/main/java/de/htwsaar/webshop/service/ReviewService.java +++ b/00-backend/src/main/java/de/htwsaar/webshop/service/ReviewService.java @@ -1,11 +1,13 @@ package de.htwsaar.webshop.service; import java.util.List; +import java.util.UUID; import de.htwsaar.webshop.repository.entities.Review; public interface ReviewService { Review save(Review review); + Review save(UUID articleUuid, int rating, String content); void delete(Long reviewId); Review getReviewById(Long id); List getAllByArticleId(Long articleId); diff --git a/00-backend/src/main/java/de/htwsaar/webshop/service/impl/ImageServiceImpl.java b/00-backend/src/main/java/de/htwsaar/webshop/service/impl/ImageServiceImpl.java index 307b5c3..598ba92 100644 --- a/00-backend/src/main/java/de/htwsaar/webshop/service/impl/ImageServiceImpl.java +++ b/00-backend/src/main/java/de/htwsaar/webshop/service/impl/ImageServiceImpl.java @@ -2,22 +2,29 @@ package de.htwsaar.webshop.service.impl; import de.htwsaar.webshop.model.ImageModel; import de.htwsaar.webshop.repository.ImageRepository; +import de.htwsaar.webshop.repository.entities.Article; import de.htwsaar.webshop.repository.entities.Image; +import de.htwsaar.webshop.service.ArticleService; import de.htwsaar.webshop.service.ImageService; import lombok.extern.slf4j.Slf4j; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; +import org.springframework.transaction.annotation.Transactional; import java.util.List; +import java.util.UUID; @Service @Slf4j +@Transactional public class ImageServiceImpl implements ImageService { private final ImageRepository imageRepository; + private final ArticleService articleService; @Autowired - public ImageServiceImpl(ImageRepository imageRepository) { + public ImageServiceImpl(ImageRepository imageRepository, ArticleService articleService) { this.imageRepository = imageRepository; + this.articleService = articleService; } @@ -37,11 +44,25 @@ public class ImageServiceImpl implements ImageService { } @Override + @Transactional public Image save(Image image) { return imageRepository.save(image); } @Override + @Transactional + public Image save(UUID uuid, String uri) { + Article article = articleService.findByUUID(uuid); + if(article == null) { + return null; + } + Image image = new Image(null, article, uri); + System.out.println(image); + return imageRepository.save(image); + } + + @Override + @Transactional public void deleteById(Long imageId) { imageRepository.deleteById(imageId); } diff --git a/00-backend/src/main/java/de/htwsaar/webshop/service/impl/ReviewServiceImpl.java b/00-backend/src/main/java/de/htwsaar/webshop/service/impl/ReviewServiceImpl.java index 896eaab..e5763cf 100644 --- a/00-backend/src/main/java/de/htwsaar/webshop/service/impl/ReviewServiceImpl.java +++ b/00-backend/src/main/java/de/htwsaar/webshop/service/impl/ReviewServiceImpl.java @@ -2,21 +2,25 @@ package de.htwsaar.webshop.service.impl; import de.htwsaar.webshop.repository.ReviewRepository; import de.htwsaar.webshop.repository.entities.Review; +import de.htwsaar.webshop.service.ArticleService; import de.htwsaar.webshop.service.ReviewService; import lombok.extern.slf4j.Slf4j; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import java.util.List; +import java.util.UUID; @Service @Slf4j public class ReviewServiceImpl implements ReviewService { private final ReviewRepository reviewRepository; + private final ArticleService articleService; @Autowired - public ReviewServiceImpl(ReviewRepository reviewRepository) { + public ReviewServiceImpl(ReviewRepository reviewRepository, ArticleService articleService) { this.reviewRepository = reviewRepository; + this.articleService = articleService; } @Override @@ -24,6 +28,15 @@ public class ReviewServiceImpl implements ReviewService { return reviewRepository.save(review); } + @Override + public Review save(UUID articleUuid, int rating, String content) { + if(articleUuid == null) { + return null; + } + Review review = new Review(null, content, articleService.findByUUID(articleUuid), rating); + return reviewRepository.save(review); + } + @Override public void delete(Long reviewId) { reviewRepository.deleteById(reviewId); diff --git a/00-backend/src/main/resources/images/dps_sticker.webp b/00-backend/src/main/resources/images/dps_sticker.webp new file mode 100644 index 0000000..7cf86b5 Binary files /dev/null and b/00-backend/src/main/resources/images/dps_sticker.webp differ