-
Notifications
You must be signed in to change notification settings - Fork 0
Feat/us04-81: endpoints para busca de UF e municipios brasileiros #83
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
d42acd7
feat: Cria endpoints de busca de UFs e municipios
femelloffm 16f90fc
feat: Renomeia classes e implementa testes unitarios
femelloffm 7e01aa5
fix: Corrige erros de compilacao em DEV
femelloffm b19a5fa
fix: Resolve conflitos
femelloffm a42c169
Merge branch 'dev' into feat/US04-81
femelloffm File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
73 changes: 73 additions & 0 deletions
73
pointtils/src/main/java/com/pointtils/pointtils/src/application/clients/IbgeClient.java
This file contains hidden or 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,73 @@ | ||
package com.pointtils.pointtils.src.application.clients; | ||
|
||
import com.pointtils.pointtils.src.application.dto.CityIbgeResponseDTO; | ||
import com.pointtils.pointtils.src.application.dto.StateDataDTO; | ||
import com.pointtils.pointtils.src.application.dto.StateIbgeResponseDTO; | ||
import com.pointtils.pointtils.src.core.domain.exceptions.ClientTimeoutException; | ||
import jakarta.persistence.EntityNotFoundException; | ||
import lombok.extern.slf4j.Slf4j; | ||
import org.apache.commons.lang3.ArrayUtils; | ||
import org.springframework.beans.factory.annotation.Qualifier; | ||
import org.springframework.beans.factory.annotation.Value; | ||
import org.springframework.http.ResponseEntity; | ||
import org.springframework.stereotype.Component; | ||
import org.springframework.web.client.ResourceAccessException; | ||
import org.springframework.web.client.RestTemplate; | ||
|
||
import java.util.Arrays; | ||
import java.util.Comparator; | ||
import java.util.List; | ||
import java.util.function.Function; | ||
|
||
@Slf4j | ||
@Component | ||
public class IbgeClient { | ||
|
||
private final RestTemplate restTemplate; | ||
private final String stateUrl; | ||
private final String cityUrl; | ||
|
||
public IbgeClient(@Qualifier("ibgeRestTemplate") RestTemplate restTemplate, | ||
@Value("${client.ibge.state-url}") String stateUrl, | ||
@Value("${client.ibge.city-url}") String cityUrl) { | ||
this.restTemplate = restTemplate; | ||
this.stateUrl = stateUrl; | ||
this.cityUrl = cityUrl; | ||
} | ||
|
||
public List<StateDataDTO> getStateList() { | ||
try { | ||
log.info("Iniciando uma requisicao para a API do IBGE para buscar a lista de UFs"); | ||
ResponseEntity<StateIbgeResponseDTO[]> stateResponse = restTemplate.getForEntity(stateUrl, StateIbgeResponseDTO[].class); | ||
Function<StateIbgeResponseDTO, StateDataDTO> mapper = state -> new StateDataDTO(state.getAbbreviation()); | ||
return formatResponseData(stateResponse.getBody(), mapper, "UFs não encontradas"); | ||
} catch (ResourceAccessException ex) { | ||
log.error("Timeout na requisicao para a API do IBGE para buscar a lista de UFs", ex); | ||
throw new ClientTimeoutException("Timeout ao acessar o serviço de UFs"); | ||
} | ||
} | ||
|
||
public List<StateDataDTO> getCityListByState(String state) { | ||
try { | ||
log.info("Iniciando uma requisicao para a API do IBGE para buscar a lista de municípios da UF {}", state); | ||
ResponseEntity<CityIbgeResponseDTO[]> cityResponse = restTemplate.getForEntity(cityUrl, CityIbgeResponseDTO[].class, state); | ||
Function<CityIbgeResponseDTO, StateDataDTO> mapper = city -> new StateDataDTO(city.getName()); | ||
femelloffm marked this conversation as resolved.
Show resolved
Hide resolved
|
||
return formatResponseData(cityResponse.getBody(), mapper, "Municípios não encontrados"); | ||
} catch (ResourceAccessException ex) { | ||
log.error("Timeout na requisicao para a API do IBGE para buscar a lista de municípios da UF {}", state, ex); | ||
throw new ClientTimeoutException("Timeout ao acessar o serviço de municípios"); | ||
} | ||
} | ||
|
||
private <T> List<StateDataDTO> formatResponseData(T[] responseBody, | ||
Function<? super T, StateDataDTO> mapper, | ||
String errorMessage) { | ||
if (ArrayUtils.isEmpty(responseBody)) { | ||
throw new EntityNotFoundException(errorMessage); | ||
} | ||
return Arrays.stream(responseBody) | ||
.map(mapper) | ||
.sorted(Comparator.comparing(StateDataDTO::getName)) | ||
.toList(); | ||
} | ||
} |
38 changes: 38 additions & 0 deletions
38
...ls/src/main/java/com/pointtils/pointtils/src/application/controllers/StateController.java
This file contains hidden or 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,38 @@ | ||
package com.pointtils.pointtils.src.application.controllers; | ||
|
||
import com.pointtils.pointtils.src.application.dto.StateResponseDTO; | ||
import com.pointtils.pointtils.src.application.services.StateService; | ||
import io.swagger.v3.oas.annotations.Operation; | ||
import io.swagger.v3.oas.annotations.security.SecurityRequirement; | ||
import io.swagger.v3.oas.annotations.tags.Tag; | ||
import lombok.RequiredArgsConstructor; | ||
import org.springframework.http.HttpStatus; | ||
import org.springframework.web.bind.annotation.GetMapping; | ||
import org.springframework.web.bind.annotation.PathVariable; | ||
import org.springframework.web.bind.annotation.RequestMapping; | ||
import org.springframework.web.bind.annotation.ResponseStatus; | ||
import org.springframework.web.bind.annotation.RestController; | ||
|
||
@RestController | ||
@RequiredArgsConstructor | ||
@RequestMapping("/v1/states") | ||
@SecurityRequirement(name = "bearerAuth") | ||
@Tag(name = "State Controller", description = "Endpoints para gerenciar dados de UFs brasileiras") | ||
public class StateController { | ||
|
||
private final StateService stateService; | ||
|
||
@GetMapping | ||
@ResponseStatus(HttpStatus.OK) | ||
@Operation(summary = "Busca todas as UFs brasileiras") | ||
public StateResponseDTO getStates() { | ||
return stateService.getAllStates(); | ||
} | ||
|
||
@GetMapping("/{stateId}/cities") | ||
@ResponseStatus(HttpStatus.OK) | ||
@Operation(summary = "Busca todos os municípios de uma determinada UF brasileira") | ||
public StateResponseDTO getCitiesByState(@PathVariable String stateId) { | ||
femelloffm marked this conversation as resolved.
Show resolved
Hide resolved
|
||
return stateService.getCitiesByState(stateId); | ||
} | ||
} |
19 changes: 19 additions & 0 deletions
19
pointtils/src/main/java/com/pointtils/pointtils/src/application/dto/CityIbgeResponseDTO.java
This file contains hidden or 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,19 @@ | ||
package com.pointtils.pointtils.src.application.dto; | ||
|
||
import com.fasterxml.jackson.annotation.JsonProperty; | ||
import lombok.AllArgsConstructor; | ||
import lombok.Getter; | ||
import lombok.NoArgsConstructor; | ||
import lombok.Setter; | ||
|
||
@Getter | ||
@Setter | ||
@NoArgsConstructor | ||
@AllArgsConstructor | ||
public class CityIbgeResponseDTO { | ||
|
||
private Long id; | ||
|
||
@JsonProperty("nome") | ||
private String name; | ||
} |
15 changes: 15 additions & 0 deletions
15
pointtils/src/main/java/com/pointtils/pointtils/src/application/dto/StateDataDTO.java
This file contains hidden or 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,15 @@ | ||
package com.pointtils.pointtils.src.application.dto; | ||
|
||
import lombok.AllArgsConstructor; | ||
import lombok.Getter; | ||
import lombok.NoArgsConstructor; | ||
import lombok.Setter; | ||
|
||
@Getter | ||
@Setter | ||
@NoArgsConstructor | ||
@AllArgsConstructor | ||
public class StateDataDTO { | ||
|
||
private String name; | ||
} |
22 changes: 22 additions & 0 deletions
22
...ttils/src/main/java/com/pointtils/pointtils/src/application/dto/StateIbgeResponseDTO.java
This file contains hidden or 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,22 @@ | ||
package com.pointtils.pointtils.src.application.dto; | ||
|
||
import com.fasterxml.jackson.annotation.JsonProperty; | ||
import lombok.AllArgsConstructor; | ||
import lombok.Getter; | ||
import lombok.NoArgsConstructor; | ||
import lombok.Setter; | ||
|
||
@Getter | ||
@Setter | ||
@NoArgsConstructor | ||
@AllArgsConstructor | ||
public class StateIbgeResponseDTO { | ||
|
||
private Long id; | ||
|
||
@JsonProperty("sigla") | ||
private String abbreviation; | ||
|
||
@JsonProperty("nome") | ||
private String name; | ||
} |
19 changes: 19 additions & 0 deletions
19
pointtils/src/main/java/com/pointtils/pointtils/src/application/dto/StateResponseDTO.java
This file contains hidden or 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,19 @@ | ||
package com.pointtils.pointtils.src.application.dto; | ||
|
||
import lombok.AllArgsConstructor; | ||
import lombok.Getter; | ||
import lombok.NoArgsConstructor; | ||
import lombok.Setter; | ||
|
||
import java.util.List; | ||
|
||
@Getter | ||
@Setter | ||
@NoArgsConstructor | ||
@AllArgsConstructor | ||
public class StateResponseDTO { | ||
|
||
private boolean success; | ||
private String message; | ||
private List<StateDataDTO> data; | ||
} |
2 changes: 0 additions & 2 deletions
2
pointtils/src/main/java/com/pointtils/pointtils/src/application/services/AuthService.java
This file contains hidden or 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
22 changes: 22 additions & 0 deletions
22
pointtils/src/main/java/com/pointtils/pointtils/src/application/services/StateService.java
This file contains hidden or 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,22 @@ | ||
package com.pointtils.pointtils.src.application.services; | ||
|
||
import com.pointtils.pointtils.src.application.clients.IbgeClient; | ||
import com.pointtils.pointtils.src.application.dto.StateResponseDTO; | ||
import lombok.RequiredArgsConstructor; | ||
import org.springframework.stereotype.Service; | ||
|
||
@Service | ||
@RequiredArgsConstructor | ||
public class StateService { | ||
|
||
private final IbgeClient ibgeClient; | ||
|
||
public StateResponseDTO getAllStates() { | ||
return new StateResponseDTO(true, "UFs encontradas com sucesso", ibgeClient.getStateList()); | ||
} | ||
|
||
public StateResponseDTO getCitiesByState(String state) { | ||
return new StateResponseDTO(true, "Municípios encontrados com sucesso", | ||
ibgeClient.getCityListByState(state)); | ||
} | ||
} |
This file contains hidden or 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
7 changes: 7 additions & 0 deletions
7
.../main/java/com/pointtils/pointtils/src/core/domain/exceptions/ClientTimeoutException.java
This file contains hidden or 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,7 @@ | ||
package com.pointtils.pointtils.src.core.domain.exceptions; | ||
|
||
public class ClientTimeoutException extends RuntimeException { | ||
public ClientTimeoutException(String message) { | ||
super(message); | ||
} | ||
} |
This file contains hidden or 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
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.