Skip to content
This repository has been archived by the owner on Dec 4, 2024. It is now read-only.

Commit

Permalink
refactor: switch the category classes to clean architecture
Browse files Browse the repository at this point in the history
  • Loading branch information
jonasmzsouza committed Jul 19, 2024
1 parent 4f07cad commit d83e9ee
Show file tree
Hide file tree
Showing 10 changed files with 95 additions and 43 deletions.
Original file line number Diff line number Diff line change
@@ -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<CategoryDTO> findAll();
List<Category> findAll();
}
Original file line number Diff line number Diff line change
@@ -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<Category> execute() {
return categoryGateway.findAll();
}
}

This file was deleted.

Original file line number Diff line number Diff line change
@@ -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);
}
}
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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<CategoryDTO> findAll() {
public List<Category> findAll() {
return jpaCategoryRepository.findAll().stream()
.map(entity -> new CategoryDTO(categoryMapper.mapFrom(entity)))
.map(entity -> new Category(entity.getId(), entity.getName()))
.toList();
}
}
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -17,18 +18,25 @@
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
@Operation(
summary = "Get all categories",
description = "Retrieve a list of all registered categories")
public ResponseEntity<List<CategoryDTO>> findAll() {
List<CategoryDTO> list = this.listAllCategoriesInMenuUseCase.execute();
List<CategoryDTO> list =
this.listAllCategoriesInMenuUseCase.execute().stream()
.map(this.categoryDTOMapper::mapTo)
.toList();
return ResponseEntity.ok().body(list);
}
}
Original file line number Diff line number Diff line change
@@ -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<Category, CategoryDTO> {

@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;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
import org.springframework.stereotype.Component;

@Component
public class CategoryMapper implements Mapper<Category, CategoryEntity> {
public class CategoryEntityMapper implements Mapper<Category, CategoryEntity> {

@Override
public CategoryEntity mapTo(Category category) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,14 @@
public final class ProductDTOMapper implements Mapper<ProductDTO, ProductEntity> {

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
Expand All @@ -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;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,11 @@
@Component
public final class ProductMapper implements Mapper<Product, ProductEntity> {

private final CategoryMapper categoryMapper;
private final CategoryEntityMapper categoryEntityMapper;

@Autowired
public ProductMapper(CategoryMapper categoryMapper) {
this.categoryMapper = categoryMapper;
public ProductMapper(CategoryEntityMapper categoryEntityMapper) {
this.categoryEntityMapper = categoryEntityMapper;
}

@Override
Expand All @@ -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;
}

Expand All @@ -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;
}
}

0 comments on commit d83e9ee

Please sign in to comment.