Refactored changes.
This commit is contained in:
2
.idea/misc.xml
generated
2
.idea/misc.xml
generated
@@ -1,6 +1,6 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project version="4">
|
||||
<component name="ProjectRootManager">
|
||||
<component name="ProjectRootManager" version="2" project-jdk-name="corretto-17 (2)" project-jdk-type="JavaSDK">
|
||||
<output url="file://$PROJECT_DIR$/out" />
|
||||
</component>
|
||||
</project>
|
||||
@@ -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";
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
package de.htwsaar.webshop.config;
|
||||
|
||||
@SuppressWarnings("unused")
|
||||
public class ParameterConfig {
|
||||
|
||||
public static final String PARAM_ID = "id";
|
||||
|
||||
@@ -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<List<ArticleModel>> 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<ArticleModel> 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<Boolean> 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<Boolean> 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);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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;
|
||||
|
||||
@@ -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<Article, Long> {
|
||||
Optional<Article> findArticleById(@NonNull Long id);
|
||||
Optional<Article> findArticleByName(@NonNull String Name);
|
||||
Optional<Article> findArticleByUuid(@NonNull String uuid);
|
||||
Optional<Article> findArticleByUuid(@NonNull UUID uuid);
|
||||
}
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
|
||||
@@ -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<Article> findAll();
|
||||
Article findByUUID(String uuid);
|
||||
Article findByUUID(UUID uuid);
|
||||
void delete(Long id);
|
||||
Article save(Article article);
|
||||
double getRating(Long id);
|
||||
|
||||
@@ -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);
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user