Skip to content
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
8 changes: 4 additions & 4 deletions API Teste/bancopessoas.sql
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
create database pessoas;
use pessoas;
create table pessoas(
create table pessoa(
id integer auto_increment primary key,
nome_completo varchar(200) not null,
cpf varchar(11) not null,
data_nascimento varchar(8) not null,
email varchar(50) not null,
data_nascimento date not null,
email varchar(50) not null
);

insert into pessoas values (null, 'Elizangela', '999999999', '04/07/1990', 'elizangela@gmail.com');
insert into pessoa values (null, 'Elizangela', '999999999', '1990-07-04', 'elizangela@gmail.com');

select * from pessoas;

3 changes: 1 addition & 2 deletions API Teste/projeto/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,7 @@
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
<artifactId>spring-boot-starter-security</artifactId>
</dependency>
</dependencies>

Expand Down
10 changes: 0 additions & 10 deletions API Teste/projeto/src/main/java/DAO/IPessoas.java

This file was deleted.

6 changes: 0 additions & 6 deletions API Teste/projeto/src/main/java/Service/Pessoa.java

This file was deleted.

48 changes: 0 additions & 48 deletions API Teste/projeto/src/main/java/Service/PessoaService.java

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,8 @@
@SpringBootApplication(exclude = {SecurityAutoConfiguration.class})
public class ProjetoApplication {

public static void main(String[] args) {

SpringApplication.run(ProjetoApplication.class, args);
}
public static void main(String[] args) {
SpringApplication.run(ProjetoApplication.class, args);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
package br.com.criandoapi.projeto.controller;

import br.com.criandoapi.projeto.service.PessoaService;
import br.com.criandoapi.projeto.model.Pessoa;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Controller
@RequestMapping("/pessoas")
public class PessoasController {

@Autowired
private PessoaService pessoaService;

public PessoasController() {
}

@GetMapping
public ResponseEntity<List<Pessoa>> listaPessoas() {
return ResponseEntity.status(200).body(pessoaService.listarPessoa());
}

@PostMapping
public ResponseEntity<Pessoa> criarPessoa(@RequestBody Pessoa pessoa) {
return ResponseEntity.status(201).body(pessoaService.criarPessoa(pessoa));
}

@PutMapping
public ResponseEntity<Pessoa> editarPessoa(@RequestBody Pessoa pessoa) {
return ResponseEntity.status(200).body(pessoaService.editarPessoa(pessoa));
}

//Não existia um endpoint no controller para exclusão de pessoas.
@DeleteMapping
public ResponseEntity<Boolean> excluirPessoa(@RequestBody Pessoa pessoa) {
return ResponseEntity.status(200).body(pessoaService.excluirPessoa(pessoa.getId()));
}

@PostMapping("/cpf")
public ResponseEntity<Object> validarCPF(@RequestBody Pessoa pessoa) {
Boolean valid = pessoaService.validarCPF(pessoa);
if (!valid) {
return ResponseEntity.status(HttpStatus.UNAUTHORIZED).build();
}
return ResponseEntity.status(200).build();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package br.com.criandoapi.projeto.dao;

import br.com.criandoapi.projeto.model.Pessoa;
import org.springframework.data.jpa.repository.JpaRepository;

public interface PessoaDao extends JpaRepository<Pessoa, Integer> {

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
package br.com.criandoapi.projeto.model;

import com.sun.istack.NotNull;

import javax.persistence.*;
import java.time.LocalDate;

@Entity
@Table(name = "pessoa")
public class Pessoa {

@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "id")
private Integer ID;

@NotNull
@Column(name = "nome_completo", length = 200, nullable = false)
private String nome;

@Column(name = "cpf", length = 11, nullable = false)
private String cpf;

@Column(name = "data_nascimento", length = 8, nullable = false)
private LocalDate dataNascimento;
@Column(name = "email", length = 50, nullable = false)
private String email;

public Integer getId() {
return ID;
}

public void setID(Integer ID) {
this.ID = ID;
}

public String getNome() {
return nome;
}

public void setNome(String nome) {
this.nome = nome;
}

public void setCpf(String cpf) {
this.cpf = cpf;
}

public String getCpf() {
return cpf;
}

public void setDataNascimento(LocalDate dataNascimento) {
this.dataNascimento = dataNascimento;
}

public LocalDate getDataNascimento() {
return dataNascimento;
}

public void setEmail(String email) {
this.email = email;
}

public String getEmail() {
return email;
}
}


Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
package br.com.criandoapi.projeto.service;

import br.com.criandoapi.projeto.dao.PessoaDao;
import br.com.criandoapi.projeto.model.Pessoa;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class PessoaService {
@Autowired
private PessoaDao dao;
private PasswordEncoder passwordEncoder;

public PessoaService() {
this.passwordEncoder = new BCryptPasswordEncoder();
}

public List<Pessoa> listarPessoa() {
List<Pessoa> lista = dao.findAll();
return lista;
}

public Pessoa criarPessoa(Pessoa pessoa) {
String encoder = this.passwordEncoder.encode(pessoa.getCpf());
pessoa.setCpf(encoder);
Pessoa pessoaNovo = dao.save(pessoa);
return pessoaNovo;
}

public Pessoa editarPessoa(Pessoa pessoa) {
Pessoa pessoaNovo = dao.save(pessoa);
return pessoaNovo;
}

public Boolean excluirPessoa(Integer id) {
dao.deleteById(id);
return true;
}

public Boolean validarCPF(Pessoa pessoa) {
String cpf = dao.getReferenceById(pessoa.getId()).getCpf();
boolean valid = false;
passwordEncoder.matches(pessoa.getCpf(), cpf);
return valid;
}


}


Loading