Skip to content

Commit

Permalink
Merge pull request #316 from vinho-notas/VIN-391-Refatorar-as-classes…
Browse files Browse the repository at this point in the history
…-que-recebem-AddressInputDTO

feat: VIN-391 - refatorando as classes que recebem AddressInputDTO
  • Loading branch information
vanderleik committed Mar 21, 2024
2 parents 584f8ad + 332bf2c commit 72a9c57
Show file tree
Hide file tree
Showing 17 changed files with 98 additions and 123 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@ public class AddressInputDTO {
private String district;
private String zipCode;
private String city;
private StateInputDTO uf;
private CountryInputDTO country;
private String uf;
private String country;
private String phoneNumber;
private LocalDateTime dthreg;
private String userreg;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@
import com.vinhonotas.bff.application.services.exceptions.BadRequestException;
import com.vinhonotas.bff.client.cadastro.AddressClient;
import com.vinhonotas.bff.interfaces.dtos.inputs.cadastro.AddressInputDTO;
import com.vinhonotas.bff.interfaces.dtos.inputs.cadastro.CountryInputDTO;
import com.vinhonotas.bff.interfaces.dtos.inputs.cadastro.StateInputDTO;
import com.vinhonotas.bff.interfaces.dtos.outputs.cadastro.AddressOutputDTO;
import com.vinhonotas.bff.interfaces.dtos.outputs.cadastro.CountryOutputDTO;
import com.vinhonotas.bff.interfaces.dtos.outputs.cadastro.StateOutputDTO;
Expand Down Expand Up @@ -198,8 +196,8 @@ private AddressInputDTO createAddressInputDTO() {
.district("district")
.zipCode("00000-000")
.city("Blumenau")
.uf(Mockito.mock(StateInputDTO.class))
.country(Mockito.mock(CountryInputDTO.class))
.uf("SC")
.country("Brasil")
.phoneNumber("0000000000")
.build();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@
import com.vinhonotas.bff.application.services.cadastro.AddressService;
import com.vinhonotas.bff.application.services.exceptions.BadRequestException;
import com.vinhonotas.bff.interfaces.dtos.inputs.cadastro.AddressInputDTO;
import com.vinhonotas.bff.interfaces.dtos.inputs.cadastro.CountryInputDTO;
import com.vinhonotas.bff.interfaces.dtos.inputs.cadastro.StateInputDTO;
import com.vinhonotas.bff.interfaces.dtos.outputs.cadastro.AddressOutputDTO;
import com.vinhonotas.bff.interfaces.dtos.outputs.cadastro.CountryOutputDTO;
import com.vinhonotas.bff.interfaces.dtos.outputs.cadastro.StateOutputDTO;
Expand Down Expand Up @@ -208,24 +206,9 @@ private AddressInputDTO createAddressInputDTO() {
.district("Bairro 1")
.zipCode("00000-000")
.city("Cidade 1")
.uf(createStateInputDTO())
.country(createCountryInputDTO())
.phoneNumber("00000000000")
.build();
}

private StateInputDTO createStateInputDTO() {
return StateInputDTO.builder()
.stateName("Santa Catarina")
.uf("SC")
.country(createCountryInputDTO())
.build();
}

private CountryInputDTO createCountryInputDTO() {
return CountryInputDTO.builder()
.countryName("Brasil")
.continentName("América do Sul")
.country("Brasil")
.phoneNumber("00000000000")
.build();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,8 @@
import com.fasterxml.jackson.databind.ObjectMapper;
import com.vinhonotas.bff.application.services.cadastro.PersonService;
import com.vinhonotas.bff.application.services.exceptions.BadRequestException;
import com.vinhonotas.bff.interfaces.controllers.cadastro.PersonController;
import com.vinhonotas.bff.interfaces.dtos.inputs.cadastro.AddressInputDTO;
import com.vinhonotas.bff.interfaces.dtos.inputs.cadastro.CountryInputDTO;
import com.vinhonotas.bff.interfaces.dtos.inputs.cadastro.PersonInputDTO;
import com.vinhonotas.bff.interfaces.dtos.inputs.cadastro.StateInputDTO;
import com.vinhonotas.bff.interfaces.dtos.outputs.cadastro.AddressOutputDTO;
import com.vinhonotas.bff.interfaces.dtos.outputs.cadastro.CountryOutputDTO;
import com.vinhonotas.bff.interfaces.dtos.outputs.cadastro.PersonOutputDTO;
Expand Down Expand Up @@ -232,28 +229,13 @@ private AddressInputDTO createAddressInputDTO() {
.complement("Complemento da pessoa")
.district("Bairro da pessoa")
.city("Cidade da pessoa")
.uf(createStateInputDTO())
.country(createCountryInputDTO())
.uf("SC")
.country("Brasil")
.zipCode("12345678")
.phoneNumber("12345678910")
.build();
}

private CountryInputDTO createCountryInputDTO() {
return CountryInputDTO.builder()
.countryName("Brasil")
.continentName("América do Sul")
.build();
}

private StateInputDTO createStateInputDTO() {
return StateInputDTO.builder()
.stateName("Nome do estado")
.uf("UF")
.country(createCountryInputDTO())
.build();
}

private PersonOutputDTO createPersonOutputDTO() {
return PersonOutputDTO.builder()
.id(UUID.fromString("123e4567-e89b-12d3-a456-426614174000"))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -232,30 +232,15 @@ private AddressInputDTO createAddressInputDTO() {
.addressDescription("Rua Teste")
.addressNumber(123)
.city("Cidade Teste")
.uf(createStateInputDTO())
.uf("SC")
.complement("Complemento Teste")
.country(createCountryInputDTO())
.country("Brasil")
.district("Bairro Teste")
.phoneNumber("123456789")
.zipCode("12345678")
.build();
}

private StateInputDTO createStateInputDTO() {
return StateInputDTO.builder()
.stateName("São Paulo")
.uf("SP")
.country(createCountryInputDTO())
.build();
}

private CountryInputDTO createCountryInputDTO() {
return CountryInputDTO.builder()
.countryName("Brasil")
.continentName("América do Sul")
.build();
}

private UserOutputDTO createUserOutputDTO() {
return UserOutputDTO.builder()
.id(UUID.fromString("8d39bcba-cb01-4103-b562-93c84a89c972"))
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
package com.vinhonotas.cadastro.application.converters;

import com.vinhonotas.cadastro.domain.entities.AddressEntity;
import com.vinhonotas.cadastro.domain.entities.CountryEntity;
import com.vinhonotas.cadastro.domain.entities.StateEntity;
import com.vinhonotas.cadastro.infrastructure.CountryRepository;
import com.vinhonotas.cadastro.infrastructure.StateRepository;
import com.vinhonotas.cadastro.interfaces.dtos.inputs.AddressInputDTO;
import com.vinhonotas.cadastro.interfaces.dtos.outputs.AddressOutputDTO;
import lombok.RequiredArgsConstructor;
Expand All @@ -16,17 +20,21 @@ public class AddressConverter {

private final StateConverter stateConverter;
private final CountryConverter countryConverter;
private final StateRepository stateRepository;
private final CountryRepository countryRepository;

public AddressEntity convertToEntity(AddressInputDTO addressInputDTO) {
StateEntity state = stateRepository.findByUf(addressInputDTO.getUf());
CountryEntity country = countryRepository.findByCountryName(addressInputDTO.getCountry());
return AddressEntity.builder()
.addressDescription(addressInputDTO.getAddressDescription())
.addressNumber(addressInputDTO.getAddressNumber())
.complement(addressInputDTO.getComplement())
.district(addressInputDTO.getDistrict())
.zipCode(addressInputDTO.getZipCode())
.city(addressInputDTO.getCity())
.uf(stateConverter.convertToEntity(addressInputDTO.getUf()))
.country(countryConverter.convertToEntity(addressInputDTO.getCountry()))
.uf(state)
.country(country)
.phoneNumber(addressInputDTO.getPhoneNumber())
.userreg(addressInputDTO.getUserreg())
.dthreg(LocalDateTime.now())
Expand All @@ -47,10 +55,8 @@ public AddressEntity convertToEntityUpdate(AddressEntity entity, UUID id, Addres
.district(addressInputDTO.getDistrict() != null ? addressInputDTO.getDistrict() : entity.getDistrict())
.zipCode(addressInputDTO.getZipCode() != null ? addressInputDTO.getZipCode() : entity.getZipCode())
.city(addressInputDTO.getCity() != null ? addressInputDTO.getCity() : entity.getCity())
.uf(addressInputDTO.getUf() != null ? stateConverter.convertToEntity(addressInputDTO.getUf()) : entity
.getUf())
.country(addressInputDTO.getCountry() != null ? countryConverter.convertToEntity(addressInputDTO
.getCountry()) : entity.getCountry())
.uf(entity.getUf())
.country(entity.getCountry())
.phoneNumber(addressInputDTO.getPhoneNumber() != null ? addressInputDTO.getPhoneNumber() : entity
.getPhoneNumber())
.userreg(addressInputDTO.getUserreg() != null ? addressInputDTO.getUserreg() : entity.getUserreg())
Expand Down Expand Up @@ -104,8 +110,8 @@ public AddressInputDTO convertToInputDTO(AddressEntity address) {
.district(address.getDistrict())
.zipCode(address.getZipCode())
.city(address.getCity())
.uf(stateConverter.convertToInputDTO(address.getUf()))
.country(countryConverter.convertToInputDTO(address.getCountry()))
.uf(address.getUf().getUf())
.country(address.getCountry().getCountryName())
.phoneNumber(address.getPhoneNumber())
.userreg(address.getUserreg())
.dthreg(address.getDthreg())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,17 +55,15 @@ public AddressEntity create(AddressInputDTO addressInputDTO) {
}

private CountryEntity getCountryEntity(AddressInputDTO addressInputDTO) {
log.info("Buscando país pelo id: {}", addressInputDTO.getCountry().getId());
CountryEntity country = countryRepository.findById(UUID.fromString(addressInputDTO.getCountry().getId()))
.orElseThrow(() -> new BadRequestException(MessagesConstants.COUNTRY_NOT_FOUND_WITH_ID + addressInputDTO.getCountry().getId()));
log.info("Buscando país pelo nome: {}", addressInputDTO.getCountry());
CountryEntity country = countryRepository.findByCountryName(addressInputDTO.getCountry());
log.info("País encontrado: {}", country.toString());
return country;
}

private StateEntity getStateEntity(AddressInputDTO addressInputDTO) {
log.info("Buscando estado pelo id: {}", addressInputDTO.getUf().getId());
StateEntity state = stateRepository.findById(UUID.fromString(addressInputDTO.getUf().getId()))
.orElseThrow(() -> new BadRequestException(MessagesConstants.STATE_NOT_FOUND_WITH_ID + addressInputDTO.getUf().getId()));
log.info("Buscando estado pela UF: {}", addressInputDTO.getUf());
StateEntity state = stateRepository.findByUf(addressInputDTO.getUf());
log.info("Estado encontrado: {}", state.toString());
return state;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,24 +49,22 @@ public PersonEntity create(PersonInputDTO personInputDTO) {
}

private void existsCountryByCountryName(PersonInputDTO personInputDTO) {
CountryEntity country = countryService.getByName(personInputDTO.getAddress().getCountry().getCountryName());
CountryEntity country = countryService.getByName(personInputDTO.getAddress().getCountry());
if (Objects.isNull(country)) {
throw new BadRequestException(MessagesConstants.COUNTRY_NOT_FOUND_WITH_NAME + personInputDTO.getAddress().getCountry().getCountryName());
throw new BadRequestException(MessagesConstants.COUNTRY_NOT_FOUND_WITH_NAME + personInputDTO.getAddress().getCountry());
}
log.info("Salvando um país com os dados: {}", country.toString());
personInputDTO.getAddress().setCountry(countryConverter.convertToInputDTO(country));
personInputDTO.getAddress().getUf().setCountry(countryConverter.convertToInputDTO(country));
personInputDTO.getAddress().setCountry(countryConverter.convertToInputDTO(country).getCountryName());
}

private void existsStateByUf(PersonInputDTO personInputDTO) {
StateEntity state = stateService.getByUf(personInputDTO.getAddress().getUf().getUf());
StateEntity state = stateService.getByUf(personInputDTO.getAddress().getUf());
if (Objects.isNull(state)) {
log.error("create :: Ocorreu um erro: {}", MessagesConstants.STATE_NOT_FOUND);
throw new BadRequestException(MessagesConstants.STATE_NOT_FOUND);
} else {
log.info("Salvando um estado com os dados: {}", state.toString());
personInputDTO.getAddress().getUf().setId(state.getId().toString());
personInputDTO.getAddress().getUf().setStateName(state.getStateName());
personInputDTO.getAddress().setUf(state.getUf());
}
}

Expand Down Expand Up @@ -142,15 +140,15 @@ private void updatePersonAddress(PersonInputDTO personInputDTO, PersonEntity exi
address.setCity(personInputDTO.getAddress().getCity());
address.setPhoneNumber(personInputDTO.getAddress().getPhoneNumber());

StateEntity state = stateService.getByUf(personInputDTO.getAddress().getUf().getUf());
StateEntity state = stateService.getByUf(personInputDTO.getAddress().getUf());
if (state == null) {
throw new BadRequestException(MessagesConstants.STATE_NOT_FOUND);
}
address.setUf(state);

CountryEntity country = countryService.getByName(personInputDTO.getAddress().getCountry().getCountryName());
CountryEntity country = countryService.getByName(personInputDTO.getAddress().getCountry());
if (country == null) {
throw new BadRequestException(MessagesConstants.COUNTRY_NOT_FOUND_WITH_NAME + personInputDTO.getAddress().getCountry().getCountryName());
throw new BadRequestException(MessagesConstants.COUNTRY_NOT_FOUND_WITH_NAME + personInputDTO.getAddress().getCountry());
}
address.setCountry(country);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@ public class AddressInputDTO {
private String district;
private String zipCode;
private String city;
private StateInputDTO uf;
private CountryInputDTO country;
private String uf;
private String country;
private String phoneNumber;
private LocalDateTime dthreg;
private String userreg;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
import com.vinhonotas.cadastro.domain.entities.AddressEntity;
import com.vinhonotas.cadastro.domain.entities.CountryEntity;
import com.vinhonotas.cadastro.domain.entities.StateEntity;
import com.vinhonotas.cadastro.infrastructure.CountryRepository;
import com.vinhonotas.cadastro.infrastructure.StateRepository;
import com.vinhonotas.cadastro.interfaces.dtos.inputs.AddressInputDTO;
import com.vinhonotas.cadastro.interfaces.dtos.inputs.CountryInputDTO;
import com.vinhonotas.cadastro.interfaces.dtos.inputs.StateInputDTO;
Expand Down Expand Up @@ -34,6 +36,10 @@ class AddressConverterTest {
private StateConverter stateConverter;
@Mock
private CountryConverter countryConverter;
@Mock
private StateRepository stateRepository;
@Mock
private CountryRepository countryRepository;

private AddressInputDTO addressInputDTO;
private AddressEntity addressEntity;
Expand All @@ -49,6 +55,9 @@ void setUp() {
@Test
@DisplayName("Teste de conversão de AddressInputDTO para AddressEntity")
void testToEntity() {
when(stateRepository.findByUf(Mockito.anyString())).thenReturn(createStateEntity());
when(countryRepository.findByCountryName(Mockito.anyString())).thenReturn(createCountryEntity());

AddressEntity addressEntity = assertDoesNotThrow(()-> addressConverter.convertToEntity(addressInputDTO));
assertNotNull(addressEntity);
assertEquals(addressInputDTO.getAddressDescription(), addressEntity.getAddressDescription());
Expand All @@ -57,8 +66,8 @@ void testToEntity() {
assertEquals(addressInputDTO.getDistrict(), addressEntity.getDistrict());
assertEquals(addressInputDTO.getZipCode(), addressEntity.getZipCode());
assertEquals(addressInputDTO.getCity(), addressEntity.getCity());
assertEquals(stateConverter.convertToEntity(addressInputDTO.getUf()), addressEntity.getUf());
assertEquals(countryConverter.convertToEntity(addressInputDTO.getCountry()), addressEntity.getCountry());
assertEquals(createStateEntity(), addressEntity.getUf());
assertEquals(createCountryEntity(), addressEntity.getCountry());
assertEquals(addressInputDTO.getPhoneNumber(), addressEntity.getPhoneNumber());
}

Expand All @@ -75,8 +84,8 @@ void testToEntityUpdate() {
assertEquals(addressInputDTO.getDistrict(), addressEntityUpdate.getDistrict());
assertEquals(addressInputDTO.getZipCode(), addressEntityUpdate.getZipCode());
assertEquals(addressInputDTO.getCity(), addressEntityUpdate.getCity());
assertEquals(stateConverter.convertToEntity(addressInputDTO.getUf()), addressEntityUpdate.getUf());
assertEquals(countryConverter.convertToEntity(addressInputDTO.getCountry()), addressEntityUpdate.getCountry());
assertEquals(createStateEntity(), addressEntityUpdate.getUf());
assertEquals(createCountryEntity(), addressEntityUpdate.getCountry());
assertEquals(addressInputDTO.getPhoneNumber(), addressEntityUpdate.getPhoneNumber());
}

Expand Down Expand Up @@ -161,8 +170,8 @@ private AddressEntity createAddressEntity() {
.district("district")
.zipCode("00000-000")
.city("Blumenau")
.uf(Mockito.mock(StateEntity.class))
.country(Mockito.mock(CountryEntity.class))
.uf(createStateEntity())
.country(createCountryEntity())
.phoneNumber("0000000000")
.build();
}
Expand All @@ -175,8 +184,8 @@ private AddressInputDTO createAddressInputDTO() {
.district("district")
.zipCode("00000-000")
.city("Blumenau")
.uf(createStateInputDTO())
.country(createCountryInputDTO())
.uf("SC")
.country("Brasil")
.phoneNumber("0000000000")
.build();
}
Expand All @@ -189,6 +198,23 @@ private StateInputDTO createStateInputDTO() {
.build();
}

private StateEntity createStateEntity() {
return StateEntity.builder()
.id(UUID.fromString("f5d2e9e0-0b7e-4b1e-9b0a-0e9f5b9b6b1a"))
.stateName("Santa Catarina")
.uf("SC")
.country(createCountryEntity())
.build();
}

private CountryEntity createCountryEntity() {
return CountryEntity.builder()
.id(UUID.fromString("f5d2e9e0-0b7e-4b1e-9b0a-0e9f5b9b6b1a"))
.countryName("Brasil")
.continentName("America do Sul")
.build();
}

private CountryInputDTO createCountryInputDTO() {
return CountryInputDTO.builder()
.countryName("Brasil")
Expand Down
Loading

0 comments on commit 72a9c57

Please sign in to comment.