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"?>
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
<project version="4">
|
<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" />
|
<output url="file://$PROJECT_DIR$/out" />
|
||||||
</component>
|
</component>
|
||||||
</project>
|
</project>
|
||||||
@@ -1,5 +1,6 @@
|
|||||||
package de.htwsaar.webshop.config;
|
package de.htwsaar.webshop.config;
|
||||||
|
|
||||||
|
@SuppressWarnings("unused")
|
||||||
public class ControllerPathConfig {
|
public class ControllerPathConfig {
|
||||||
//HealthController
|
//HealthController
|
||||||
public static final String HEALTH = "/health";
|
public static final String HEALTH = "/health";
|
||||||
@@ -8,10 +9,11 @@ public class ControllerPathConfig {
|
|||||||
public static final String ERROR = "/error";
|
public static final String ERROR = "/error";
|
||||||
|
|
||||||
//ItemController
|
//ItemController
|
||||||
public static final String ARTICLE_BASE = "/item";
|
public static final String ARTICLE_ADD = "/item";
|
||||||
public static final String ARTICLE_ADD = "/item/add";
|
public static final String ARTICLE_GET = "/item";
|
||||||
public static final String ARTICLE_GET = "/item/get";
|
public static final String ARTICLE_UPDATE = "/item";
|
||||||
public static final String ARTICLE_GETALL = "/item/getAll";
|
public static final String ARTICLE_DELETE = "/item";
|
||||||
|
public static final String ARTICLE_GET_ALL = "/item/all";
|
||||||
|
|
||||||
//ImageController
|
//ImageController
|
||||||
public static final String IMAGE_BASE = "/image";
|
public static final String IMAGE_BASE = "/image";
|
||||||
|
|||||||
@@ -1,5 +1,6 @@
|
|||||||
package de.htwsaar.webshop.config;
|
package de.htwsaar.webshop.config;
|
||||||
|
|
||||||
|
@SuppressWarnings("unused")
|
||||||
public class ParameterConfig {
|
public class ParameterConfig {
|
||||||
|
|
||||||
public static final String PARAM_ID = "id";
|
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.repository.entities.Article;
|
||||||
import de.htwsaar.webshop.service.ArticleModelFactory;
|
import de.htwsaar.webshop.service.ArticleModelFactory;
|
||||||
import de.htwsaar.webshop.service.ArticleService;
|
import de.htwsaar.webshop.service.ArticleService;
|
||||||
import jakarta.persistence.criteria.CriteriaBuilder;
|
|
||||||
import jakarta.servlet.http.HttpServletRequest;
|
import jakarta.servlet.http.HttpServletRequest;
|
||||||
import lombok.extern.slf4j.Slf4j;
|
import lombok.extern.slf4j.Slf4j;
|
||||||
import org.springframework.beans.factory.annotation.Autowired;
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
import org.springframework.http.ResponseEntity;
|
import org.springframework.http.ResponseEntity;
|
||||||
import org.springframework.stereotype.Controller;
|
import org.springframework.web.bind.annotation.*;
|
||||||
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 java.util.List;
|
import java.util.List;
|
||||||
import java.util.UUID;
|
import java.util.UUID;
|
||||||
@@ -34,7 +29,7 @@ public class ArticleController {
|
|||||||
this.articleModelFactory = articleModelFactory;
|
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) {
|
public ResponseEntity<List<ArticleModel>> getAll(HttpServletRequest request) {
|
||||||
logRequest(request);
|
logRequest(request);
|
||||||
return ResponseEntity.ok(articleModelFactory.from(articleService.findAll()));
|
return ResponseEntity.ok(articleModelFactory.from(articleService.findAll()));
|
||||||
@@ -42,51 +37,35 @@ public class ArticleController {
|
|||||||
|
|
||||||
@RequestMapping(path = ARTICLE_GET, method = RequestMethod.GET, produces = "application/json")
|
@RequestMapping(path = ARTICLE_GET, method = RequestMethod.GET, produces = "application/json")
|
||||||
public ResponseEntity<ArticleModel> getByUUID(HttpServletRequest request,
|
public ResponseEntity<ArticleModel> getByUUID(HttpServletRequest request,
|
||||||
@RequestParam(value = PARAM_UUID) String uuid) {
|
@RequestParam(value = PARAM_UUID) UUID uuid) {
|
||||||
logRequest(request);
|
logRequest(request);
|
||||||
return ResponseEntity.ok(articleModelFactory.from(articleService.findByUUID(uuid)));
|
return ResponseEntity.ok(articleModelFactory.from(articleService.findByUUID(uuid)));
|
||||||
}
|
}
|
||||||
|
|
||||||
@RequestMapping(path = ARTICLE_ADD, method = RequestMethod.POST, produces = "application/json")
|
@RequestMapping(path = ARTICLE_ADD, method = RequestMethod.POST, produces = "application/json")
|
||||||
public ResponseEntity<Boolean> add(HttpServletRequest request,
|
public ResponseEntity<Boolean> add(HttpServletRequest request,
|
||||||
@RequestParam(value = PARAM_NAME) String name,
|
@RequestBody Article article) {
|
||||||
@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) {
|
|
||||||
logRequest(request);
|
logRequest(request);
|
||||||
int stockInt;
|
if (article.getPrice100() < 0 || article.getStock() < 0 ||
|
||||||
int priceInt;
|
article.getDiscount100() > 100 || article.getDiscount100() < 0) {
|
||||||
int discountInt;
|
log.warn("[{}] failed Validation, sending bad request", request.getRequestURI());
|
||||||
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());
|
|
||||||
return ResponseEntity.badRequest().body(false);
|
return ResponseEntity.badRequest().body(false);
|
||||||
}
|
}
|
||||||
|
|
||||||
Article a = articleService.save(new Article(
|
Article a = articleService.save(article);
|
||||||
0L,
|
|
||||||
UUID.randomUUID().toString(),
|
|
||||||
stockInt,
|
|
||||||
name,
|
|
||||||
description,
|
|
||||||
priceInt,
|
|
||||||
discountInt,
|
|
||||||
category
|
|
||||||
));
|
|
||||||
return ResponseEntity.ok(a != null);
|
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;
|
package de.htwsaar.webshop.model;
|
||||||
|
|
||||||
import jakarta.annotation.Nullable;
|
|
||||||
import lombok.AllArgsConstructor;
|
import lombok.AllArgsConstructor;
|
||||||
import lombok.Getter;
|
import lombok.Getter;
|
||||||
import lombok.Setter;
|
import lombok.Setter;
|
||||||
import org.antlr.v4.runtime.misc.NotNull;
|
|
||||||
|
import java.util.UUID;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* What the Frontend gets when requesting an Article, POJO
|
* What the Frontend gets when requesting an Article, POJO
|
||||||
@@ -14,7 +14,7 @@ import org.antlr.v4.runtime.misc.NotNull;
|
|||||||
@Getter
|
@Getter
|
||||||
public class ArticleModel {
|
public class ArticleModel {
|
||||||
private long id;
|
private long id;
|
||||||
private String uuid;
|
private UUID uuid;
|
||||||
private String name;
|
private String name;
|
||||||
private String description;
|
private String description;
|
||||||
private int price100;
|
private int price100;
|
||||||
|
|||||||
@@ -6,10 +6,11 @@ import org.springframework.data.jpa.repository.JpaRepository;
|
|||||||
import org.springframework.stereotype.Repository;
|
import org.springframework.stereotype.Repository;
|
||||||
|
|
||||||
import java.util.Optional;
|
import java.util.Optional;
|
||||||
|
import java.util.UUID;
|
||||||
|
|
||||||
@Repository
|
@Repository
|
||||||
public interface ArticleRepository extends JpaRepository<Article, Long> {
|
public interface ArticleRepository extends JpaRepository<Article, Long> {
|
||||||
Optional<Article> findArticleById(@NonNull Long id);
|
Optional<Article> findArticleById(@NonNull Long id);
|
||||||
Optional<Article> findArticleByName(@NonNull String Name);
|
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;
|
package de.htwsaar.webshop.repository.entities;
|
||||||
|
|
||||||
import jakarta.annotation.*;
|
import jakarta.persistence.*;
|
||||||
import jakarta.persistence.Entity;
|
|
||||||
import jakarta.persistence.Id;
|
|
||||||
import jakarta.validation.constraints.Max;
|
import jakarta.validation.constraints.Max;
|
||||||
import jakarta.validation.constraints.Min;
|
import jakarta.validation.constraints.Min;
|
||||||
import jakarta.validation.constraints.NotNull;
|
import lombok.AllArgsConstructor;
|
||||||
import lombok.*;
|
import lombok.Getter;
|
||||||
|
import lombok.NoArgsConstructor;
|
||||||
|
import lombok.Setter;
|
||||||
|
|
||||||
|
import java.util.List;
|
||||||
|
import java.util.UUID;
|
||||||
|
|
||||||
@Entity
|
@Entity
|
||||||
@Getter
|
@Getter
|
||||||
@Setter
|
@Setter
|
||||||
@AllArgsConstructor
|
@AllArgsConstructor
|
||||||
@NoArgsConstructor
|
@NoArgsConstructor
|
||||||
|
@Table(name = "Article")
|
||||||
public class Article {
|
public class Article {
|
||||||
@Id
|
@Id
|
||||||
@NonNull
|
@GeneratedValue(strategy = GenerationType.IDENTITY)
|
||||||
|
@Column(name = "id", nullable = false)
|
||||||
private Long id;
|
private Long id;
|
||||||
@NotNull
|
|
||||||
private String uuid;
|
@Column(name = "uuid", nullable = false, unique = true)
|
||||||
|
private UUID uuid;
|
||||||
|
|
||||||
@Min(0)
|
@Min(0)
|
||||||
|
@Column(name = "stock", nullable = false)
|
||||||
private Integer stock;
|
private Integer stock;
|
||||||
@NotNull
|
|
||||||
|
@Column(name = "name", nullable = false)
|
||||||
private String name;
|
private String name;
|
||||||
@Nullable
|
|
||||||
|
@Column(name = "description", nullable = false)
|
||||||
private String description;
|
private String description;
|
||||||
|
|
||||||
@Min(0)
|
@Min(0)
|
||||||
|
@Column(name = "price100", nullable = false)
|
||||||
private Integer price100;
|
private Integer price100;
|
||||||
|
|
||||||
@Min(0)
|
@Min(0)
|
||||||
@Max(100)
|
@Max(100)
|
||||||
|
@Column(name = "discount100", nullable = false)
|
||||||
private Integer discount100;
|
private Integer discount100;
|
||||||
@Nullable
|
|
||||||
|
@Column(name = "catefory", nullable = false)
|
||||||
private String category;
|
private String category;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,19 +1,28 @@
|
|||||||
package de.htwsaar.webshop.repository.entities;
|
package de.htwsaar.webshop.repository.entities;
|
||||||
|
|
||||||
import jakarta.persistence.Entity;
|
import jakarta.persistence.*;
|
||||||
import jakarta.persistence.Id;
|
|
||||||
import jakarta.validation.constraints.NotEmpty;
|
import jakarta.validation.constraints.NotEmpty;
|
||||||
import jakarta.validation.constraints.NotNull;
|
import jakarta.validation.constraints.NotNull;
|
||||||
|
import lombok.AllArgsConstructor;
|
||||||
import lombok.Getter;
|
import lombok.Getter;
|
||||||
|
import lombok.NoArgsConstructor;
|
||||||
|
import lombok.Setter;
|
||||||
|
|
||||||
@Entity
|
@Entity
|
||||||
@Getter
|
@Getter
|
||||||
|
@Setter
|
||||||
|
@NoArgsConstructor
|
||||||
|
@AllArgsConstructor
|
||||||
|
@Table(name = "Image")
|
||||||
public class Image {
|
public class Image {
|
||||||
@Id
|
@Id
|
||||||
|
@GeneratedValue(strategy = GenerationType.IDENTITY)
|
||||||
|
@Column(name = "id", nullable = false)
|
||||||
private Long id;
|
private Long id;
|
||||||
@NotNull
|
|
||||||
|
@Column(name = "articleId", nullable = false)
|
||||||
private Long articleId;
|
private Long articleId;
|
||||||
@NotNull
|
|
||||||
@NotEmpty
|
@Column(name = "uri", nullable = false)
|
||||||
private String uri;
|
private String uri;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,27 +1,36 @@
|
|||||||
package de.htwsaar.webshop.repository.entities;
|
package de.htwsaar.webshop.repository.entities;
|
||||||
|
|
||||||
import jakarta.annotation.Nullable;
|
import jakarta.annotation.Nullable;
|
||||||
import jakarta.persistence.Entity;
|
import jakarta.persistence.*;
|
||||||
import jakarta.persistence.Id;
|
|
||||||
import jakarta.validation.constraints.Max;
|
import jakarta.validation.constraints.Max;
|
||||||
import jakarta.validation.constraints.NotNull;
|
import jakarta.validation.constraints.NotNull;
|
||||||
import jakarta.validation.constraints.Positive;
|
import jakarta.validation.constraints.Positive;
|
||||||
|
import lombok.AllArgsConstructor;
|
||||||
import lombok.Getter;
|
import lombok.Getter;
|
||||||
|
import lombok.NoArgsConstructor;
|
||||||
import lombok.Setter;
|
import lombok.Setter;
|
||||||
|
|
||||||
@Entity
|
@Entity
|
||||||
@Getter
|
@Getter
|
||||||
@Setter
|
@Setter
|
||||||
|
@AllArgsConstructor
|
||||||
|
@NoArgsConstructor
|
||||||
|
@Table(name = "Review")
|
||||||
public class Review {
|
public class Review {
|
||||||
@Id
|
@Id
|
||||||
|
@GeneratedValue(strategy = GenerationType.IDENTITY)
|
||||||
|
@Column(name = "id", nullable = false)
|
||||||
private Long id;
|
private Long id;
|
||||||
@Nullable
|
|
||||||
|
@Column(name = "content", nullable = false)
|
||||||
private String content;
|
private String content;
|
||||||
@NotNull
|
|
||||||
@Positive
|
@Positive
|
||||||
|
@Column(name = "articleId", nullable = false)
|
||||||
private Long articleId;
|
private Long articleId;
|
||||||
@NotNull
|
|
||||||
@Positive
|
@Positive
|
||||||
@Max(10)
|
@Max(10)
|
||||||
|
@Column(name = "rating", nullable = false)
|
||||||
private Integer rating;
|
private Integer rating;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -3,10 +3,11 @@ package de.htwsaar.webshop.service;
|
|||||||
import de.htwsaar.webshop.repository.entities.Article;
|
import de.htwsaar.webshop.repository.entities.Article;
|
||||||
|
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
import java.util.UUID;
|
||||||
|
|
||||||
public interface ArticleService {
|
public interface ArticleService {
|
||||||
List<Article> findAll();
|
List<Article> findAll();
|
||||||
Article findByUUID(String uuid);
|
Article findByUUID(UUID uuid);
|
||||||
void delete(Long id);
|
void delete(Long id);
|
||||||
Article save(Article article);
|
Article save(Article article);
|
||||||
double getRating(Long id);
|
double getRating(Long id);
|
||||||
|
|||||||
@@ -10,6 +10,7 @@ import org.springframework.beans.factory.annotation.Autowired;
|
|||||||
import org.springframework.stereotype.Service;
|
import org.springframework.stereotype.Service;
|
||||||
|
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
import java.util.UUID;
|
||||||
|
|
||||||
@Service
|
@Service
|
||||||
@Slf4j
|
@Slf4j
|
||||||
@@ -29,7 +30,7 @@ public class ArticleServiceImpl implements ArticleService {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public Article findByUUID(String uuid) {
|
public Article findByUUID(UUID uuid) {
|
||||||
return articleRepository.findArticleByUuid(uuid).orElse(null);
|
return articleRepository.findArticleByUuid(uuid).orElse(null);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user