Skip to content
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

Atualizado os endpoints, entidades e a camada de serviço #6

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,19 @@
# Explorando Padrões de Projetos na Prática com Java

Repositório com as implementações dos padrões de projeto explorados no Lab "Explorando Padrões de Projetos na Prática com Java". Especificamente, este projeto explorou alguns padrões usando o Spring Framework, são eles:

- Singleton
- Strategy/Repository
- Facade

# Layout

![Swagger](https://user-images.githubusercontent.com/105529988/215643638-895255cf-9ddd-4312-8f75-d043091eab63.png)

# Passo a passo

Após fazer o clone da aplicação, basta executa-la em uma IDE e digitar no navegodor: 127.0.0.1:8080/swagger-ui/index.html ou clicar aqui 👉🏽 [Swagger](http://127.0.0.1:8080/swagger-ui/index.html) e então, realizar a consulta fazendo um post passando o cep e depois um get para consultar.

# Autor

Edielson Assis
23 changes: 14 additions & 9 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,21 +6,21 @@
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.5.4</version>
<version>2.7.8</version>
<relativePath /> <!-- lookup parent from repository -->
</parent>
<groupId>digitalinnovation.one</groupId>
<artifactId>lab-padroes-projeto-spring</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>lab-padroes-projeto-spring</name>
<description>Exemplos de Padrões de Projeto em Spring</description>

<properties>
<java.version>11</java.version>
<openapi.version>1.5.10</openapi.version>
<spring-cloud.version>2020.0.3</spring-cloud.version>
<java.version>17</java.version>
<openapi.version>1.6.14</openapi.version>
<spring-cloud.version>2021.0.3</spring-cloud.version>
</properties>

<dependencies>
<!-- Spring Data JPA (adicionado via Spring Initializr) -->
<dependency>
Expand All @@ -43,20 +43,25 @@
<artifactId>springdoc-openapi-ui</artifactId>
<version>${openapi.version}</version>
</dependency>

<!-- H2 Database (adicionado via Spring Initializr) -->
<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
<scope>runtime</scope>
</dependency>
<!-- Lombok (adicionado via Spring Initializr) -->
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<scope>annotationProcessor</scope>
</dependency>

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
</dependencies>

<dependencyManagement>
<dependencies>
Expand All @@ -79,4 +84,4 @@
</plugins>
</build>

</project>
</project>
4 changes: 3 additions & 1 deletion src/main/java/one/digitalinnovation/gof/Application.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,10 @@
* - Spring Web
* - H2 Database
* - OpenFeign
* - Lombok
*
* @author falvojr
* @author edielson-assis
*/
@EnableFeignClients
@SpringBootApplication
Expand All @@ -22,4 +24,4 @@ public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,48 +11,49 @@
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import one.digitalinnovation.gof.model.Cliente;
import one.digitalinnovation.gof.service.ClienteService;
import one.digitalinnovation.gof.model.Client;
import one.digitalinnovation.gof.service.ClientService;

/**
* Esse {@link RestController} representa nossa <b>Facade</b>, pois abstrai toda
* a complexidade de integrações (Banco de Dados H2 e API do ViaCEP) em uma
* interface simples e coesa (API REST).
*
* @author falvojr
* @author edielson-assis
*/
@RestController
@RequestMapping("clientes")
public class ClienteRestController {
@RequestMapping(value = "clients")
public class ClientRestController {

@Autowired
private ClienteService clienteService;
private ClientService clientService;

@GetMapping
public ResponseEntity<Iterable<Cliente>> buscarTodos() {
return ResponseEntity.ok(clienteService.buscarTodos());
public ResponseEntity<Iterable<Client>> findAll() {
return ResponseEntity.ok(clientService.findAll());
}

@GetMapping("/{id}")
public ResponseEntity<Cliente> buscarPorId(@PathVariable Long id) {
return ResponseEntity.ok(clienteService.buscarPorId(id));
public ResponseEntity<Client> findById(@PathVariable Long id) {
return ResponseEntity.ok(clientService.findById(id));
}

@PostMapping
public ResponseEntity<Cliente> inserir(@RequestBody Cliente cliente) {
clienteService.inserir(cliente);
return ResponseEntity.ok(cliente);
public ResponseEntity<Client> insert(@RequestBody Client client) {
clientService.insert(client);
return ResponseEntity.ok(client);
}

@PutMapping("/{id}")
public ResponseEntity<Cliente> atualizar(@PathVariable Long id, @RequestBody Cliente cliente) {
clienteService.atualizar(id, cliente);
return ResponseEntity.ok(cliente);
public ResponseEntity<Client> update(@PathVariable Long id, @RequestBody Client client) {
clientService.update(id, client);
return ResponseEntity.ok(client);
}

@DeleteMapping("/{id}")
public ResponseEntity<Void> deletar(@PathVariable Long id) {
clienteService.deletar(id);
public ResponseEntity<Void> delete(@PathVariable Long id) {
clientService.delete(id);
return ResponseEntity.ok().build();
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package one.digitalinnovation.gof.controller.exceptions;

import java.time.Instant;

import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;

import javax.servlet.http.HttpServletRequest;
import one.digitalinnovation.gof.service.exceptions.DatabaseException;
import one.digitalinnovation.gof.service.exceptions.ResourceNotFoundException;

@ControllerAdvice
public class ResourceExceptionHandler {

@ExceptionHandler(ResourceNotFoundException.class)
public ResponseEntity<StandardError> resourceNotFound(ResourceNotFoundException e, HttpServletRequest request) {
String error = "Resource not found";
HttpStatus status = HttpStatus.NOT_FOUND;
StandardError err = new StandardError(Instant.now(), status.value(), error, e.getMessage(), request.getRequestURI());
return ResponseEntity.status(status).body(err);
}

@ExceptionHandler(DatabaseException.class)
public ResponseEntity<StandardError> database(DatabaseException e, HttpServletRequest request) {
String error = "Database error";
HttpStatus status = HttpStatus.BAD_REQUEST;
StandardError err = new StandardError(Instant.now(), status.value(), error, e.getMessage(), request.getRequestURI());
return ResponseEntity.status(status).body(err);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package one.digitalinnovation.gof.controller.exceptions;

import java.io.Serializable;
import java.time.Instant;

import com.fasterxml.jackson.annotation.JsonFormat;

import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;

@Data
@Builder
@NoArgsConstructor
@AllArgsConstructor
public class StandardError implements Serializable {

@JsonFormat(shape = JsonFormat.Shape.STRING, pattern = "yyyy-MM-dd'T'HH:mm:ss'Z'", timezone = "GMT")
private Instant timestamp;
private Integer status;
private String error;
private String message;
private String path;
}
33 changes: 33 additions & 0 deletions src/main/java/one/digitalinnovation/gof/model/Address.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package one.digitalinnovation.gof.model;

import javax.persistence.Entity;
import javax.persistence.Id;

import lombok.Data;

/**
* Os atributos desse modelo foram gerados automaticamente pelo site
* jsonschema2pojo.org. Para isso, usamos o JSON de retorno da API do ViaCEP.
*
* @see <a href="https://www.jsonschema2pojo.org">jsonschema2pojo.org</a>
* @see <a href="https://viacep.com.br">ViaCEP</a>
*
* @author falvojr
* @author edielson-assis
*/
@Data
@Entity
public class Address {

@Id
private String cep;
private String logradouro;
private String complemento;
private String bairro;
private String localidade;
private String uf;
private String ibge;
private String gia;
private String ddd;
private String siafi;
}
27 changes: 27 additions & 0 deletions src/main/java/one/digitalinnovation/gof/model/Client.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package one.digitalinnovation.gof.model;

import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.ManyToOne;

import lombok.Data;

/**
* Classe {@link Client} representa um cliente no banco de dados.
*
* @author falvojr
* @author edielson-assis
*/
@Data
@Entity
public class Client {

@Id
@GeneratedValue(strategy = javax.persistence.GenerationType.AUTO)
private Long id;
private String name;

@ManyToOne
private Address address;
}
43 changes: 0 additions & 43 deletions src/main/java/one/digitalinnovation/gof/model/Cliente.java

This file was deleted.

This file was deleted.

Loading