Skip to content

Commit

Permalink
feat: adicionando ligação entre produto e empresa
Browse files Browse the repository at this point in the history
  • Loading branch information
oLeoBarreto committed May 2, 2024
1 parent 45359f1 commit 18dfe0b
Show file tree
Hide file tree
Showing 8 changed files with 48 additions and 22 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -23,18 +23,18 @@ public class ProductController implements ProductEndpoints {
private final ProductImageUseCase productImageService;

@GetMapping()
public ResponseEntity<Page<Product>> getProductsLists(Pageable pageable) {
return new ResponseEntity<>(productService.listAllProducts(pageable), HttpStatus.OK);
public ResponseEntity<Page<Product>> getProductsLists(Pageable pageable, @RequestParam String companyId) {
return new ResponseEntity<>(productService.listAllProducts(companyId, pageable), HttpStatus.OK);
}

@GetMapping("/findByCategory")
public ResponseEntity<Page<Product>> getProductByCategory(Pageable pageable, @RequestParam String category) {
return new ResponseEntity<>(productService.findProductByCategory(pageable, category), HttpStatus.FOUND);
public ResponseEntity<Page<Product>> getProductByCategory(Pageable pageable, @RequestParam String category, @RequestParam String companyId) {
return new ResponseEntity<>(productService.findProductByCategory(pageable, category, companyId), HttpStatus.FOUND);
}

@GetMapping("/findBySupplier")
public ResponseEntity<Page<Product>> getProductBySupplier(Pageable pageable, @RequestParam String supplier) {
return new ResponseEntity<>(productService.findProductBySupplier(pageable, supplier), HttpStatus.FOUND);
public ResponseEntity<Page<Product>> getProductBySupplier(Pageable pageable, @RequestParam String supplier, @RequestParam String companyId) {
return new ResponseEntity<>(productService.findProductBySupplier(pageable, supplier, companyId), HttpStatus.FOUND);
}

@GetMapping("/findById")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@
import org.springframework.web.multipart.MultipartFile;

public interface ProductEndpoints {
ResponseEntity<Page<Product>> getProductsLists(Pageable pageable);
ResponseEntity<Page<Product>> getProductByCategory(Pageable pageable, String category);
ResponseEntity<Page<Product>> getProductBySupplier(Pageable pageable, String supplier);
ResponseEntity<Page<Product>> getProductsLists(Pageable pageable, String companyId);
ResponseEntity<Page<Product>> getProductByCategory(Pageable pageable, String category, String companyId);
ResponseEntity<Page<Product>> getProductBySupplier(Pageable pageable, String supplier, String companyId);
ResponseEntity<Product> getProductById(String id);
ResponseEntity<byte[]> getProductImage(String productId);
ResponseEntity<Product> postNewProduct(ProductPostRequestBody product);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
package com.barreto.stockmanagement.domains;

import jakarta.persistence.CascadeType;
import jakarta.persistence.Entity;
import jakarta.persistence.JoinColumn;
import jakarta.persistence.ManyToOne;
import jakarta.validation.constraints.NotEmpty;
import jakarta.validation.constraints.NotNull;
import jakarta.validation.constraints.PositiveOrZero;
Expand Down Expand Up @@ -43,4 +46,9 @@ public class Product extends AbstractDomain{
private String image;

private Float stockQuantity = 0F;

@NotNull(message = "Company id could not be null")
@ManyToOne(cascade = CascadeType.ALL)
@JoinColumn(name = "company_id")
private Company company;
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.barreto.stockmanagement.infra.DTOs.Mappers;

import com.barreto.stockmanagement.domains.Company;
import com.barreto.stockmanagement.infra.DTOs.product.ProductPostRequestBody;
import com.barreto.stockmanagement.domains.Product;
import org.springframework.stereotype.Component;
Expand All @@ -18,4 +19,17 @@ public Product toProduct(ProductPostRequestBody productPostRequestBody) {

return product;
}

public Product toProduct(ProductPostRequestBody productPostRequestBody, Company company) {
Product product = new Product();

product.setName(productPostRequestBody.name());
product.setDescription(productPostRequestBody.description());
product.setUnitPrice(productPostRequestBody.unitPrice());
product.setSupplier(productPostRequestBody.supplier());
product.setCategory(productPostRequestBody.category());
product.setCompany(company);

return product;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,6 @@

import java.math.BigDecimal;

public record ProductPostRequestBody( String name, String description, BigDecimal unitPrice, String supplier, String category) {
public record ProductPostRequestBody(String name, String description, BigDecimal unitPrice, String supplier, String category, String companyId) {

}
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import java.util.List;

public interface ProductRepository extends JpaRepository<Product, String> {
Page<Product> findByCategory(Pageable pageable, String category);
Page<Product> findBySupplier(Pageable pageable, String supplier);
Page<Product> findByCategoryAndCompanyId(Pageable pageable, String category, String companyId);
Page<Product> findBySupplierAndCompanyId(Pageable pageable, String supplier, String companyId);
Page<Product> findByCompanyId(Pageable pageable, String companyId);
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import com.barreto.stockmanagement.infra.DTOs.product.ProductPutRequestBody;
import com.barreto.stockmanagement.infra.database.repository.ProductRepository;
import com.barreto.stockmanagement.infra.exceptions.BadRequestException;
import com.barreto.stockmanagement.useCases.company.CompanyUseCase;
import jakarta.transaction.Transactional;
import lombok.RequiredArgsConstructor;
import org.springframework.data.domain.Page;
Expand All @@ -17,17 +18,18 @@
public class ProductService implements ProductUseCase {

private final ProductRepository repository;
private final CompanyUseCase companyService;

public Page<Product> listAllProducts(Pageable pageable) {
return repository.findAll(pageable);
public Page<Product> listAllProducts(String companyId, Pageable pageable) {
return repository.findByCompanyId(pageable, companyId);
}

public Page<Product> findProductByCategory(Pageable pageable, String category) {
return repository.findByCategory(pageable, category);
public Page<Product> findProductByCategory(Pageable pageable, String category, String companyId) {
return repository.findByCategoryAndCompanyId(pageable, category, companyId);
}

public Page<Product> findProductBySupplier(Pageable pageable, String supplier) {
return repository.findBySupplier(pageable, supplier);
public Page<Product> findProductBySupplier(Pageable pageable, String supplier, String companyId) {
return repository.findBySupplierAndCompanyId(pageable, supplier, companyId);
}

public Product findProductById(String id) {
Expand All @@ -36,7 +38,8 @@ public Product findProductById(String id) {

@Transactional
public Product createNewProduct(ProductPostRequestBody product) {
return repository.save(ProductMapper.INSTANCE.toProduct(product));
var company = companyService.findCompanyById(product.companyId());
return repository.save(ProductMapper.INSTANCE.toProduct(product, company));
}

public Product updateProduct(ProductPutRequestBody product) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@
import org.springframework.data.domain.Pageable;

public interface ProductUseCase {
Page<Product> listAllProducts(Pageable pageable);
Page<Product> findProductByCategory(Pageable pageable, String category);
Page<Product> findProductBySupplier(Pageable pageable, String supplier);
Page<Product> listAllProducts(String companyId, Pageable pageable);
Page<Product> findProductByCategory(Pageable pageable, String category, String companyId);
Page<Product> findProductBySupplier(Pageable pageable, String supplier, String companyId);
Product findProductById(String id);
Product createNewProduct(ProductPostRequestBody product);
Product updateProduct(ProductPutRequestBody product);
Expand Down

0 comments on commit 18dfe0b

Please sign in to comment.