diff --git a/.idea/misc.xml b/.idea/misc.xml index 639900d..a31c4c5 100644 --- a/.idea/misc.xml +++ b/.idea/misc.xml @@ -1,6 +1,6 @@ - + \ No newline at end of file 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 a603e0e..46d1399 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 @@ -1,5 +1,6 @@ package de.htwsaar.webshop.config; +@SuppressWarnings("unused") public class ControllerPathConfig { //HealthController public static final String HEALTH = "/health"; @@ -8,10 +9,11 @@ public class ControllerPathConfig { public static final String ERROR = "/error"; //ItemController - public static final String ARTICLE_BASE = "/item"; - public static final String ARTICLE_ADD = "/item/add"; - public static final String ARTICLE_GET = "/item/get"; - public static final String ARTICLE_GETALL = "/item/getAll"; + public static final String ARTICLE_ADD = "/item"; + public static final String ARTICLE_GET = "/item"; + public static final String ARTICLE_UPDATE = "/item"; + public static final String ARTICLE_DELETE = "/item"; + public static final String ARTICLE_GET_ALL = "/item/all"; //ImageController public static final String IMAGE_BASE = "/image"; 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 7d4b2a0..e77423e 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 @@ -1,5 +1,6 @@ package de.htwsaar.webshop.config; +@SuppressWarnings("unused") public class ParameterConfig { public static final String PARAM_ID = "id"; 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 c1c16a1..da7fcdb 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 @@ -4,16 +4,11 @@ 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.persistence.criteria.CriteriaBuilder; 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.stereotype.Controller; -import org.springframework.web.bind.annotation.RequestMapping; -import org.springframework.web.bind.annotation.RequestMethod; -import org.springframework.web.bind.annotation.RequestParam; -import org.springframework.web.bind.annotation.RestController; +import org.springframework.web.bind.annotation.*; import java.util.List; import java.util.UUID; @@ -34,7 +29,7 @@ public class ArticleController { this.articleModelFactory = articleModelFactory; } - @RequestMapping(path = ARTICLE_GETALL, method = RequestMethod.GET, produces = "application/json") + @RequestMapping(path = ARTICLE_GET_ALL, method = RequestMethod.GET, produces = "application/json") public ResponseEntity> getAll(HttpServletRequest request) { logRequest(request); return ResponseEntity.ok(articleModelFactory.from(articleService.findAll())); @@ -42,51 +37,35 @@ public class ArticleController { @RequestMapping(path = ARTICLE_GET, method = RequestMethod.GET, produces = "application/json") public ResponseEntity getByUUID(HttpServletRequest request, - @RequestParam(value = PARAM_UUID) String uuid) { + @RequestParam(value = PARAM_UUID) UUID uuid) { logRequest(request); return ResponseEntity.ok(articleModelFactory.from(articleService.findByUUID(uuid))); } @RequestMapping(path = ARTICLE_ADD, method = RequestMethod.POST, produces = "application/json") public ResponseEntity add(HttpServletRequest request, - @RequestParam(value = PARAM_NAME) String name, - @RequestParam(value = PARAM_STOCK) String stock, - @RequestParam(value = PARAM_DESCRIPTION) String description, - @RequestParam(value = PARAM_PRICE) String price, - @RequestParam(value = PARAM_DISCOUNT) String discount, - @RequestParam(value = PARAM_CATEGORY) String category) { + @RequestBody Article article) { logRequest(request); - int stockInt; - int priceInt; - int discountInt; - try { - stockInt = Integer.parseInt(stock); - priceInt = Integer.parseInt(price); - discountInt = Integer.parseInt(discount); - if (priceInt < 0 || - stockInt < 0 || - discountInt > 100 || - discountInt < 0) { - return ResponseEntity.badRequest().body(false); - } - } catch (Exception e) { - log.warn("[{}] failed Validation: {}, sending bad request", request.getRequestURI(), e.getMessage()); + if (article.getPrice100() < 0 || article.getStock() < 0 || + article.getDiscount100() > 100 || article.getDiscount100() < 0) { + log.warn("[{}] failed Validation, sending bad request", request.getRequestURI()); return ResponseEntity.badRequest().body(false); } - Article a = articleService.save(new Article( - 0L, - UUID.randomUUID().toString(), - stockInt, - name, - description, - priceInt, - discountInt, - category - )); + Article a = articleService.save(article); return ResponseEntity.ok(a != null); } + @RequestMapping(path = ARTICLE_DELETE, method = RequestMethod.DELETE, produces = "application/json") + public ResponseEntity update(HttpServletRequest request, + @RequestParam(value = PARAM_UUID) UUID uuid, + @RequestBody Article article) { + logRequest(request); + if (uuid == null || uuid.toString().isEmpty() || articleService.findByUUID(uuid) == null) { + return ResponseEntity.badRequest().body(false); + } + article.setId(articleService.findByUUID(uuid).getId()); - + return ResponseEntity.ok(articleService.save(article) != null); + } } diff --git a/00-backend/src/main/java/de/htwsaar/webshop/model/ArticleModel.java b/00-backend/src/main/java/de/htwsaar/webshop/model/ArticleModel.java index 1fc5561..f32e269 100644 --- a/00-backend/src/main/java/de/htwsaar/webshop/model/ArticleModel.java +++ b/00-backend/src/main/java/de/htwsaar/webshop/model/ArticleModel.java @@ -1,10 +1,10 @@ package de.htwsaar.webshop.model; -import jakarta.annotation.Nullable; import lombok.AllArgsConstructor; import lombok.Getter; import lombok.Setter; -import org.antlr.v4.runtime.misc.NotNull; + +import java.util.UUID; /** * What the Frontend gets when requesting an Article, POJO @@ -14,7 +14,7 @@ import org.antlr.v4.runtime.misc.NotNull; @Getter public class ArticleModel { private long id; - private String uuid; + private UUID uuid; private String name; private String description; private int price100; diff --git a/00-backend/src/main/java/de/htwsaar/webshop/repository/ArticleRepository.java b/00-backend/src/main/java/de/htwsaar/webshop/repository/ArticleRepository.java index d69fccb..1ae9527 100644 --- a/00-backend/src/main/java/de/htwsaar/webshop/repository/ArticleRepository.java +++ b/00-backend/src/main/java/de/htwsaar/webshop/repository/ArticleRepository.java @@ -6,10 +6,11 @@ import org.springframework.data.jpa.repository.JpaRepository; import org.springframework.stereotype.Repository; import java.util.Optional; +import java.util.UUID; @Repository public interface ArticleRepository extends JpaRepository { Optional
findArticleById(@NonNull Long id); Optional
findArticleByName(@NonNull String Name); - Optional
findArticleByUuid(@NonNull String uuid); + Optional
findArticleByUuid(@NonNull UUID uuid); } diff --git a/00-backend/src/main/java/de/htwsaar/webshop/repository/entities/Article.java b/00-backend/src/main/java/de/htwsaar/webshop/repository/entities/Article.java index da445f5..a9c005e 100644 --- a/00-backend/src/main/java/de/htwsaar/webshop/repository/entities/Article.java +++ b/00-backend/src/main/java/de/htwsaar/webshop/repository/entities/Article.java @@ -1,35 +1,50 @@ package de.htwsaar.webshop.repository.entities; -import jakarta.annotation.*; -import jakarta.persistence.Entity; -import jakarta.persistence.Id; +import jakarta.persistence.*; import jakarta.validation.constraints.Max; import jakarta.validation.constraints.Min; -import jakarta.validation.constraints.NotNull; -import lombok.*; +import lombok.AllArgsConstructor; +import lombok.Getter; +import lombok.NoArgsConstructor; +import lombok.Setter; + +import java.util.List; +import java.util.UUID; @Entity @Getter @Setter @AllArgsConstructor @NoArgsConstructor +@Table(name = "Article") public class Article { @Id - @NonNull + @GeneratedValue(strategy = GenerationType.IDENTITY) + @Column(name = "id", nullable = false) private Long id; - @NotNull - private String uuid; + + @Column(name = "uuid", nullable = false, unique = true) + private UUID uuid; + @Min(0) + @Column(name = "stock", nullable = false) private Integer stock; - @NotNull + + @Column(name = "name", nullable = false) private String name; - @Nullable + + @Column(name = "description", nullable = false) private String description; + @Min(0) + @Column(name = "price100", nullable = false) private Integer price100; + @Min(0) @Max(100) + @Column(name = "discount100", nullable = false) private Integer discount100; - @Nullable + + @Column(name = "catefory", nullable = false) private String category; } 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 170e48f..294a871 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 @@ -1,19 +1,28 @@ package de.htwsaar.webshop.repository.entities; -import jakarta.persistence.Entity; -import jakarta.persistence.Id; +import jakarta.persistence.*; import jakarta.validation.constraints.NotEmpty; import jakarta.validation.constraints.NotNull; +import lombok.AllArgsConstructor; import lombok.Getter; +import lombok.NoArgsConstructor; +import lombok.Setter; @Entity @Getter +@Setter +@NoArgsConstructor +@AllArgsConstructor +@Table(name = "Image") public class Image { @Id + @GeneratedValue(strategy = GenerationType.IDENTITY) + @Column(name = "id", nullable = false) private Long id; - @NotNull + + @Column(name = "articleId", nullable = false) private Long articleId; - @NotNull - @NotEmpty + + @Column(name = "uri", nullable = false) private String uri; } diff --git a/00-backend/src/main/java/de/htwsaar/webshop/repository/entities/Review.java b/00-backend/src/main/java/de/htwsaar/webshop/repository/entities/Review.java index 2738a64..43e2986 100644 --- a/00-backend/src/main/java/de/htwsaar/webshop/repository/entities/Review.java +++ b/00-backend/src/main/java/de/htwsaar/webshop/repository/entities/Review.java @@ -1,27 +1,36 @@ package de.htwsaar.webshop.repository.entities; import jakarta.annotation.Nullable; -import jakarta.persistence.Entity; -import jakarta.persistence.Id; +import jakarta.persistence.*; import jakarta.validation.constraints.Max; import jakarta.validation.constraints.NotNull; import jakarta.validation.constraints.Positive; +import lombok.AllArgsConstructor; import lombok.Getter; +import lombok.NoArgsConstructor; import lombok.Setter; @Entity @Getter @Setter +@AllArgsConstructor +@NoArgsConstructor +@Table(name = "Review") public class Review { @Id + @GeneratedValue(strategy = GenerationType.IDENTITY) + @Column(name = "id", nullable = false) private Long id; - @Nullable + + @Column(name = "content", nullable = false) private String content; - @NotNull + @Positive + @Column(name = "articleId", nullable = false) private Long articleId; - @NotNull + @Positive @Max(10) + @Column(name = "rating", nullable = false) private Integer rating; } diff --git a/00-backend/src/main/java/de/htwsaar/webshop/service/ArticleService.java b/00-backend/src/main/java/de/htwsaar/webshop/service/ArticleService.java index 7bce06e..5856751 100644 --- a/00-backend/src/main/java/de/htwsaar/webshop/service/ArticleService.java +++ b/00-backend/src/main/java/de/htwsaar/webshop/service/ArticleService.java @@ -3,10 +3,11 @@ package de.htwsaar.webshop.service; import de.htwsaar.webshop.repository.entities.Article; import java.util.List; +import java.util.UUID; public interface ArticleService { List
findAll(); - Article findByUUID(String uuid); + Article findByUUID(UUID uuid); void delete(Long id); Article save(Article article); double getRating(Long id); diff --git a/00-backend/src/main/java/de/htwsaar/webshop/service/impl/ArticleServiceImpl.java b/00-backend/src/main/java/de/htwsaar/webshop/service/impl/ArticleServiceImpl.java index e2a9e8e..8cf7f9d 100644 --- a/00-backend/src/main/java/de/htwsaar/webshop/service/impl/ArticleServiceImpl.java +++ b/00-backend/src/main/java/de/htwsaar/webshop/service/impl/ArticleServiceImpl.java @@ -10,6 +10,7 @@ import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import java.util.List; +import java.util.UUID; @Service @Slf4j @@ -29,7 +30,7 @@ public class ArticleServiceImpl implements ArticleService { } @Override - public Article findByUUID(String uuid) { + public Article findByUUID(UUID uuid) { return articleRepository.findArticleByUuid(uuid).orElse(null); }