From edb0a992167fe7de2b6e3cfec462d4541f68c63a Mon Sep 17 00:00:00 2001 From: juanfra312003 <80067016+juanfra312003@users.noreply.github.com> Date: Wed, 2 Oct 2024 17:27:58 -0500 Subject: [PATCH 01/37] :sparkles: Create Product --- .../controllers/ProductsController.java | 32 ++++++++++ .../productsservice/dtos/LabelDto.java | 21 +++++++ .../dtos/ProductCreateDTO.java | 11 ++++ .../productsservice/dtos/ProductDTO.java | 33 ++++++++++ .../dtos/ReportedProductDto.java | 17 +++++ .../productsservice/dtos/Response.java | 4 ++ .../productsservice/dtos/delete.txt | 0 .../productsservice/model/Label.java | 29 +++++++++ .../productsservice/model/Product.java | 63 +++++++++++++++++++ .../productsservice/model/Rating.java | 35 +++++++++++ .../model/ReportedProduct.java | 32 ++++++++++ .../productsservice/model/delete.txt | 0 .../repository/LabelRepository.java | 6 ++ .../repository/ProductRepository.java | 6 ++ .../repository/ReportsRepository.java | 6 ++ .../productsservice/repository/delete.txt | 0 .../services/FirebaseService.java | 25 ++++++++ .../services/ProductService.java | 35 +++++++++++ .../utils/OperationException.java | 14 +++++ .../productsservice/utils/delete.txt | 0 20 files changed, 369 insertions(+) create mode 100644 src/main/java/co/allconnected/fussiontech/productsservice/controllers/ProductsController.java create mode 100644 src/main/java/co/allconnected/fussiontech/productsservice/dtos/LabelDto.java create mode 100644 src/main/java/co/allconnected/fussiontech/productsservice/dtos/ProductCreateDTO.java create mode 100644 src/main/java/co/allconnected/fussiontech/productsservice/dtos/ProductDTO.java create mode 100644 src/main/java/co/allconnected/fussiontech/productsservice/dtos/ReportedProductDto.java create mode 100644 src/main/java/co/allconnected/fussiontech/productsservice/dtos/Response.java delete mode 100644 src/main/java/co/allconnected/fussiontech/productsservice/dtos/delete.txt create mode 100644 src/main/java/co/allconnected/fussiontech/productsservice/model/Label.java create mode 100644 src/main/java/co/allconnected/fussiontech/productsservice/model/Product.java create mode 100644 src/main/java/co/allconnected/fussiontech/productsservice/model/Rating.java create mode 100644 src/main/java/co/allconnected/fussiontech/productsservice/model/ReportedProduct.java delete mode 100644 src/main/java/co/allconnected/fussiontech/productsservice/model/delete.txt create mode 100644 src/main/java/co/allconnected/fussiontech/productsservice/repository/LabelRepository.java create mode 100644 src/main/java/co/allconnected/fussiontech/productsservice/repository/ProductRepository.java create mode 100644 src/main/java/co/allconnected/fussiontech/productsservice/repository/ReportsRepository.java delete mode 100644 src/main/java/co/allconnected/fussiontech/productsservice/repository/delete.txt create mode 100644 src/main/java/co/allconnected/fussiontech/productsservice/services/FirebaseService.java create mode 100644 src/main/java/co/allconnected/fussiontech/productsservice/services/ProductService.java create mode 100644 src/main/java/co/allconnected/fussiontech/productsservice/utils/OperationException.java delete mode 100644 src/main/java/co/allconnected/fussiontech/productsservice/utils/delete.txt diff --git a/src/main/java/co/allconnected/fussiontech/productsservice/controllers/ProductsController.java b/src/main/java/co/allconnected/fussiontech/productsservice/controllers/ProductsController.java new file mode 100644 index 0000000..b35c3c5 --- /dev/null +++ b/src/main/java/co/allconnected/fussiontech/productsservice/controllers/ProductsController.java @@ -0,0 +1,32 @@ +package co.allconnected.fussiontech.productsservice.controllers; + +import co.allconnected.fussiontech.productsservice.dtos.ProductCreateDTO; +import co.allconnected.fussiontech.productsservice.dtos.ProductDTO; +import co.allconnected.fussiontech.productsservice.services.ProductService; +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; + +@RestController +@RequestMapping("/api/v1/products") +public class ProductsController { + private final ProductService productService; + + @Autowired + public ProductsController(ProductService productService) { + this.productService = productService; + } + + @PostMapping + public ResponseEntity createProduct(@ModelAttribute ProductCreateDTO product, @RequestParam(value = "photo_url", required = false) MultipartFile photo) { + try { + ProductDTO productDTO = productService.createProduct(product, photo); + return ResponseEntity.status(HttpStatus.CREATED).body(productDTO); + } catch (Exception e) { + return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR) + .body(null); + } + } +} diff --git a/src/main/java/co/allconnected/fussiontech/productsservice/dtos/LabelDto.java b/src/main/java/co/allconnected/fussiontech/productsservice/dtos/LabelDto.java new file mode 100644 index 0000000..c6e698e --- /dev/null +++ b/src/main/java/co/allconnected/fussiontech/productsservice/dtos/LabelDto.java @@ -0,0 +1,21 @@ +package co.allconnected.fussiontech.productsservice.dtos; + +import co.allconnected.fussiontech.productsservice.model.Label; +import co.allconnected.fussiontech.productsservice.model.Product; +import lombok.Getter; +import lombok.Setter; +import java.io.Serializable; + +@Getter +@Setter +public class LabelDto implements Serializable { + Integer id; + String label; + String [] products; + + public LabelDto(Label label){ + this.id = label.getId(); + this.label = label.getLabel(); + this.products = label.getProducts().stream().map(Product::getName).toArray(String[]::new); + } +} \ No newline at end of file diff --git a/src/main/java/co/allconnected/fussiontech/productsservice/dtos/ProductCreateDTO.java b/src/main/java/co/allconnected/fussiontech/productsservice/dtos/ProductCreateDTO.java new file mode 100644 index 0000000..ea11706 --- /dev/null +++ b/src/main/java/co/allconnected/fussiontech/productsservice/dtos/ProductCreateDTO.java @@ -0,0 +1,11 @@ +package co.allconnected.fussiontech.productsservice.dtos; + +import lombok.Getter; +import lombok.Setter; + +import java.util.UUID; + +@Getter +@Setter +public record ProductCreateDTO (UUID idBusiness, String name, String description, int stock, float price, String status ){ +} diff --git a/src/main/java/co/allconnected/fussiontech/productsservice/dtos/ProductDTO.java b/src/main/java/co/allconnected/fussiontech/productsservice/dtos/ProductDTO.java new file mode 100644 index 0000000..9856223 --- /dev/null +++ b/src/main/java/co/allconnected/fussiontech/productsservice/dtos/ProductDTO.java @@ -0,0 +1,33 @@ +package co.allconnected.fussiontech.productsservice.dtos; + +import co.allconnected.fussiontech.productsservice.model.Label; +import co.allconnected.fussiontech.productsservice.model.Product; +import lombok.Getter; +import lombok.Setter; +import java.io.Serializable; + +@Getter +@Setter +public class ProductDTO implements Serializable { + private Integer id; + private String idBusiness; + private String name; + private String description; + private String photoUrl; + private Integer stock; + private Double price; + private String status; + private String [] labels; + + public ProductDTO(Product product){ + this.id = product.getId(); + this.idBusiness = product.getIdBusiness().toString(); + this.name = product.getName(); + this.description = product.getDescription(); + this.photoUrl = product.getPhotoUrl(); + this.stock = product.getStock(); + this.price = product.getPrice(); + this.status = product.getStatus(); + this.labels = product.getLabels().stream().map(Label::getLabel).toArray(String[]::new); + } +} \ No newline at end of file diff --git a/src/main/java/co/allconnected/fussiontech/productsservice/dtos/ReportedProductDto.java b/src/main/java/co/allconnected/fussiontech/productsservice/dtos/ReportedProductDto.java new file mode 100644 index 0000000..ef7a4d1 --- /dev/null +++ b/src/main/java/co/allconnected/fussiontech/productsservice/dtos/ReportedProductDto.java @@ -0,0 +1,17 @@ +package co.allconnected.fussiontech.productsservice.dtos; + +import lombok.Getter; +import lombok.Setter; + +import java.io.Serializable; +import java.time.Instant; + +@Getter +@Setter +public class ReportedProductDto implements Serializable { + private Integer id; + private Integer productId; + private String reason; + private String description; + private Instant reportDate; +} \ No newline at end of file diff --git a/src/main/java/co/allconnected/fussiontech/productsservice/dtos/Response.java b/src/main/java/co/allconnected/fussiontech/productsservice/dtos/Response.java new file mode 100644 index 0000000..ac7a347 --- /dev/null +++ b/src/main/java/co/allconnected/fussiontech/productsservice/dtos/Response.java @@ -0,0 +1,4 @@ +package co.allconnected.fussiontech.productsservice.dtos; + +public record Response(int code, String message) { +} diff --git a/src/main/java/co/allconnected/fussiontech/productsservice/dtos/delete.txt b/src/main/java/co/allconnected/fussiontech/productsservice/dtos/delete.txt deleted file mode 100644 index e69de29..0000000 diff --git a/src/main/java/co/allconnected/fussiontech/productsservice/model/Label.java b/src/main/java/co/allconnected/fussiontech/productsservice/model/Label.java new file mode 100644 index 0000000..1d443b3 --- /dev/null +++ b/src/main/java/co/allconnected/fussiontech/productsservice/model/Label.java @@ -0,0 +1,29 @@ +package co.allconnected.fussiontech.productsservice.model; + +import jakarta.persistence.*; +import lombok.Getter; +import lombok.Setter; + +import java.util.LinkedHashSet; +import java.util.Set; + +@Getter +@Setter +@Entity +@Table(name = "label", schema = "all_connected_products") +public class Label { + @Id + @GeneratedValue(strategy = GenerationType.IDENTITY) + @Column(name = "id_label", nullable = false) + private Integer id; + + @Column(name = "label", nullable = false, length = 45) + private String label; + + @ManyToMany + @JoinTable(name = "product_label", + joinColumns = @JoinColumn(name = "id_label"), + inverseJoinColumns = @JoinColumn(name = "id_announcement")) + private Set products = new LinkedHashSet<>(); + +} \ No newline at end of file diff --git a/src/main/java/co/allconnected/fussiontech/productsservice/model/Product.java b/src/main/java/co/allconnected/fussiontech/productsservice/model/Product.java new file mode 100644 index 0000000..c2fa260 --- /dev/null +++ b/src/main/java/co/allconnected/fussiontech/productsservice/model/Product.java @@ -0,0 +1,63 @@ +package co.allconnected.fussiontech.productsservice.model; + +import co.allconnected.fussiontech.productsservice.dtos.ProductCreateDTO; +import jakarta.persistence.*; +import lombok.Getter; +import lombok.NoArgsConstructor; +import lombok.Setter; + +import java.util.LinkedHashSet; +import java.util.Set; +import java.util.UUID; + +@Getter +@Setter +@Entity +@NoArgsConstructor +@Table(name = "product", schema = "all_connected_products") +public class Product { + public Product (ProductCreateDTO dto){ + this.idBusiness = dto.getIdBusiness(); + this.name = dto.getName(); + this.description = dto.getDescription(); + this.stock = dto.getStock(); + this.price = (double) dto.getPrice(); + this.status = dto.getStatus(); + } + + + @Id + @GeneratedValue(strategy = GenerationType.IDENTITY) + @Column(name = "id_product", nullable = false) + private Integer id; + + @Column(name = "id_business", nullable = false) + private UUID idBusiness; + + @Column(name = "name", nullable = false, length = 45) + private String name; + + @Column(name = "description", nullable = false, length = 280) + private String description; + + @Column(name = "photo_url", length = 700) + private String photoUrl; + + @Column(name = "stock", nullable = false) + private Integer stock; + + @Column(name = "price", nullable = false) + private Double price; + + @Column(name = "status", nullable = false, length = Integer.MAX_VALUE) + private String status; + + @ManyToMany(mappedBy = "products") + private Set