From 6268b7781a0db8a55ca1f83791ff124c167489ed Mon Sep 17 00:00:00 2001 From: Jonas Souza Date: Fri, 19 Jul 2024 14:56:10 -0300 Subject: [PATCH] refactor: switch the product classes to clean architecture --- .../repositories/ProductRepository.java | 10 +-- .../product/CreateProductUseCase.java | 16 ++--- .../product/DeleteProductUseCase.java | 12 ++-- .../usecases/product/GetProductUseCase.java | 16 ++--- .../ListProductsByCategoryUseCase.java | 16 ++--- .../product/UpdateProductUseCase.java | 16 ++--- .../configuration/ProductConfiguration.java | 45 +++++++++++++ .../gateways/ProductGateway.java | 35 +++++----- .../controllers/ProductController.java | 23 +++++-- .../mapper/impl/CategoryDTOMapper.java | 4 +- .../mapper/impl/ProductDTOMapper.java | 64 +++++++------------ ...ctMapper.java => ProductEntityMapper.java} | 20 +++++- 12 files changed, 158 insertions(+), 119 deletions(-) create mode 100644 src/main/java/br/com/fiap/grupo30/fastfood/infrastructure/configuration/ProductConfiguration.java rename src/main/java/br/com/fiap/grupo30/fastfood/presentation/presenters/mapper/impl/{ProductMapper.java => ProductEntityMapper.java} (59%) diff --git a/src/main/java/br/com/fiap/grupo30/fastfood/domain/repositories/ProductRepository.java b/src/main/java/br/com/fiap/grupo30/fastfood/domain/repositories/ProductRepository.java index 89fd531..b6c2ce6 100644 --- a/src/main/java/br/com/fiap/grupo30/fastfood/domain/repositories/ProductRepository.java +++ b/src/main/java/br/com/fiap/grupo30/fastfood/domain/repositories/ProductRepository.java @@ -1,17 +1,17 @@ package br.com.fiap.grupo30.fastfood.domain.repositories; -import br.com.fiap.grupo30.fastfood.presentation.presenters.dto.ProductDTO; +import br.com.fiap.grupo30.fastfood.domain.entities.Product; import java.util.List; public interface ProductRepository { - List findProductsByCategoryId(Long categoryId); + List findProductsByCategoryId(Long categoryId); - ProductDTO findById(Long id); + Product findById(Long id); - ProductDTO insert(ProductDTO dto); + Product insert(Product product); - ProductDTO update(Long id, ProductDTO dto); + Product update(Long id, Product product); void delete(Long id); } diff --git a/src/main/java/br/com/fiap/grupo30/fastfood/domain/usecases/product/CreateProductUseCase.java b/src/main/java/br/com/fiap/grupo30/fastfood/domain/usecases/product/CreateProductUseCase.java index 0c29238..07a4a38 100644 --- a/src/main/java/br/com/fiap/grupo30/fastfood/domain/usecases/product/CreateProductUseCase.java +++ b/src/main/java/br/com/fiap/grupo30/fastfood/domain/usecases/product/CreateProductUseCase.java @@ -1,19 +1,17 @@ package br.com.fiap.grupo30.fastfood.domain.usecases.product; -import br.com.fiap.grupo30.fastfood.domain.repositories.ProductRepository; -import br.com.fiap.grupo30.fastfood.presentation.presenters.dto.ProductDTO; -import org.springframework.stereotype.Component; +import br.com.fiap.grupo30.fastfood.domain.entities.Product; +import br.com.fiap.grupo30.fastfood.infrastructure.gateways.ProductGateway; -@Component public class CreateProductUseCase { - private final ProductRepository productRepository; + private final ProductGateway productGateway; - public CreateProductUseCase(ProductRepository productRepository) { - this.productRepository = productRepository; + public CreateProductUseCase(ProductGateway productGateway) { + this.productGateway = productGateway; } - public ProductDTO execute(ProductDTO product) { - return productRepository.insert(product); + public Product execute(Product product) { + return productGateway.insert(product); } } diff --git a/src/main/java/br/com/fiap/grupo30/fastfood/domain/usecases/product/DeleteProductUseCase.java b/src/main/java/br/com/fiap/grupo30/fastfood/domain/usecases/product/DeleteProductUseCase.java index a2b5370..9bab566 100644 --- a/src/main/java/br/com/fiap/grupo30/fastfood/domain/usecases/product/DeleteProductUseCase.java +++ b/src/main/java/br/com/fiap/grupo30/fastfood/domain/usecases/product/DeleteProductUseCase.java @@ -1,18 +1,16 @@ package br.com.fiap.grupo30.fastfood.domain.usecases.product; -import br.com.fiap.grupo30.fastfood.domain.repositories.ProductRepository; -import org.springframework.stereotype.Component; +import br.com.fiap.grupo30.fastfood.infrastructure.gateways.ProductGateway; -@Component public class DeleteProductUseCase { - private final ProductRepository productRepository; + private final ProductGateway productGateway; - public DeleteProductUseCase(ProductRepository productRepository) { - this.productRepository = productRepository; + public DeleteProductUseCase(ProductGateway productGateway) { + this.productGateway = productGateway; } public void execute(Long id) { - productRepository.delete(id); + productGateway.delete(id); } } diff --git a/src/main/java/br/com/fiap/grupo30/fastfood/domain/usecases/product/GetProductUseCase.java b/src/main/java/br/com/fiap/grupo30/fastfood/domain/usecases/product/GetProductUseCase.java index b40f3bd..53adb03 100644 --- a/src/main/java/br/com/fiap/grupo30/fastfood/domain/usecases/product/GetProductUseCase.java +++ b/src/main/java/br/com/fiap/grupo30/fastfood/domain/usecases/product/GetProductUseCase.java @@ -1,19 +1,17 @@ package br.com.fiap.grupo30.fastfood.domain.usecases.product; -import br.com.fiap.grupo30.fastfood.domain.repositories.ProductRepository; -import br.com.fiap.grupo30.fastfood.presentation.presenters.dto.ProductDTO; -import org.springframework.stereotype.Component; +import br.com.fiap.grupo30.fastfood.domain.entities.Product; +import br.com.fiap.grupo30.fastfood.infrastructure.gateways.ProductGateway; -@Component public class GetProductUseCase { - private final ProductRepository productRepository; + private final ProductGateway productGateway; - public GetProductUseCase(ProductRepository productRepository) { - this.productRepository = productRepository; + public GetProductUseCase(ProductGateway productGateway) { + this.productGateway = productGateway; } - public ProductDTO execute(Long id) { - return productRepository.findById(id); + public Product execute(Long id) { + return productGateway.findById(id); } } diff --git a/src/main/java/br/com/fiap/grupo30/fastfood/domain/usecases/product/ListProductsByCategoryUseCase.java b/src/main/java/br/com/fiap/grupo30/fastfood/domain/usecases/product/ListProductsByCategoryUseCase.java index ae42a7c..ffd6123 100644 --- a/src/main/java/br/com/fiap/grupo30/fastfood/domain/usecases/product/ListProductsByCategoryUseCase.java +++ b/src/main/java/br/com/fiap/grupo30/fastfood/domain/usecases/product/ListProductsByCategoryUseCase.java @@ -1,20 +1,18 @@ package br.com.fiap.grupo30.fastfood.domain.usecases.product; -import br.com.fiap.grupo30.fastfood.domain.repositories.ProductRepository; -import br.com.fiap.grupo30.fastfood.presentation.presenters.dto.ProductDTO; +import br.com.fiap.grupo30.fastfood.domain.entities.Product; +import br.com.fiap.grupo30.fastfood.infrastructure.gateways.ProductGateway; import java.util.List; -import org.springframework.stereotype.Component; -@Component public class ListProductsByCategoryUseCase { - private final ProductRepository productRepository; + private final ProductGateway productGateway; - public ListProductsByCategoryUseCase(ProductRepository productRepository) { - this.productRepository = productRepository; + public ListProductsByCategoryUseCase(ProductGateway productGateway) { + this.productGateway = productGateway; } - public List execute(Long categoryId) { - return productRepository.findProductsByCategoryId(categoryId); + public List execute(Long categoryId) { + return productGateway.findProductsByCategoryId(categoryId); } } diff --git a/src/main/java/br/com/fiap/grupo30/fastfood/domain/usecases/product/UpdateProductUseCase.java b/src/main/java/br/com/fiap/grupo30/fastfood/domain/usecases/product/UpdateProductUseCase.java index 725c1ca..937fe80 100644 --- a/src/main/java/br/com/fiap/grupo30/fastfood/domain/usecases/product/UpdateProductUseCase.java +++ b/src/main/java/br/com/fiap/grupo30/fastfood/domain/usecases/product/UpdateProductUseCase.java @@ -1,19 +1,17 @@ package br.com.fiap.grupo30.fastfood.domain.usecases.product; -import br.com.fiap.grupo30.fastfood.domain.repositories.ProductRepository; -import br.com.fiap.grupo30.fastfood.presentation.presenters.dto.ProductDTO; -import org.springframework.stereotype.Component; +import br.com.fiap.grupo30.fastfood.domain.entities.Product; +import br.com.fiap.grupo30.fastfood.infrastructure.gateways.ProductGateway; -@Component public class UpdateProductUseCase { - private final ProductRepository productRepository; + private final ProductGateway productGateway; - public UpdateProductUseCase(ProductRepository productRepository) { - this.productRepository = productRepository; + public UpdateProductUseCase(ProductGateway productGateway) { + this.productGateway = productGateway; } - public ProductDTO execute(Long id, ProductDTO product) { - return productRepository.update(id, product); + public Product execute(Long id, Product product) { + return productGateway.update(id, product); } } diff --git a/src/main/java/br/com/fiap/grupo30/fastfood/infrastructure/configuration/ProductConfiguration.java b/src/main/java/br/com/fiap/grupo30/fastfood/infrastructure/configuration/ProductConfiguration.java new file mode 100644 index 0000000..56c5900 --- /dev/null +++ b/src/main/java/br/com/fiap/grupo30/fastfood/infrastructure/configuration/ProductConfiguration.java @@ -0,0 +1,45 @@ +package br.com.fiap.grupo30.fastfood.infrastructure.configuration; + +import br.com.fiap.grupo30.fastfood.domain.repositories.ProductRepository; +import br.com.fiap.grupo30.fastfood.domain.usecases.product.*; +import br.com.fiap.grupo30.fastfood.infrastructure.gateways.ProductGateway; +import br.com.fiap.grupo30.fastfood.infrastructure.persistence.repositories.JpaProductRepository; +import br.com.fiap.grupo30.fastfood.presentation.presenters.mapper.impl.ProductEntityMapper; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; + +@Configuration +public class ProductConfiguration { + + @Bean + public ProductRepository productRepository( + JpaProductRepository jpaProductRepository, ProductEntityMapper productEntityMapper) { + return new ProductGateway(jpaProductRepository, productEntityMapper); + } + + @Bean + public ListProductsByCategoryUseCase listProductsByCategoryUseCase( + ProductGateway productGateway) { + return new ListProductsByCategoryUseCase(productGateway); + } + + @Bean + public GetProductUseCase getProductUseCase(ProductGateway productGateway) { + return new GetProductUseCase(productGateway); + } + + @Bean + public CreateProductUseCase createProductUseCase(ProductGateway productGateway) { + return new CreateProductUseCase(productGateway); + } + + @Bean + public UpdateProductUseCase updateProductUseCase(ProductGateway productGateway) { + return new UpdateProductUseCase(productGateway); + } + + @Bean + public DeleteProductUseCase deleteProductUseCase(ProductGateway productGateway) { + return new DeleteProductUseCase(productGateway); + } +} diff --git a/src/main/java/br/com/fiap/grupo30/fastfood/infrastructure/gateways/ProductGateway.java b/src/main/java/br/com/fiap/grupo30/fastfood/infrastructure/gateways/ProductGateway.java index 0d51cdf..c638ffa 100644 --- a/src/main/java/br/com/fiap/grupo30/fastfood/infrastructure/gateways/ProductGateway.java +++ b/src/main/java/br/com/fiap/grupo30/fastfood/infrastructure/gateways/ProductGateway.java @@ -1,13 +1,12 @@ package br.com.fiap.grupo30.fastfood.infrastructure.gateways; +import br.com.fiap.grupo30.fastfood.domain.entities.Product; import br.com.fiap.grupo30.fastfood.domain.repositories.ProductRepository; import br.com.fiap.grupo30.fastfood.infrastructure.persistence.entities.ProductEntity; import br.com.fiap.grupo30.fastfood.infrastructure.persistence.repositories.JpaProductRepository; -import br.com.fiap.grupo30.fastfood.presentation.presenters.dto.ProductDTO; import br.com.fiap.grupo30.fastfood.presentation.presenters.exceptions.DatabaseException; import br.com.fiap.grupo30.fastfood.presentation.presenters.exceptions.ResourceNotFoundException; -import br.com.fiap.grupo30.fastfood.presentation.presenters.mapper.impl.ProductDTOMapper; -import br.com.fiap.grupo30.fastfood.presentation.presenters.mapper.impl.ProductMapper; +import br.com.fiap.grupo30.fastfood.presentation.presenters.mapper.impl.ProductEntityMapper; import jakarta.persistence.EntityNotFoundException; import java.util.List; import java.util.Optional; @@ -21,52 +20,48 @@ public class ProductGateway implements ProductRepository { private final JpaProductRepository jpaProductRepository; - private final ProductMapper productMapper; - private final ProductDTOMapper productDTOMapper; + private final ProductEntityMapper productEntityMapper; @Autowired public ProductGateway( - JpaProductRepository jpaProductRepository, - ProductMapper productMapper, - ProductDTOMapper productDTOMapper) { + JpaProductRepository jpaProductRepository, ProductEntityMapper productEntityMapper) { this.jpaProductRepository = jpaProductRepository; - this.productMapper = productMapper; - this.productDTOMapper = productDTOMapper; + this.productEntityMapper = productEntityMapper; } @Override @Transactional(readOnly = true) - public List findProductsByCategoryId(Long categoryId) { + public List findProductsByCategoryId(Long categoryId) { Long category = categoryId == 0 ? null : categoryId; return jpaProductRepository.findProductsByCategoryId(category).stream() - .map(entity -> new ProductDTO(productMapper.mapFrom(entity))) + .map(this.productEntityMapper::mapFrom) .toList(); } @Override @Transactional(readOnly = true) - public ProductDTO findById(Long id) { + public Product findById(Long id) { Optional obj = jpaProductRepository.findById(id); ProductEntity entity = obj.orElseThrow(() -> new ResourceNotFoundException("Entity not found")); - return new ProductDTO(productMapper.mapFrom(entity)); + return this.productEntityMapper.mapFrom(entity); } @Override @Transactional - public ProductDTO insert(ProductDTO dto) { - ProductEntity entity = jpaProductRepository.save(productDTOMapper.mapTo(dto)); - return new ProductDTO(productMapper.mapFrom(entity)); + public Product insert(Product product) { + ProductEntity entity = jpaProductRepository.save(productEntityMapper.mapTo(product)); + return this.productEntityMapper.mapFrom(entity); } @Override @Transactional - public ProductDTO update(Long id, ProductDTO dto) { + public Product update(Long id, Product product) { try { ProductEntity entity = jpaProductRepository.getReferenceById(id); - productDTOMapper.updateEntityFromDTO(entity, dto); + productEntityMapper.updateEntityFromProduct(entity, product); entity = jpaProductRepository.save(entity); - return new ProductDTO(productMapper.mapFrom(entity)); + return this.productEntityMapper.mapFrom(entity); } catch (EntityNotFoundException e) { throw new ResourceNotFoundException("Id not found " + id, e); } diff --git a/src/main/java/br/com/fiap/grupo30/fastfood/presentation/controllers/ProductController.java b/src/main/java/br/com/fiap/grupo30/fastfood/presentation/controllers/ProductController.java index 0d44499..ee3c54f 100644 --- a/src/main/java/br/com/fiap/grupo30/fastfood/presentation/controllers/ProductController.java +++ b/src/main/java/br/com/fiap/grupo30/fastfood/presentation/controllers/ProductController.java @@ -1,7 +1,9 @@ package br.com.fiap.grupo30.fastfood.presentation.controllers; +import br.com.fiap.grupo30.fastfood.domain.entities.Product; import br.com.fiap.grupo30.fastfood.domain.usecases.product.*; import br.com.fiap.grupo30.fastfood.presentation.presenters.dto.ProductDTO; +import br.com.fiap.grupo30.fastfood.presentation.presenters.mapper.impl.ProductDTOMapper; import io.swagger.v3.oas.annotations.Operation; import io.swagger.v3.oas.annotations.tags.Tag; import jakarta.validation.Valid; @@ -25,19 +27,23 @@ public class ProductController { private final UpdateProductUseCase updateProductUseCase; private final DeleteProductUseCase deleteProductUseCase; + private final ProductDTOMapper productDTOMapper; + @Autowired public ProductController( ListProductsByCategoryUseCase listProductsByCategoryUseCase, GetProductUseCase getProductUseCase, CreateProductUseCase createProductUseCase, UpdateProductUseCase updateProductUseCase, - DeleteProductUseCase deleteProductUseCase) { + DeleteProductUseCase deleteProductUseCase, + ProductDTOMapper productDTOMapper) { this.listProductsByCategoryUseCase = listProductsByCategoryUseCase; this.getProductUseCase = getProductUseCase; this.createProductUseCase = createProductUseCase; this.updateProductUseCase = updateProductUseCase; this.deleteProductUseCase = deleteProductUseCase; + this.productDTOMapper = productDTOMapper; } @GetMapping @@ -48,7 +54,10 @@ public ProductController( + "via RequestParam. i.e., ?categoryId=1") public ResponseEntity> findProductsByCategoryId( @RequestParam(value = "categoryId", defaultValue = "0") Long categoryId) { - List list = this.listProductsByCategoryUseCase.execute(categoryId); + List list = + this.listProductsByCategoryUseCase.execute(categoryId).stream() + .map(this.productDTOMapper::mapTo) + .toList(); return ResponseEntity.ok().body(list); } @@ -57,7 +66,7 @@ public ResponseEntity> findProductsByCategoryId( summary = "Get a product by ID", description = "Retrieve a specific product based on its ID") public ResponseEntity findProductById(@PathVariable Long id) { - ProductDTO dto = this.getProductUseCase.execute(id); + ProductDTO dto = this.productDTOMapper.mapTo(this.getProductUseCase.execute(id)); return ResponseEntity.ok().body(dto); } @@ -66,7 +75,9 @@ public ResponseEntity findProductById(@PathVariable Long id) { summary = "Create a new product", description = "Create a new product and return the created product's data") public ResponseEntity createProduct(@RequestBody @Valid ProductDTO dto) { - ProductDTO dtoCreated = this.createProductUseCase.execute(dto); + Product domainObj = this.productDTOMapper.mapFrom(dto); + ProductDTO dtoCreated = + this.productDTOMapper.mapTo(this.createProductUseCase.execute(domainObj)); URI uri = ServletUriComponentsBuilder.fromCurrentRequest() .path(PATH_VARIABLE_ID) @@ -81,7 +92,9 @@ public ResponseEntity createProduct(@RequestBody @Valid ProductDTO d description = "Update the data of an existing product based on its ID") public ResponseEntity updateProduct( @PathVariable Long id, @RequestBody @Valid ProductDTO dto) { - ProductDTO dtoUpdated = this.updateProductUseCase.execute(id, dto); + Product domainObj = this.productDTOMapper.mapFrom(dto); + ProductDTO dtoUpdated = + this.productDTOMapper.mapTo(this.updateProductUseCase.execute(id, domainObj)); return ResponseEntity.ok().body(dtoUpdated); } diff --git a/src/main/java/br/com/fiap/grupo30/fastfood/presentation/presenters/mapper/impl/CategoryDTOMapper.java b/src/main/java/br/com/fiap/grupo30/fastfood/presentation/presenters/mapper/impl/CategoryDTOMapper.java index 1811ff5..10cc5b0 100644 --- a/src/main/java/br/com/fiap/grupo30/fastfood/presentation/presenters/mapper/impl/CategoryDTOMapper.java +++ b/src/main/java/br/com/fiap/grupo30/fastfood/presentation/presenters/mapper/impl/CategoryDTOMapper.java @@ -2,11 +2,11 @@ import br.com.fiap.grupo30.fastfood.domain.entities.Category; import br.com.fiap.grupo30.fastfood.presentation.presenters.dto.CategoryDTO; -import br.com.fiap.grupo30.fastfood.presentation.presenters.mapper.Mapper; +import br.com.fiap.grupo30.fastfood.presentation.presenters.mapper.BiDirectionalMapper; import org.springframework.stereotype.Component; @Component -public class CategoryDTOMapper implements Mapper { +public class CategoryDTOMapper implements BiDirectionalMapper { @Override public CategoryDTO mapTo(Category category) { diff --git a/src/main/java/br/com/fiap/grupo30/fastfood/presentation/presenters/mapper/impl/ProductDTOMapper.java b/src/main/java/br/com/fiap/grupo30/fastfood/presentation/presenters/mapper/impl/ProductDTOMapper.java index 8c93075..5ea93ed 100644 --- a/src/main/java/br/com/fiap/grupo30/fastfood/presentation/presenters/mapper/impl/ProductDTOMapper.java +++ b/src/main/java/br/com/fiap/grupo30/fastfood/presentation/presenters/mapper/impl/ProductDTOMapper.java @@ -1,61 +1,41 @@ package br.com.fiap.grupo30.fastfood.presentation.presenters.mapper.impl; -import br.com.fiap.grupo30.fastfood.domain.entities.Category; -import br.com.fiap.grupo30.fastfood.infrastructure.persistence.entities.CategoryEntity; -import br.com.fiap.grupo30.fastfood.infrastructure.persistence.entities.ProductEntity; -import br.com.fiap.grupo30.fastfood.infrastructure.persistence.repositories.JpaCategoryRepository; -import br.com.fiap.grupo30.fastfood.presentation.presenters.dto.CategoryDTO; +import br.com.fiap.grupo30.fastfood.domain.entities.Product; import br.com.fiap.grupo30.fastfood.presentation.presenters.dto.ProductDTO; import br.com.fiap.grupo30.fastfood.presentation.presenters.mapper.BiDirectionalMapper; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Component; @Component -public final class ProductDTOMapper implements BiDirectionalMapper { - - private final JpaCategoryRepository jpaCategoryRepository; - private final CategoryEntityMapper categoryEntityMapper; +public final class ProductDTOMapper implements BiDirectionalMapper { + private final CategoryDTOMapper categoryDTOMapper; @Autowired - public ProductDTOMapper( - JpaCategoryRepository jpaCategoryRepository, - CategoryEntityMapper categoryEntityMapper) { - this.jpaCategoryRepository = jpaCategoryRepository; - this.categoryEntityMapper = categoryEntityMapper; + public ProductDTOMapper(CategoryDTOMapper categoryDTOMapper) { + this.categoryDTOMapper = categoryDTOMapper; } @Override - public ProductEntity mapTo(ProductDTO dto) { - ProductEntity entity = new ProductEntity(); - entity.setId(dto.getId()); - entity.setName(dto.getName()); - entity.setDescription(dto.getDescription()); - entity.setPrice(dto.getPrice()); - entity.setImgUrl(dto.getImgUrl()); - CategoryEntity category = jpaCategoryRepository.getReferenceById(dto.getCategory().getId()); - entity.setCategory(category); - return entity; - } - - @Override - public ProductDTO mapFrom(ProductEntity entity) { + public ProductDTO mapTo(Product product) { ProductDTO dto = new ProductDTO(); - dto.setId(entity.getId()); - dto.setName(entity.getName()); - dto.setDescription(entity.getDescription()); - dto.setImgUrl(entity.getImgUrl()); - dto.setPrice(entity.getPrice()); - Category category = categoryEntityMapper.mapFrom(entity.getCategory()); - dto.setCategory(new CategoryDTO(category)); + dto.setId(product.getId()); + dto.setName(product.getName()); + dto.setDescription(product.getDescription()); + dto.setPrice(product.getPrice()); + dto.setImgUrl(product.getImgUrl()); + dto.setCategory(categoryDTOMapper.mapTo(product.getCategory())); return dto; } - public void updateEntityFromDTO(ProductEntity entity, ProductDTO dto) { - entity.setName(dto.getName()); - entity.setDescription(dto.getDescription()); - entity.setImgUrl(dto.getImgUrl()); - entity.setPrice(dto.getPrice()); - CategoryEntity category = jpaCategoryRepository.getReferenceById(dto.getCategory().getId()); - entity.setCategory(category); + @Override + public Product mapFrom(ProductDTO dto) { + Product product = new Product(); + product.setId(dto.getId()); + product.setName(dto.getName()); + product.setDescription(dto.getDescription()); + product.setImgUrl(dto.getImgUrl()); + product.setPrice(dto.getPrice()); + product.setCategory(categoryDTOMapper.mapFrom(dto.getCategory())); + return product; } } diff --git a/src/main/java/br/com/fiap/grupo30/fastfood/presentation/presenters/mapper/impl/ProductMapper.java b/src/main/java/br/com/fiap/grupo30/fastfood/presentation/presenters/mapper/impl/ProductEntityMapper.java similarity index 59% rename from src/main/java/br/com/fiap/grupo30/fastfood/presentation/presenters/mapper/impl/ProductMapper.java rename to src/main/java/br/com/fiap/grupo30/fastfood/presentation/presenters/mapper/impl/ProductEntityMapper.java index cbf3e99..b497044 100644 --- a/src/main/java/br/com/fiap/grupo30/fastfood/presentation/presenters/mapper/impl/ProductMapper.java +++ b/src/main/java/br/com/fiap/grupo30/fastfood/presentation/presenters/mapper/impl/ProductEntityMapper.java @@ -1,19 +1,25 @@ package br.com.fiap.grupo30.fastfood.presentation.presenters.mapper.impl; import br.com.fiap.grupo30.fastfood.domain.entities.Product; +import br.com.fiap.grupo30.fastfood.infrastructure.persistence.entities.CategoryEntity; import br.com.fiap.grupo30.fastfood.infrastructure.persistence.entities.ProductEntity; +import br.com.fiap.grupo30.fastfood.infrastructure.persistence.repositories.JpaCategoryRepository; import br.com.fiap.grupo30.fastfood.presentation.presenters.mapper.BiDirectionalMapper; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Component; @Component -public final class ProductMapper implements BiDirectionalMapper { +public final class ProductEntityMapper implements BiDirectionalMapper { private final CategoryEntityMapper categoryEntityMapper; + private final JpaCategoryRepository jpaCategoryRepository; @Autowired - public ProductMapper(CategoryEntityMapper categoryEntityMapper) { + public ProductEntityMapper( + CategoryEntityMapper categoryEntityMapper, + JpaCategoryRepository jpaCategoryRepository) { this.categoryEntityMapper = categoryEntityMapper; + this.jpaCategoryRepository = jpaCategoryRepository; } @Override @@ -39,4 +45,14 @@ public Product mapFrom(ProductEntity entity) { product.setCategory(categoryEntityMapper.mapFrom(entity.getCategory())); return product; } + + public void updateEntityFromProduct(ProductEntity entity, Product product) { + entity.setName(product.getName()); + entity.setDescription(product.getDescription()); + entity.setImgUrl(product.getImgUrl()); + entity.setPrice(product.getPrice()); + CategoryEntity category = + jpaCategoryRepository.getReferenceById(product.getCategory().getId()); + entity.setCategory(category); + } }