Skip to content

Commit

Permalink
🔀 Merge pull request #9 from FusionTech-2430/develop
Browse files Browse the repository at this point in the history
🚀 Release v1.0
  • Loading branch information
Estebans441 authored Nov 6, 2024
2 parents e5b3e28 + fbd06c2 commit f778cd2
Show file tree
Hide file tree
Showing 18 changed files with 409 additions and 107 deletions.

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -20,28 +20,27 @@ public LabelsController(LabelService labelService) {
}

@PostMapping
public ResponseEntity<LabelDTO> createLabel(String name) {
try{
public ResponseEntity<LabelDTO> createLabel(@RequestParam String name) {
try {
if (name == null || name.trim().isEmpty()) {
return ResponseEntity.status(HttpStatus.BAD_REQUEST).body(null); // Manejo del error si 'name' es nulo o vacío
}
LabelDTO labelDTO = labelService.createLabel(name);
return ResponseEntity.status(HttpStatus.CREATED).body(labelDTO);
}
catch (Exception e) {
} catch (Exception e) {
return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(null);
}
}

@PutMapping("/{id}")
public ResponseEntity<?> updateLabel (@PathVariable String id, @ModelAttribute LabelDTO labelDTO){
try{
return ResponseEntity.status(HttpStatus.OK).body(labelService.updateLabel(id, labelDTO));
}
catch (OperationException e) {
public ResponseEntity<?> updateLabel(@PathVariable String id, @RequestParam String name) {
try {
return ResponseEntity.status(HttpStatus.OK).body(labelService.updateLabel(id, name));
} catch (OperationException e) {
return ResponseEntity.status(e.getCode()).body(new Response(e.getCode(), e.getMessage()));
} catch (Exception e) {
return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(new Response(HttpStatus.INTERNAL_SERVER_ERROR.value(), "Unexpected error occurred: " + e.getMessage()));
}
}

@GetMapping("/{id}")
public ResponseEntity<?> getLabel(@PathVariable String id) {
try {
Expand All @@ -57,8 +56,6 @@ public ResponseEntity<?> getLabel(@PathVariable String id) {
public ResponseEntity<?> getLabels() {
try {
LabelDTO[] listLabelsDTO = labelService.getLabels();
if (listLabelsDTO.length == 0)
return ResponseEntity.status(HttpStatus.NOT_FOUND).body(new Response(HttpStatus.NOT_FOUND.value(), "No labels found"));
return ResponseEntity.status(HttpStatus.OK).body(listLabelsDTO);
} catch (OperationException e) {
return ResponseEntity.status(e.getCode()).body(new Response(e.getCode(), e.getMessage()));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,30 +18,30 @@ public class ProductsController {
public ProductsController(ProductService productService) {
this.productService = productService;
}

/*
CRUD PRODUCTS
*/
@PostMapping
public ResponseEntity<ProductDTO> createProduct(@ModelAttribute ProductCreateDTO product, @RequestParam(value = "photo_url", required = false) MultipartFile photo) {
public ResponseEntity<?> createProduct(@ModelAttribute ProductCreateDTO product, @RequestParam(value = "photo", required = false) MultipartFile photo) {
try {
System.out.println(product.idBusiness());
ProductDTO productDTO = productService.createProduct(product, photo);
return ResponseEntity.status(HttpStatus.CREATED).body(productDTO);
} catch (OperationException e) {
return ResponseEntity.status(e.getCode()).body(new Response(e.getCode(), e.getMessage()));
} catch (Exception e) {
return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR)
.body(null);
return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(new Response(HttpStatus.INTERNAL_SERVER_ERROR.value(), "Unexpected error occurred: " + e.getMessage()));
}
}
@PutMapping("/{id}")
public ResponseEntity<?> updateProduct(@PathVariable String id, @ModelAttribute ProductCreateDTO product, @RequestParam(value = "photo_url", required = false) MultipartFile photo) {
public ResponseEntity<?> updateProduct(@PathVariable String id, @ModelAttribute ProductCreateDTO product, @RequestParam(value = "photo", required = false) MultipartFile photo) {
try {
return ResponseEntity.status(HttpStatus.OK).body(productService.updateProduct(id, product, photo));
} catch (OperationException e) {
return ResponseEntity.status(e.getCode()).body(new Response(e.getCode(), e.getMessage()));
} catch (Exception e) {
return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(new Response(HttpStatus.INTERNAL_SERVER_ERROR.value(), "Unexpected error occurred: " + e.getMessage()));
}

}
@GetMapping("/{id}")
public ResponseEntity<?> getProduct(@PathVariable String id) {
Expand All @@ -57,8 +57,6 @@ public ResponseEntity<?> getProduct(@PathVariable String id) {
public ResponseEntity<?> getProductsByBusiness(@PathVariable String id_business) {
try {
ProductDTO[] listProductsDTO = productService.getProductsByBusiness(id_business);
if (listProductsDTO.length == 0)
return ResponseEntity.status(HttpStatus.NOT_FOUND).body(new Response(HttpStatus.NOT_FOUND.value(), "No products found"));
return ResponseEntity.status(HttpStatus.OK).body(listProductsDTO);
} catch (OperationException e) {
return ResponseEntity.status(e.getCode()).body(new Response(e.getCode(), e.getMessage()));
Expand All @@ -70,8 +68,6 @@ public ResponseEntity<?> getProductsByBusiness(@PathVariable String id_business)
public ResponseEntity<?> getProducts() {
try {
ProductDTO[] listProductsDTO = productService.getProducts();
if (listProductsDTO.length == 0)
return ResponseEntity.status(HttpStatus.NOT_FOUND).body(new Response(HttpStatus.NOT_FOUND.value(), "No products found"));
return ResponseEntity.status(HttpStatus.OK).body(listProductsDTO);
} catch (OperationException e) {
return ResponseEntity.status(e.getCode()).body(new Response(e.getCode(), e.getMessage()));
Expand All @@ -94,24 +90,24 @@ public ResponseEntity<?> deleteProduct(@PathVariable String id) {
/*
OPERATIONS PRODUCTS - LABELS
*/
@PostMapping("({id_product}/labels/{id_label}")
@PostMapping("/{id_product}/labels/{id_label}")
public ResponseEntity<?> addLabel(@PathVariable String id_product, @PathVariable String id_label) {
try {
productService.assignLabelToProduct(id_product, id_label);
return ResponseEntity.status(HttpStatus.OK)
.body("Label assigned to product successfully.");
.body(new Response(HttpStatus.OK.value(), "Label assigned to product successfully."));
} catch (OperationException e) {
return ResponseEntity.status(e.getCode()).body(new Response(e.getCode(), e.getMessage()));
} catch (Exception e) {
return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(new Response(HttpStatus.INTERNAL_SERVER_ERROR.value(), "Unexpected error occurred: " + e.getMessage()));
}
}
@DeleteMapping("/{id_product}/labels/{id_label}")
@DeleteMapping("/{id_product}/labels/{id_label}/delete")
public ResponseEntity<?> deleteLabel(@PathVariable String id_product, @PathVariable String id_label) {
try {
productService.deleteLabelFromProduct(id_product, id_label);
return ResponseEntity.status(HttpStatus.OK)
.body("Label deleted from product successfully.");
.body(new Response(HttpStatus.OK.value(), "Label deleted from product successfully."));
} catch (OperationException e) {
return ResponseEntity.status(e.getCode()).body(new Response(e.getCode(), e.getMessage()));
} catch (Exception e) {
Expand All @@ -122,7 +118,7 @@ public ResponseEntity<?> deleteLabel(@PathVariable String id_product, @PathVaria
OPERATIONS PRODUCTS - REPORTS
*/
@PostMapping("/{id_product}/report")
public ResponseEntity<?> addReport(@PathVariable String id_product, @ModelAttribute ReportedProductCreateDTO reportedProductCreateDTO){
public ResponseEntity<?> addReport(@PathVariable String id_product, @RequestBody ReportedProductCreateDTO reportedProductCreateDTO){
try {
ReportedProductDTO reportedProductDTO = productService.reportProduct(id_product, reportedProductCreateDTO);
return ResponseEntity.status(HttpStatus.CREATED).body(reportedProductDTO);
Expand All @@ -132,6 +128,17 @@ public ResponseEntity<?> addReport(@PathVariable String id_product, @ModelAttrib
return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(new Response(HttpStatus.INTERNAL_SERVER_ERROR.value(), "Unexpected error occurred: " + e.getMessage()));
}
}
@PutMapping("/{id_product}/report")
public ResponseEntity<?> updateReport(@PathVariable String id_product, @RequestBody ReportedProductCreateDTO reportedProductCreateDTO){
try {
ReportedProductDTO reportedProductDTO = productService.updateProductReport(id_product, reportedProductCreateDTO);
return ResponseEntity.status(HttpStatus.OK).body(reportedProductDTO);
} catch (OperationException e) {
return ResponseEntity.status(e.getCode()).body(new Response(e.getCode(), e.getMessage()));
} catch (Exception e) {
return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(new Response(HttpStatus.INTERNAL_SERVER_ERROR.value(), "Unexpected error occurred: " + e.getMessage()));
}
}
@DeleteMapping("/{id_product}/report")
public ResponseEntity<?> deleteReport(@PathVariable String id_product){
try {
Expand Down Expand Up @@ -166,4 +173,71 @@ public ResponseEntity<?> getReports(){
return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(new Response(HttpStatus.INTERNAL_SERVER_ERROR.value(), e.getMessage()));
}
}
/*
RATING OPERATIONS
*/
@PostMapping("/rating/{id_product}")
public ResponseEntity<?> addRating(@PathVariable String id_product, @RequestBody RatingCreateDTO ratingCreateDTO){
try {
/*
System.out.println(ratingCreateDTO.userId());
System.out.println(ratingCreateDTO.productId());
System.out.println(ratingCreateDTO.rating());
*/
RatingDTO ratingDTO = productService.rateProduct(id_product, ratingCreateDTO);
return ResponseEntity.status(HttpStatus.CREATED).body(ratingDTO);
} catch (OperationException e) {
return ResponseEntity.status(e.getCode()).body(new Response(e.getCode(), e.getMessage()));
} catch (Exception e) {
return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(new Response(HttpStatus.INTERNAL_SERVER_ERROR.value(), "Unexpected error occurred: " + e.getMessage()));
}
}

@GetMapping("/rating")
public ResponseEntity<?> getRatings(){
try {
RatingDTO[] listRatingsDTO = productService.getAllRating();
return ResponseEntity.status(HttpStatus.OK).body(listRatingsDTO);
} catch (OperationException e) {
return ResponseEntity.status(e.getCode()).body(new Response(e.getCode(), e.getMessage()));
} catch (RuntimeException e) {
return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(new Response(HttpStatus.INTERNAL_SERVER_ERROR.value(), e.getMessage()));
}
}

@GetMapping("/rating/{id_product}/average")
public ResponseEntity<?> getRating(@PathVariable String id_product){
try {
float rating = productService.getAverageRating(id_product);
return ResponseEntity.status(HttpStatus.OK).body(new Response(HttpStatus.OK.value(), "Average rating: " + rating));
} catch (OperationException e) {
return ResponseEntity.status(e.getCode()).body(new Response(e.getCode(), e.getMessage()));
} catch (RuntimeException e) {
return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(new Response(HttpStatus.INTERNAL_SERVER_ERROR.value(), e.getMessage()));
}
}

@GetMapping("/rating/{id_product}")
public ResponseEntity<?> getRatingsByProduct(@PathVariable String id_product){
try {
RatingDTO[] listRatingsDTO = productService.getRatingByProduct(id_product);
return ResponseEntity.status(HttpStatus.OK).body(listRatingsDTO);
} catch (OperationException e) {
return ResponseEntity.status(e.getCode()).body(new Response(e.getCode(), e.getMessage()));
} catch (RuntimeException e) {
return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(new Response(HttpStatus.INTERNAL_SERVER_ERROR.value(), e.getMessage()));
}
}
@DeleteMapping("/rating/{id_rating}")
public ResponseEntity<?> deleteRating (@PathVariable String id_rating){
try {
productService.deleteRating(id_rating);
return ResponseEntity.status(HttpStatus.OK).body(new Response(HttpStatus.OK.value(), "Rating deleted"));
} catch (OperationException e) {
return ResponseEntity.status(e.getCode()).body(new Response(e.getCode(), e.getMessage()));
} catch (Exception e) {
return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(new Response(HttpStatus.INTERNAL_SERVER_ERROR.value(), "Unexpected error occurred: " + e.getMessage()));
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,22 @@

@Getter
@Setter

public class LabelDTO implements Serializable {
Integer id;
String label;
String [] products;

public LabelDTO(){

}

public LabelDTO(Label label){
this.id = label.getId();
this.label = label.getLabel();
this.products = label.getProducts().stream().map(Product::getName).toArray(String[]::new);
}



}
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
package co.allconnected.fussiontech.productsservice.dtos;

public record ProductCreateDTO (String idBusiness, String name, String description, int stock, float price, String status ){
import java.util.UUID;

public record ProductCreateDTO (UUID idBusiness, String name, String description, Integer stock, Double price, String status) {
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
package co.allconnected.fussiontech.productsservice.dtos;

public record RatingCreateDTO(Integer productId, String userId, Integer rating, String comment) {
}
Original file line number Diff line number Diff line change
@@ -1,15 +1,29 @@
package co.allconnected.fussiontech.productsservice.dtos;

import co.allconnected.fussiontech.productsservice.model.Rating;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;
import java.io.Serializable;
import java.time.Instant;

@Getter
@Setter
@NoArgsConstructor
public class RatingDTO implements Serializable {
private Integer idRating;
private Integer productId;
private Integer userId;
private String userId;
private Integer rating;
private String comment;
private String date;
private Instant date;

public RatingDTO(Rating rating){
this.idRating = rating.getId();
this.productId = rating.getIdProduct().getId();
this.userId = rating.getIdUser();
this.rating = rating.getRating();
this.comment = rating.getComment();
this.date = rating.getDate();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,10 @@
@NoArgsConstructor
@Table(name = "label", schema = "all_connected_products")
public class Label {
public Label (String label){
public Label(String label) {
this.label = label;
}

@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "id_label", nullable = false)
Expand All @@ -27,7 +28,8 @@ public Label (String label){

@ManyToMany
@JoinTable(name = "product_label",
schema = "all_connected_products", // Agrega el esquema explícitamente
joinColumns = @JoinColumn(name = "id_label"),
inverseJoinColumns = @JoinColumn(name = "id_announcement"))
private Set<Product> products = new LinkedHashSet<>();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,16 +16,14 @@
@NoArgsConstructor
@Table(name = "product", schema = "all_connected_products")
public class Product {
public Product (ProductCreateDTO dto){
this.idBusiness = UUID.fromString(dto.idBusiness());
public Product (ProductCreateDTO dto) {
this.idBusiness = dto.idBusiness();
this.name = dto.name();
this.description = dto.description();
this.stock = dto.stock();
this.price = (double) dto.price();
this.price = dto.price();
this.status = dto.status();
}


@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "id_product", nullable = false)
Expand Down Expand Up @@ -57,7 +55,4 @@ public Product (ProductCreateDTO dto){

@OneToMany(mappedBy = "idProduct")
private Set<Rating> ratings = new LinkedHashSet<>();

@OneToOne(mappedBy = "product")
private ReportedProduct reportedProduct;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package co.allconnected.fussiontech.productsservice.model;

import jakarta.persistence.*;
import lombok.Getter;
import lombok.Setter;

@Getter
@Setter
@Entity
@Table(name = "product_label", schema = "all_connected_products")
public class ProductLabel {
@EmbeddedId
private ProductLabelId id;

@MapsId("idAnnouncement")
@ManyToOne(fetch = FetchType.LAZY, optional = false)
@JoinColumn(name = "id_announcement", nullable = false)
private Product idAnnouncement;

@MapsId("idLabel")
@ManyToOne(fetch = FetchType.LAZY, optional = false)
@JoinColumn(name = "id_label", nullable = false)
private Label idLabel;

}
Loading

0 comments on commit f778cd2

Please sign in to comment.