diff --git a/00-backend/datasource/database.sqlite b/00-backend/datasource/database.sqlite index e0af80c..9d5f4c2 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 cffa3c3..e37e24b 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,7 +25,7 @@ public class ControllerPathConfig { //ImageController public static final String IMAGE_BASE = "/image"; - public static final String IMAGE_GET_ALL = IMAGE_BASE + "/all"; + public static final String IMAGE_ALL = IMAGE_BASE + "/all"; //OrderController public static final String ORDER_BASE = "/order"; 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 07b9cf3..3613c2b 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 @@ -2,7 +2,6 @@ package de.htwsaar.webshop.config; @SuppressWarnings("unused") public class ParameterConfig { - public static final String PARAM_ID = "id"; public static final String PARAM_UUID = "uuid"; public static final String PARAM_ARTICLE_ID = "articleId"; @@ -11,4 +10,5 @@ public class ParameterConfig { public static final String PARAM_CUSTOMER_ID = "customerId"; public static final String PARAM_URI = "uri"; public static final String PARAM_RATING = "rating"; + public static final String PARAM_IMAGE = "image"; } 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 8acf6c3..9824ab8 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 @@ -1,6 +1,5 @@ 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; @@ -10,12 +9,13 @@ import org.springframework.beans.factory.annotation.Autowired; import org.springframework.http.HttpStatus; import org.springframework.http.ResponseEntity; import org.springframework.web.bind.annotation.*; +import org.springframework.web.multipart.MultipartFile; +import java.util.ArrayList; 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; +import static de.htwsaar.webshop.config.ControllerPathConfig.*; import static de.htwsaar.webshop.config.ParameterConfig.*; import static de.htwsaar.webshop.util.LoggerUtil.logRequest; @@ -31,44 +31,67 @@ public class ImageController { this.articleService = articleService; } - @RequestMapping(path = IMAGE_GET_ALL, method = RequestMethod.GET, produces = "application/json") - public ResponseEntity> getAll(HttpServletRequest request, + @RequestMapping(path = IMAGE_ALL, method = RequestMethod.GET, produces = "application/json") + public ResponseEntity> getAll(HttpServletRequest request, @RequestParam(value = PARAM_UUID) UUID uuid) { logRequest(request); List images = imageService.getImagesByUUID(uuid); if(images.isEmpty()) { return new ResponseEntity<>(HttpStatus.NO_CONTENT); } - return ResponseEntity.ok(imageService.from(images)); + List images_base = new ArrayList<>(); + images.forEach(image -> images_base.add(image.getBase64())); + + return ResponseEntity.ok(images_base); } - @RequestMapping(path = IMAGE_BASE, method = RequestMethod.GET, produces = "application/json") - public ResponseEntity getFirst(HttpServletRequest request, - @RequestParam(value = PARAM_UUID) UUID uuid) { + @RequestMapping(path = IMAGE_BASE, method = RequestMethod.GET, produces = "image/*") + public ResponseEntity getFirst(HttpServletRequest request, + @RequestParam(value = PARAM_UUID) UUID uuid) { logRequest(request); Image image = imageService.getImageByUUID(uuid); if(image == null) { return new ResponseEntity<>(HttpStatus.NO_CONTENT); } - return ResponseEntity.ok(imageService.from(image)); + return ResponseEntity.ok(image.getBase64()); } @RequestMapping(path = IMAGE_BASE, method = RequestMethod.POST, produces = "application/json") public ResponseEntity add(HttpServletRequest request, - @RequestParam(value = PARAM_UUID) UUID articleUuid, - @RequestParam(value = PARAM_URI) String uri) { + @RequestParam(value = PARAM_UUID) UUID articleUuid, + @RequestParam(value = PARAM_IMAGE) MultipartFile file) { logRequest(request); if (articleUuid == null || articleService.findByUUID(articleUuid) == null - || uri == null || uri.isEmpty()) { + || file == null || file.isEmpty()) { log.warn("[{}] failed Validation, sending bad request", request.getRequestURI()); return ResponseEntity.badRequest().body(false); } - Image a = imageService.save(articleUuid, uri); + Image a = imageService.save(articleUuid, file); return ResponseEntity.ok(a != null); } + @RequestMapping(path = IMAGE_ALL, method = RequestMethod.POST, produces = "application/json") + public ResponseEntity addAll(HttpServletRequest request, + @RequestParam(value = PARAM_UUID) UUID articleUuid, + @RequestParam(value = PARAM_IMAGE) List files) { + logRequest(request); + + if (articleUuid == null || articleService.findByUUID(articleUuid) == null + || files == null || files.isEmpty()) { + log.warn("[{}] failed Validation, sending bad request", request.getRequestURI()); + return ResponseEntity.badRequest().body(0); + } + int[] successful = {0}; + files.forEach(mpf -> { + if (imageService.save(articleUuid, mpf) != null) { + successful[0]++; + } + }); + return ResponseEntity.ok(successful[0]); + } + @RequestMapping(path = IMAGE_BASE, method = RequestMethod.PUT, produces = "application/json") public ResponseEntity update(HttpServletRequest request, @RequestParam(value = PARAM_ID) Long imageId, 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 index d70e70b..cfb1a4e 100644 --- a/00-backend/src/main/java/de/htwsaar/webshop/controller/ImageHardcodeController.java +++ b/00-backend/src/main/java/de/htwsaar/webshop/controller/ImageHardcodeController.java @@ -9,10 +9,7 @@ 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 diff --git a/00-backend/src/main/java/de/htwsaar/webshop/model/ImageModel.java b/00-backend/src/main/java/de/htwsaar/webshop/model/ImageModel.java deleted file mode 100644 index 3d5240d..0000000 --- a/00-backend/src/main/java/de/htwsaar/webshop/model/ImageModel.java +++ /dev/null @@ -1,13 +0,0 @@ -package de.htwsaar.webshop.model; - -import lombok.AllArgsConstructor; -import lombok.Getter; - -import java.util.UUID; - -@Getter -@AllArgsConstructor -public class ImageModel { - UUID articleId; - String uri; -} 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 d9ca6de..bb40f2c 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 @@ -20,6 +20,6 @@ public class Image { @JoinColumn(name = "article_id", referencedColumnName = "id", nullable = false) private Article article; - @Column(name = "uri", nullable = false) - private String uri; + @Column(name = "base64", nullable = false) + private String base64; } 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 69447e4..5a77d2b 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 @@ -1,33 +1,23 @@ package de.htwsaar.webshop.service; -import de.htwsaar.webshop.model.ImageModel; import de.htwsaar.webshop.repository.entities.Image; +import org.springframework.web.multipart.MultipartFile; import java.util.List; import java.util.UUID; public interface ImageService { - List getImagesByArticleId(Long articleId); - List getImagesByUUID(UUID uuid); Image getImageByUUID(UUID uuid); - Image getImageByArticleId(Long imageId); - Image getImageById(Long imageId); Image save(Image image); Image save(UUID uuid, String uri); + Image save(UUID uuid, MultipartFile file); + void deleteById(Long imageId); - - ImageModel from(Image image); - - /** - * @param images Images to Map - * @return an unmodifiable {@link List} of {@link ImageModel}s - */ - List from(List images); } 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 ba7251a..4694f82 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 @@ -1,6 +1,5 @@ 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; @@ -10,7 +9,9 @@ import lombok.extern.slf4j.Slf4j; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; +import org.springframework.web.multipart.MultipartFile; +import java.util.Base64; import java.util.List; import java.util.UUID; @@ -27,17 +28,6 @@ public class ImageServiceImpl implements ImageService { this.articleService = articleService; } - - @Override - public List getImagesByArticleId(Long articleId) { - return imageRepository.findAllByArticleId(articleId); - } - - @Override - public Image getImageByArticleId(Long imageId) { - return imageRepository.findImageByArticleId(imageId); - } - @Override public List getImagesByUUID(UUID uuid) { return imageRepository.findImagesByArticle_Uuid(uuid); @@ -71,19 +61,38 @@ public class ImageServiceImpl implements ImageService { return imageRepository.save(image); } + @Override + public Image save(UUID uuid, MultipartFile file) { + if(uuid == null) { + log.warn("Got no UUID, aborting"); + return null; + } + Article article = articleService.findByUUID(uuid); + if(article == null) { + log.warn("Could not find article with id {}", uuid); + return null; + } + if (file == null) { + log.warn("Got no file for {}", uuid); + return null; + } + + try { + String based64 = Base64.getEncoder().encodeToString(file.getBytes()); + if(based64 == null || based64.isEmpty()) { + log.warn("Could not save image with id {} and file size {}", uuid, file.getSize()); + return null; + } + return imageRepository.save(new Image(null, article, based64)); + } catch (Exception e) { + log.warn("Error saving image {} for article {}", file, uuid, e); + } + return null; + } + @Override @Transactional public void deleteById(Long imageId) { imageRepository.deleteById(imageId); } - - @Override - public ImageModel from(Image image) { - return new ImageModel(image.getArticle().getUuid(), image.getUri()); - } - - @Override - public List from(List images) { - return images.stream().map(this::from).toList(); - } } diff --git a/00-backend/src/main/resources/db/initdb.sql b/00-backend/src/main/resources/db/initdb.sql index 45972bb..ae9c194 100644 --- a/00-backend/src/main/resources/db/initdb.sql +++ b/00-backend/src/main/resources/db/initdb.sql @@ -18,7 +18,7 @@ CREATE TABLE IF NOT EXISTS Images ( id INTEGER PRIMARY KEY NOT NULL, article_id INTEGER NOT NULL, - uri TEXT NOT NULL, + base64 TEXT NOT NULL, FOREIGN KEY (article_id) REFERENCES Articles (id) );