diff --git a/src/main/java/br/com/fiap/grupo30/fastfood/domain/repositories/CategoryRepository.java b/src/main/java/br/com/fiap/grupo30/fastfood/domain/repositories/CategoryRepository.java index 2d12636..55298e2 100644 --- a/src/main/java/br/com/fiap/grupo30/fastfood/domain/repositories/CategoryRepository.java +++ b/src/main/java/br/com/fiap/grupo30/fastfood/domain/repositories/CategoryRepository.java @@ -1,8 +1,8 @@ package br.com.fiap.grupo30.fastfood.domain.repositories; -import br.com.fiap.grupo30.fastfood.presentation.presenters.dto.CategoryDTO; +import br.com.fiap.grupo30.fastfood.domain.entities.Category; import java.util.List; public interface CategoryRepository { - List findAll(); + List findAll(); } diff --git a/src/main/java/br/com/fiap/grupo30/fastfood/domain/usecases/category/ListAllCategoriesInMenuUseCase.java b/src/main/java/br/com/fiap/grupo30/fastfood/domain/usecases/category/ListAllCategoriesInMenuUseCase.java new file mode 100644 index 0000000..b09da43 --- /dev/null +++ b/src/main/java/br/com/fiap/grupo30/fastfood/domain/usecases/category/ListAllCategoriesInMenuUseCase.java @@ -0,0 +1,18 @@ +package br.com.fiap.grupo30.fastfood.domain.usecases.category; + +import br.com.fiap.grupo30.fastfood.domain.entities.Category; +import br.com.fiap.grupo30.fastfood.infrastructure.gateways.CategoryGateway; +import java.util.List; + +public class ListAllCategoriesInMenuUseCase { + + private final CategoryGateway categoryGateway; + + public ListAllCategoriesInMenuUseCase(CategoryGateway categoryGateway) { + this.categoryGateway = categoryGateway; + } + + public List execute() { + return categoryGateway.findAll(); + } +} diff --git a/src/main/java/br/com/fiap/grupo30/fastfood/domain/usecases/product/ListAllCategoriesInMenuUseCase.java b/src/main/java/br/com/fiap/grupo30/fastfood/domain/usecases/product/ListAllCategoriesInMenuUseCase.java deleted file mode 100644 index 72e338e..0000000 --- a/src/main/java/br/com/fiap/grupo30/fastfood/domain/usecases/product/ListAllCategoriesInMenuUseCase.java +++ /dev/null @@ -1,20 +0,0 @@ -package br.com.fiap.grupo30.fastfood.domain.usecases.product; - -import br.com.fiap.grupo30.fastfood.domain.repositories.CategoryRepository; -import br.com.fiap.grupo30.fastfood.presentation.presenters.dto.CategoryDTO; -import java.util.List; -import org.springframework.stereotype.Component; - -@Component -public class ListAllCategoriesInMenuUseCase { - - private final CategoryRepository categoryRepository; - - public ListAllCategoriesInMenuUseCase(CategoryRepository categoryRepository) { - this.categoryRepository = categoryRepository; - } - - public List execute() { - return categoryRepository.findAll(); - } -} diff --git a/src/main/java/br/com/fiap/grupo30/fastfood/infrastructure/configuration/CategoryConfiguration.java b/src/main/java/br/com/fiap/grupo30/fastfood/infrastructure/configuration/CategoryConfiguration.java new file mode 100644 index 0000000..b668c1e --- /dev/null +++ b/src/main/java/br/com/fiap/grupo30/fastfood/infrastructure/configuration/CategoryConfiguration.java @@ -0,0 +1,23 @@ +package br.com.fiap.grupo30.fastfood.infrastructure.configuration; + +import br.com.fiap.grupo30.fastfood.domain.repositories.CategoryRepository; +import br.com.fiap.grupo30.fastfood.domain.usecases.category.ListAllCategoriesInMenuUseCase; +import br.com.fiap.grupo30.fastfood.infrastructure.gateways.CategoryGateway; +import br.com.fiap.grupo30.fastfood.infrastructure.persistence.repositories.JpaCategoryRepository; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; + +@Configuration +public class CategoryConfiguration { + + @Bean + public CategoryRepository categoryRepository(JpaCategoryRepository jpaCategoryRepository) { + return new CategoryGateway(jpaCategoryRepository); + } + + @Bean + public ListAllCategoriesInMenuUseCase listAllCategoriesInMenuUseCase( + CategoryGateway categoryGateway) { + return new ListAllCategoriesInMenuUseCase(categoryGateway); + } +} diff --git a/src/main/java/br/com/fiap/grupo30/fastfood/infrastructure/gateways/CategoryGateway.java b/src/main/java/br/com/fiap/grupo30/fastfood/infrastructure/gateways/CategoryGateway.java index dd18a95..22b2f9e 100644 --- a/src/main/java/br/com/fiap/grupo30/fastfood/infrastructure/gateways/CategoryGateway.java +++ b/src/main/java/br/com/fiap/grupo30/fastfood/infrastructure/gateways/CategoryGateway.java @@ -1,9 +1,8 @@ package br.com.fiap.grupo30.fastfood.infrastructure.gateways; +import br.com.fiap.grupo30.fastfood.domain.entities.Category; import br.com.fiap.grupo30.fastfood.domain.repositories.CategoryRepository; 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.presentation.presenters.mapper.impl.CategoryMapper; import java.util.List; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; @@ -13,20 +12,17 @@ public class CategoryGateway implements CategoryRepository { public final JpaCategoryRepository jpaCategoryRepository; - private final CategoryMapper categoryMapper; @Autowired - public CategoryGateway( - JpaCategoryRepository jpaCategoryRepository, CategoryMapper categoryMapper) { + public CategoryGateway(JpaCategoryRepository jpaCategoryRepository) { this.jpaCategoryRepository = jpaCategoryRepository; - this.categoryMapper = categoryMapper; } @Override @Transactional(readOnly = true) - public List findAll() { + public List findAll() { return jpaCategoryRepository.findAll().stream() - .map(entity -> new CategoryDTO(categoryMapper.mapFrom(entity))) + .map(entity -> new Category(entity.getId(), entity.getName())) .toList(); } } diff --git a/src/main/java/br/com/fiap/grupo30/fastfood/presentation/controllers/CategoryController.java b/src/main/java/br/com/fiap/grupo30/fastfood/presentation/controllers/CategoryController.java index 981e90a..dd0681a 100644 --- a/src/main/java/br/com/fiap/grupo30/fastfood/presentation/controllers/CategoryController.java +++ b/src/main/java/br/com/fiap/grupo30/fastfood/presentation/controllers/CategoryController.java @@ -1,7 +1,8 @@ package br.com.fiap.grupo30.fastfood.presentation.controllers; -import br.com.fiap.grupo30.fastfood.domain.usecases.product.ListAllCategoriesInMenuUseCase; +import br.com.fiap.grupo30.fastfood.domain.usecases.category.ListAllCategoriesInMenuUseCase; import br.com.fiap.grupo30.fastfood.presentation.presenters.dto.CategoryDTO; +import br.com.fiap.grupo30.fastfood.presentation.presenters.mapper.impl.CategoryDTOMapper; import io.swagger.v3.oas.annotations.Operation; import io.swagger.v3.oas.annotations.tags.Tag; import java.util.List; @@ -17,10 +18,14 @@ public class CategoryController { private final ListAllCategoriesInMenuUseCase listAllCategoriesInMenuUseCase; + private final CategoryDTOMapper categoryDTOMapper; @Autowired - public CategoryController(ListAllCategoriesInMenuUseCase listAllCategoriesInMenuUseCase) { + public CategoryController( + ListAllCategoriesInMenuUseCase listAllCategoriesInMenuUseCase, + CategoryDTOMapper categoryDTOMapper) { this.listAllCategoriesInMenuUseCase = listAllCategoriesInMenuUseCase; + this.categoryDTOMapper = categoryDTOMapper; } @GetMapping @@ -28,7 +33,10 @@ public CategoryController(ListAllCategoriesInMenuUseCase listAllCategoriesInMenu summary = "Get all categories", description = "Retrieve a list of all registered categories") public ResponseEntity> findAll() { - List list = this.listAllCategoriesInMenuUseCase.execute(); + List list = + this.listAllCategoriesInMenuUseCase.execute().stream() + .map(this.categoryDTOMapper::mapTo) + .toList(); return ResponseEntity.ok().body(list); } } 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 new file mode 100644 index 0000000..1811ff5 --- /dev/null +++ b/src/main/java/br/com/fiap/grupo30/fastfood/presentation/presenters/mapper/impl/CategoryDTOMapper.java @@ -0,0 +1,26 @@ +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.presentation.presenters.dto.CategoryDTO; +import br.com.fiap.grupo30.fastfood.presentation.presenters.mapper.Mapper; +import org.springframework.stereotype.Component; + +@Component +public class CategoryDTOMapper implements Mapper { + + @Override + public CategoryDTO mapTo(Category category) { + CategoryDTO dto = new CategoryDTO(); + dto.setId(category.getId()); + dto.setName(category.getName()); + return dto; + } + + @Override + public Category mapFrom(CategoryDTO dto) { + Category category = new Category(); + category.setId(dto.getId()); + category.setName(dto.getName()); + return category; + } +} diff --git a/src/main/java/br/com/fiap/grupo30/fastfood/presentation/presenters/mapper/impl/CategoryMapper.java b/src/main/java/br/com/fiap/grupo30/fastfood/presentation/presenters/mapper/impl/CategoryEntityMapper.java similarity index 91% rename from src/main/java/br/com/fiap/grupo30/fastfood/presentation/presenters/mapper/impl/CategoryMapper.java rename to src/main/java/br/com/fiap/grupo30/fastfood/presentation/presenters/mapper/impl/CategoryEntityMapper.java index 97bf563..2298dc5 100644 --- a/src/main/java/br/com/fiap/grupo30/fastfood/presentation/presenters/mapper/impl/CategoryMapper.java +++ b/src/main/java/br/com/fiap/grupo30/fastfood/presentation/presenters/mapper/impl/CategoryEntityMapper.java @@ -6,7 +6,7 @@ import org.springframework.stereotype.Component; @Component -public class CategoryMapper implements Mapper { +public class CategoryEntityMapper implements Mapper { @Override public CategoryEntity 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 86fa829..1bb91e4 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 @@ -14,13 +14,14 @@ public final class ProductDTOMapper implements Mapper { private final JpaCategoryRepository jpaCategoryRepository; - private final CategoryMapper categoryMapper; + private final CategoryEntityMapper categoryEntityMapper; @Autowired public ProductDTOMapper( - JpaCategoryRepository jpaCategoryRepository, CategoryMapper categoryMapper) { + JpaCategoryRepository jpaCategoryRepository, + CategoryEntityMapper categoryEntityMapper) { this.jpaCategoryRepository = jpaCategoryRepository; - this.categoryMapper = categoryMapper; + this.categoryEntityMapper = categoryEntityMapper; } @Override @@ -44,7 +45,7 @@ public ProductDTO mapFrom(ProductEntity entity) { dto.setDescription(entity.getDescription()); dto.setImgUrl(entity.getImgUrl()); dto.setPrice(entity.getPrice()); - Category category = categoryMapper.mapFrom(entity.getCategory()); + Category category = categoryEntityMapper.mapFrom(entity.getCategory()); dto.setCategory(new CategoryDTO(category)); return dto; } 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/ProductMapper.java index dcd575b..0a5b50b 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/ProductMapper.java @@ -9,11 +9,11 @@ @Component public final class ProductMapper implements Mapper { - private final CategoryMapper categoryMapper; + private final CategoryEntityMapper categoryEntityMapper; @Autowired - public ProductMapper(CategoryMapper categoryMapper) { - this.categoryMapper = categoryMapper; + public ProductMapper(CategoryEntityMapper categoryEntityMapper) { + this.categoryEntityMapper = categoryEntityMapper; } @Override @@ -24,7 +24,7 @@ public ProductEntity mapTo(Product product) { entity.setDescription(product.getDescription()); entity.setPrice(product.getPrice()); entity.setImgUrl(product.getImgUrl()); - entity.setCategory(categoryMapper.mapTo(product.getCategory())); + entity.setCategory(categoryEntityMapper.mapTo(product.getCategory())); return entity; } @@ -36,7 +36,7 @@ public Product mapFrom(ProductEntity entity) { product.setDescription(entity.getDescription()); product.setPrice(entity.getPrice()); product.setImgUrl(entity.getImgUrl()); - product.setCategory(categoryMapper.mapFrom(entity.getCategory())); + product.setCategory(categoryEntityMapper.mapFrom(entity.getCategory())); return product; } }