This repository has been archived by the owner on Dec 4, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #33 from 7SOATSquad30/refactor/customer-clean-arch…
…itecture refactor: switch the customer to a clean architecture
- Loading branch information
Showing
10 changed files
with
115 additions
and
75 deletions.
There are no files selected for viewing
6 changes: 3 additions & 3 deletions
6
src/main/java/br/com/fiap/grupo30/fastfood/domain/repositories/CustomerRepository.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,10 +1,10 @@ | ||
package br.com.fiap.grupo30.fastfood.domain.repositories; | ||
|
||
import br.com.fiap.grupo30.fastfood.presentation.presenters.dto.CustomerDTO; | ||
import br.com.fiap.grupo30.fastfood.domain.entities.Customer; | ||
|
||
public interface CustomerRepository { | ||
|
||
CustomerDTO findCustomerByCpf(String cpf); | ||
Customer findCustomerByCpf(String cpf); | ||
|
||
CustomerDTO insert(CustomerDTO dto); | ||
Customer insert(Customer dto); | ||
} |
16 changes: 7 additions & 9 deletions
16
.../java/br/com/fiap/grupo30/fastfood/domain/usecases/customer/FindCustomerByCpfUseCase.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,19 +1,17 @@ | ||
package br.com.fiap.grupo30.fastfood.domain.usecases.customer; | ||
|
||
import br.com.fiap.grupo30.fastfood.domain.repositories.CustomerRepository; | ||
import br.com.fiap.grupo30.fastfood.presentation.presenters.dto.CustomerDTO; | ||
import org.springframework.stereotype.Component; | ||
import br.com.fiap.grupo30.fastfood.domain.entities.Customer; | ||
import br.com.fiap.grupo30.fastfood.infrastructure.gateways.CustomerGateway; | ||
|
||
@Component | ||
public class FindCustomerByCpfUseCase { | ||
|
||
private final CustomerRepository customerRepository; | ||
private final CustomerGateway customerGateway; | ||
|
||
public FindCustomerByCpfUseCase(CustomerRepository customerRepository) { | ||
this.customerRepository = customerRepository; | ||
public FindCustomerByCpfUseCase(CustomerGateway customerGateway) { | ||
this.customerGateway = customerGateway; | ||
} | ||
|
||
public CustomerDTO execute(String cpf) { | ||
return customerRepository.findCustomerByCpf(cpf); | ||
public Customer execute(String cpf) { | ||
return customerGateway.findCustomerByCpf(cpf); | ||
} | ||
} |
16 changes: 7 additions & 9 deletions
16
...ava/br/com/fiap/grupo30/fastfood/domain/usecases/customer/RegisterNewCustomerUseCase.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,19 +1,17 @@ | ||
package br.com.fiap.grupo30.fastfood.domain.usecases.customer; | ||
|
||
import br.com.fiap.grupo30.fastfood.domain.repositories.CustomerRepository; | ||
import br.com.fiap.grupo30.fastfood.presentation.presenters.dto.CustomerDTO; | ||
import org.springframework.stereotype.Component; | ||
import br.com.fiap.grupo30.fastfood.domain.entities.Customer; | ||
import br.com.fiap.grupo30.fastfood.infrastructure.gateways.CustomerGateway; | ||
|
||
@Component | ||
public class RegisterNewCustomerUseCase { | ||
|
||
private final CustomerRepository customerRepository; | ||
private final CustomerGateway customerGateway; | ||
|
||
public RegisterNewCustomerUseCase(CustomerRepository customerRepository) { | ||
this.customerRepository = customerRepository; | ||
public RegisterNewCustomerUseCase(CustomerGateway customerGateway) { | ||
this.customerGateway = customerGateway; | ||
} | ||
|
||
public CustomerDTO execute(CustomerDTO dto) { | ||
return customerRepository.insert(dto); | ||
public Customer execute(Customer customer) { | ||
return customerGateway.insert(customer); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -7,7 +7,7 @@ | |
import br.com.fiap.grupo30.fastfood.presentation.presenters.dto.CustomerDTO; | ||
import br.com.fiap.grupo30.fastfood.presentation.presenters.dto.OrderDTO; | ||
import br.com.fiap.grupo30.fastfood.presentation.presenters.exceptions.ResourceNotFoundException; | ||
import br.com.fiap.grupo30.fastfood.presentation.presenters.mapper.impl.CustomerMapper; | ||
import br.com.fiap.grupo30.fastfood.presentation.presenters.mapper.impl.CustomerEntityMapper; | ||
import jakarta.transaction.Transactional; | ||
import java.util.Optional; | ||
import org.springframework.beans.factory.annotation.Autowired; | ||
|
@@ -18,16 +18,16 @@ public class StartNewOrderUseCase { | |
|
||
private final JpaOrderRepository jpaOrderRepository; | ||
private final JpaCustomerRepository jpaCustomerRepository; | ||
public final CustomerMapper customerMapper; | ||
public final CustomerEntityMapper customerEntityMapper; | ||
|
||
@Autowired | ||
public StartNewOrderUseCase( | ||
JpaOrderRepository jpaOrderRepository, | ||
JpaCustomerRepository jpaCustomerRepository, | ||
CustomerMapper customerMapper) { | ||
CustomerEntityMapper customerEntityMapper) { | ||
this.jpaOrderRepository = jpaOrderRepository; | ||
this.jpaCustomerRepository = jpaCustomerRepository; | ||
this.customerMapper = customerMapper; | ||
this.customerEntityMapper = customerEntityMapper; | ||
} | ||
|
||
@Transactional | ||
|
@@ -55,15 +55,15 @@ private CustomerDTO findOrCreateAnonymousCustomer() { | |
jpaCustomerRepository.findCustomerByCpf(anonymousCpf); | ||
|
||
if (anonymousCustomer.isPresent()) { | ||
return new CustomerDTO(customerMapper.mapFrom(anonymousCustomer.get())); | ||
return new CustomerDTO(customerEntityMapper.mapFrom(anonymousCustomer.get())); | ||
} else { | ||
CustomerEntity newAnonymousCustomer = new CustomerEntity(); | ||
newAnonymousCustomer.setCpf(anonymousCpf); | ||
newAnonymousCustomer.setName("Anonymous"); | ||
newAnonymousCustomer.setEmail("[email protected]"); | ||
CustomerEntity savedAnonymousCustomer = | ||
jpaCustomerRepository.save(newAnonymousCustomer); | ||
return new CustomerDTO(customerMapper.mapFrom(savedAnonymousCustomer)); | ||
return new CustomerDTO(customerEntityMapper.mapFrom(savedAnonymousCustomer)); | ||
} | ||
} | ||
} |
31 changes: 31 additions & 0 deletions
31
...java/br/com/fiap/grupo30/fastfood/infrastructure/configuration/CustomerConfiguration.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
package br.com.fiap.grupo30.fastfood.infrastructure.configuration; | ||
|
||
import br.com.fiap.grupo30.fastfood.domain.repositories.CustomerRepository; | ||
import br.com.fiap.grupo30.fastfood.domain.usecases.customer.FindCustomerByCpfUseCase; | ||
import br.com.fiap.grupo30.fastfood.domain.usecases.customer.RegisterNewCustomerUseCase; | ||
import br.com.fiap.grupo30.fastfood.infrastructure.gateways.CustomerGateway; | ||
import br.com.fiap.grupo30.fastfood.infrastructure.persistence.repositories.JpaCustomerRepository; | ||
import br.com.fiap.grupo30.fastfood.presentation.presenters.mapper.impl.CustomerEntityMapper; | ||
import org.springframework.context.annotation.Bean; | ||
import org.springframework.context.annotation.Configuration; | ||
|
||
@Configuration | ||
public class CustomerConfiguration { | ||
|
||
@Bean | ||
public CustomerRepository customerRepository( | ||
JpaCustomerRepository jpaCustomerRepository, | ||
CustomerEntityMapper customerEntityMapper) { | ||
return new CustomerGateway(jpaCustomerRepository, customerEntityMapper); | ||
} | ||
|
||
@Bean | ||
public FindCustomerByCpfUseCase findCustomerByCpfUseCase(CustomerGateway customerGateway) { | ||
return new FindCustomerByCpfUseCase(customerGateway); | ||
} | ||
|
||
@Bean | ||
public RegisterNewCustomerUseCase registerNewCustomerUseCase(CustomerGateway customerGateway) { | ||
return new RegisterNewCustomerUseCase(customerGateway); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
37 changes: 16 additions & 21 deletions
37
...a/br/com/fiap/grupo30/fastfood/presentation/presenters/mapper/impl/CustomerDTOMapper.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,36 +1,31 @@ | ||
package br.com.fiap.grupo30.fastfood.presentation.presenters.mapper.impl; | ||
|
||
import br.com.fiap.grupo30.fastfood.infrastructure.persistence.entities.CustomerEntity; | ||
import br.com.fiap.grupo30.fastfood.domain.entities.Customer; | ||
import br.com.fiap.grupo30.fastfood.domain.valueobjects.CPF; | ||
import br.com.fiap.grupo30.fastfood.presentation.presenters.dto.CustomerDTO; | ||
import br.com.fiap.grupo30.fastfood.presentation.presenters.mapper.BiDirectionalMapper; | ||
import org.springframework.stereotype.Component; | ||
|
||
@Component | ||
public final class CustomerDTOMapper implements BiDirectionalMapper<CustomerDTO, CustomerEntity> { | ||
public final class CustomerDTOMapper implements BiDirectionalMapper<Customer, CustomerDTO> { | ||
|
||
@Override | ||
public CustomerEntity mapTo(CustomerDTO dto) { | ||
CustomerEntity entity = new CustomerEntity(); | ||
entity.setId(dto.getId()); | ||
entity.setName(dto.getName()); | ||
entity.setCpf(dto.getCpf()); | ||
entity.setEmail(dto.getEmail()); | ||
return entity; | ||
public CustomerDTO mapTo(Customer customer) { | ||
CustomerDTO dto = new CustomerDTO(); | ||
dto.setId(customer.getId()); | ||
dto.setName(customer.getName()); | ||
dto.setCpf(customer.getCpf().value()); | ||
dto.setEmail(customer.getEmail()); | ||
return dto; | ||
} | ||
|
||
@Override | ||
public CustomerDTO mapFrom(CustomerEntity entity) { | ||
CustomerDTO customer = new CustomerDTO(); | ||
customer.setId(entity.getId()); | ||
customer.setName(entity.getName()); | ||
customer.setCpf(entity.getCpf()); | ||
customer.setEmail(entity.getEmail()); | ||
public Customer mapFrom(CustomerDTO dto) { | ||
Customer customer = new Customer(); | ||
customer.setId(dto.getId()); | ||
customer.setName(dto.getName()); | ||
customer.setCpf(new CPF(dto.getCpf())); | ||
customer.setEmail(dto.getEmail()); | ||
return customer; | ||
} | ||
|
||
public void updateEntityFromDTO(CustomerEntity entity, CustomerDTO dto) { | ||
entity.setName(dto.getName()); | ||
entity.setCpf(dto.getCpf()); | ||
entity.setEmail(dto.getEmail()); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters