Fix Image and ReviewController, add articles and images

This commit is contained in:
Tim
2025-06-03 23:51:58 +02:00
parent 62b8374c89
commit 6db33c968e
13 changed files with 107 additions and 21 deletions

View File

@@ -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";
}

View File

@@ -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";
}

View File

@@ -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";

View File

@@ -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<Boolean> 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);
}

View File

@@ -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");
}
}

View File

@@ -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<Boolean> 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);
}

View File

@@ -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)

View File

@@ -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<Image> 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);

View File

@@ -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<Review> getAllByArticleId(Long articleId);

View File

@@ -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);
}

View File

@@ -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);

Binary file not shown.

After

Width:  |  Height:  |  Size: 319 KiB