Add ImageController, merge ArticleModelFactory into ArticleService
This commit is contained in:
@@ -13,6 +13,6 @@ public class ControllerPathConfig {
|
||||
public static final String ARTICLE_GET_ALL = ARTICLE_BASE + "/all";
|
||||
|
||||
//ImageController
|
||||
private static final String IMAGE_BASE = "/image";
|
||||
public static final String IMAGE_BASE = "/image";
|
||||
public static final String IMAGE_GET_ALL = IMAGE_BASE + "/all";
|
||||
}
|
||||
@@ -5,11 +5,7 @@ public class ParameterConfig {
|
||||
|
||||
public static final String PARAM_ID = "id";
|
||||
public static final String PARAM_UUID = "uuid";
|
||||
public static final String PARAM_NAME = "name";
|
||||
public static final String PARAM_DESCRIPTION = "description";
|
||||
public static final String PARAM_PRICE = "price";
|
||||
public static final String PARAM_DISCOUNT = "discount";
|
||||
public static final String PARAM_CATEGORY = "category";
|
||||
public static final String PARAM_STOCK = "stock";
|
||||
public static final String PARAM_ARTICLE_ID = "articleId";
|
||||
public static final String PARAM_IMAGE_ID = "imageId";
|
||||
|
||||
}
|
||||
|
||||
@@ -2,7 +2,6 @@ package de.htwsaar.webshop.controller;
|
||||
|
||||
import de.htwsaar.webshop.model.ArticleModel;
|
||||
import de.htwsaar.webshop.repository.entities.Article;
|
||||
import de.htwsaar.webshop.service.ArticleModelFactory;
|
||||
import de.htwsaar.webshop.service.ArticleService;
|
||||
import jakarta.servlet.http.HttpServletRequest;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
@@ -21,25 +20,23 @@ import static de.htwsaar.webshop.util.LoggerUtil.logRequest;
|
||||
@Slf4j
|
||||
public class ArticleController {
|
||||
private final ArticleService articleService;
|
||||
private final ArticleModelFactory articleModelFactory;
|
||||
|
||||
@Autowired
|
||||
public ArticleController(ArticleService articleService, ArticleModelFactory articleModelFactory) {
|
||||
public ArticleController(ArticleService articleService) {
|
||||
this.articleService = articleService;
|
||||
this.articleModelFactory = articleModelFactory;
|
||||
}
|
||||
|
||||
@RequestMapping(path = ARTICLE_GET_ALL, method = RequestMethod.GET, produces = "application/json")
|
||||
public ResponseEntity<List<ArticleModel>> getAll(HttpServletRequest request) {
|
||||
logRequest(request);
|
||||
return ResponseEntity.ok(articleModelFactory.from(articleService.findAll()));
|
||||
return ResponseEntity.ok(articleService.from(articleService.findAll()));
|
||||
}
|
||||
|
||||
@RequestMapping(path = ARTICLE_BASE, method = RequestMethod.GET, produces = "application/json")
|
||||
public ResponseEntity<ArticleModel> getByUUID(HttpServletRequest request,
|
||||
@RequestParam(value = PARAM_UUID) UUID uuid) {
|
||||
logRequest(request);
|
||||
return ResponseEntity.ok(articleModelFactory.from(articleService.findByUUID(uuid)));
|
||||
return ResponseEntity.ok(articleService.from(articleService.findByUUID(uuid)));
|
||||
}
|
||||
|
||||
@RequestMapping(path = ARTICLE_BASE, method = RequestMethod.POST, produces = "application/json")
|
||||
|
||||
@@ -0,0 +1,70 @@
|
||||
package de.htwsaar.webshop.controller;
|
||||
|
||||
import de.htwsaar.webshop.model.ImageModel;
|
||||
import de.htwsaar.webshop.repository.entities.Image;
|
||||
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;
|
||||
import org.springframework.http.ResponseEntity;
|
||||
import org.springframework.web.bind.annotation.*;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import static de.htwsaar.webshop.config.ControllerPathConfig.*;
|
||||
import static de.htwsaar.webshop.config.ParameterConfig.*;
|
||||
import static de.htwsaar.webshop.util.LoggerUtil.logRequest;
|
||||
|
||||
@RestController
|
||||
@Slf4j
|
||||
public class ImageController {
|
||||
private final ImageService imageService;
|
||||
private final ValidatorService validatorService;
|
||||
|
||||
@Autowired
|
||||
public ImageController(ImageService imageService, ValidatorService validatorService) {
|
||||
this.imageService = imageService;
|
||||
this.validatorService = validatorService;
|
||||
}
|
||||
|
||||
@RequestMapping(path = IMAGE_GET_ALL, method = RequestMethod.GET, produces = "application/json")
|
||||
public ResponseEntity<List<ImageModel>> getAll(HttpServletRequest request,
|
||||
@RequestParam(value = PARAM_ARTICLE_ID) Long articleId) {
|
||||
logRequest(request);
|
||||
return ResponseEntity.ok(imageService.from(imageService.getImagesByArticleId(articleId)));
|
||||
}
|
||||
|
||||
@RequestMapping(path = IMAGE_BASE, method = RequestMethod.GET, produces = "application/json")
|
||||
public ResponseEntity<ImageModel> getFirst(HttpServletRequest request,
|
||||
@RequestParam(value = PARAM_ARTICLE_ID) Long articleId) {
|
||||
logRequest(request);
|
||||
return ResponseEntity.ok(imageService.from(imageService.getImageByArticleId(articleId)));
|
||||
}
|
||||
|
||||
@RequestMapping(path = IMAGE_BASE, method = RequestMethod.POST, produces = "application/json")
|
||||
public ResponseEntity<Boolean> add(HttpServletRequest request,
|
||||
@RequestBody Image image) {
|
||||
logRequest(request);
|
||||
|
||||
if (validatorService.isInvalid(image)) {
|
||||
log.warn("[{}] failed Validation, sending bad request", request.getRequestURI());
|
||||
return ResponseEntity.badRequest().body(false);
|
||||
}
|
||||
|
||||
Image a = imageService.save(image);
|
||||
return ResponseEntity.ok(a != null);
|
||||
}
|
||||
|
||||
@RequestMapping(path = IMAGE_BASE, method = RequestMethod.DELETE, produces = "application/json")
|
||||
public ResponseEntity<Boolean> update(HttpServletRequest request,
|
||||
@RequestParam(value = PARAM_IMAGE_ID) Long imageId,
|
||||
@RequestBody Image image) {
|
||||
logRequest(request);
|
||||
if (imageId == null || imageService.getImageById(imageId) == null) {
|
||||
return ResponseEntity.badRequest().body(false);
|
||||
}
|
||||
image.setId(imageService.getImageById(imageId).getId());
|
||||
return ResponseEntity.ok(imageService.save(image) != null);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,13 @@
|
||||
package de.htwsaar.webshop.model;
|
||||
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.Getter;
|
||||
|
||||
import java.util.UUID;
|
||||
|
||||
@Getter
|
||||
@AllArgsConstructor
|
||||
public class ImageModel {
|
||||
UUID articleId;
|
||||
String uri;
|
||||
}
|
||||
@@ -11,6 +11,6 @@ import java.util.List;
|
||||
@Repository
|
||||
public interface ImageRepository extends JpaRepository<Image, Long> {
|
||||
List<Image> findAllByArticleId(@NonNull Long articleId);
|
||||
|
||||
Image findImageByArticleId(@NotNull Long articleId);
|
||||
Image findImageById(Long id);
|
||||
}
|
||||
|
||||
@@ -1,11 +0,0 @@
|
||||
package de.htwsaar.webshop.service;
|
||||
|
||||
import de.htwsaar.webshop.model.ArticleModel;
|
||||
import de.htwsaar.webshop.repository.entities.Article;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
public interface ArticleModelFactory {
|
||||
ArticleModel from(Article article);
|
||||
List<ArticleModel> from(List<Article> articles);
|
||||
}
|
||||
@@ -1,5 +1,6 @@
|
||||
package de.htwsaar.webshop.service;
|
||||
|
||||
import de.htwsaar.webshop.model.ArticleModel;
|
||||
import de.htwsaar.webshop.repository.entities.Article;
|
||||
|
||||
import java.util.List;
|
||||
@@ -9,7 +10,18 @@ import java.util.UUID;
|
||||
public interface ArticleService {
|
||||
List<Article> findAll();
|
||||
Article findByUUID(UUID uuid);
|
||||
Article findByTitle(String title);
|
||||
Article findById(Long articleId);
|
||||
void delete(Long id);
|
||||
Article save(Article article);
|
||||
double getRating(Long id);
|
||||
double getRating(UUID uuid);
|
||||
|
||||
ArticleModel from(Article article);
|
||||
|
||||
/**
|
||||
* @param articles Articles to Map
|
||||
* @return an <b>unmodifiable</b> {@link List} of {@link ArticleModel}s
|
||||
*/
|
||||
List<ArticleModel> from(List<Article> articles);
|
||||
}
|
||||
|
||||
@@ -1,12 +1,24 @@
|
||||
package de.htwsaar.webshop.service;
|
||||
|
||||
import de.htwsaar.webshop.model.ImageModel;
|
||||
import de.htwsaar.webshop.repository.entities.Image;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
@SuppressWarnings("unused")
|
||||
public interface ImageService {
|
||||
List<Image> getImageByItemId(Long itemId);
|
||||
List<Image> getImagesByArticleId(Long articleId);
|
||||
|
||||
Image getImageByArticleId(Long imageId);
|
||||
Image saveImage(Image image);
|
||||
|
||||
Image getImageById(Long imageId);
|
||||
|
||||
Image save(Image image);
|
||||
|
||||
ImageModel from(Image image);
|
||||
|
||||
/**
|
||||
* @param images Images to Map
|
||||
* @return an <b>unmodifiable</b> {@link List} of {@link ImageModel}s
|
||||
*/
|
||||
List<ImageModel> from(List<Image> images);
|
||||
}
|
||||
|
||||
@@ -1,48 +0,0 @@
|
||||
package de.htwsaar.webshop.service.impl;
|
||||
|
||||
import de.htwsaar.webshop.model.ArticleModel;
|
||||
import de.htwsaar.webshop.repository.entities.Article;
|
||||
import de.htwsaar.webshop.service.ArticleModelFactory;
|
||||
import de.htwsaar.webshop.service.ArticleService;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
@Service
|
||||
@Slf4j
|
||||
public class ArticleModelFactoryImpl implements ArticleModelFactory {
|
||||
|
||||
private final ArticleService articleService;
|
||||
|
||||
@Autowired
|
||||
public ArticleModelFactoryImpl(ArticleService articleService) {
|
||||
this.articleService = articleService;
|
||||
}
|
||||
|
||||
@Override
|
||||
public ArticleModel from(Article article) {
|
||||
return new ArticleModel(
|
||||
article.getId(),
|
||||
article.getUuid(),
|
||||
article.getName(),
|
||||
article.getDescription(),
|
||||
article.getPrice100(),
|
||||
article.getDiscount100(),
|
||||
article.getStock(),
|
||||
article.getCategory(),
|
||||
articleService.getRating(article.getId())
|
||||
);
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<ArticleModel> from(List<Article> articles) {
|
||||
List<ArticleModel> articleModels = new ArrayList<>();
|
||||
for (Article article : articles) {
|
||||
articleModels.add(from(article));
|
||||
}
|
||||
return articleModels;
|
||||
}
|
||||
}
|
||||
@@ -1,5 +1,6 @@
|
||||
package de.htwsaar.webshop.service.impl;
|
||||
|
||||
import de.htwsaar.webshop.model.ArticleModel;
|
||||
import de.htwsaar.webshop.repository.ArticleRepository;
|
||||
import de.htwsaar.webshop.repository.ReviewRepository;
|
||||
import de.htwsaar.webshop.repository.entities.Article;
|
||||
@@ -9,6 +10,7 @@ import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.UUID;
|
||||
|
||||
@@ -19,7 +21,7 @@ public class ArticleServiceImpl implements ArticleService {
|
||||
private final ReviewRepository reviewRepository;
|
||||
|
||||
@Autowired
|
||||
public ArticleServiceImpl(ArticleRepository articleRepository, ReviewRepository reviewRepository) {
|
||||
public ArticleServiceImpl(ArticleRepository articleRepository, ReviewRepository reviewRepository, ArticleService articleService) {
|
||||
this.articleRepository = articleRepository;
|
||||
this.reviewRepository = reviewRepository;
|
||||
}
|
||||
@@ -34,6 +36,16 @@ public class ArticleServiceImpl implements ArticleService {
|
||||
return articleRepository.findArticleByUuid(uuid).orElse(null);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Article findByTitle(String title) {
|
||||
return articleRepository.findArticleByName(title).orElse(null);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Article findById(Long articleId) {
|
||||
return articleRepository.findArticleById(articleId).orElse(null);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void delete(Long id) {
|
||||
articleRepository.deleteById(id);
|
||||
@@ -46,6 +58,35 @@ public class ArticleServiceImpl implements ArticleService {
|
||||
|
||||
@Override
|
||||
public double getRating(Long id) {
|
||||
return reviewRepository.streamReviewsByArticleId(id).mapToInt(Review::getRating).average().orElse(-1);
|
||||
return reviewRepository.streamReviewsByArticleId(id)
|
||||
.mapToInt(Review::getRating)
|
||||
.average().orElse(-1);
|
||||
}
|
||||
|
||||
@Override
|
||||
public double getRating(UUID uuid) {
|
||||
return reviewRepository.streamReviewsByArticleId(
|
||||
articleRepository.findArticleByUuid(uuid).orElseThrow().getId()
|
||||
).mapToInt(Review::getRating).average().orElse(-1);
|
||||
}
|
||||
|
||||
@Override
|
||||
public ArticleModel from(Article article) {
|
||||
return new ArticleModel(
|
||||
article.getId(),
|
||||
article.getUuid(),
|
||||
article.getName(),
|
||||
article.getDescription(),
|
||||
article.getPrice100(),
|
||||
article.getDiscount100(),
|
||||
article.getStock(),
|
||||
article.getCategory(),
|
||||
getRating(article.getId())
|
||||
);
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<ArticleModel> from(List<Article> articles) {
|
||||
return articles.stream().map(this::from).toList();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,7 +1,9 @@
|
||||
package de.htwsaar.webshop.service.impl;
|
||||
|
||||
import de.htwsaar.webshop.model.ImageModel;
|
||||
import de.htwsaar.webshop.repository.ImageRepository;
|
||||
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;
|
||||
@@ -13,16 +15,18 @@ import java.util.List;
|
||||
@Slf4j
|
||||
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;
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public List<Image> getImageByItemId(Long itemId) {
|
||||
return imageRepository.findAllByArticleId(itemId);
|
||||
public List<Image> getImagesByArticleId(Long articleId) {
|
||||
return imageRepository.findAllByArticleId(articleId);
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -31,7 +35,22 @@ public class ImageServiceImpl implements ImageService {
|
||||
}
|
||||
|
||||
@Override
|
||||
public Image saveImage(Image image) {
|
||||
public Image getImageById(Long imageId) {
|
||||
return imageRepository.findImageById(imageId);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Image save(Image image) {
|
||||
return imageRepository.save(image);
|
||||
}
|
||||
|
||||
@Override
|
||||
public ImageModel from(Image image) {
|
||||
return new ImageModel(articleService.findById(image.getArticleId()).getUuid(),image.getUri());
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<ImageModel> from(List<Image> images) {
|
||||
return images.stream().map(this::from).toList();
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user