diff --git a/API Teste/bancopessoas.sql b/API Teste/bancopessoas.sql index 4201a41..da6172b 100644 --- a/API Teste/bancopessoas.sql +++ b/API Teste/bancopessoas.sql @@ -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; diff --git a/API Teste/projeto/pom.xml b/API Teste/projeto/pom.xml index 2287401..87800bc 100644 --- a/API Teste/projeto/pom.xml +++ b/API Teste/projeto/pom.xml @@ -39,8 +39,7 @@ org.springframework.boot - spring-boot-starter-test - test + spring-boot-starter-security diff --git a/API Teste/projeto/src/main/java/DAO/IPessoas.java b/API Teste/projeto/src/main/java/DAO/IPessoas.java deleted file mode 100644 index 0ea56a9..0000000 --- a/API Teste/projeto/src/main/java/DAO/IPessoas.java +++ /dev/null @@ -1,10 +0,0 @@ -package DAO; - -import model.Pessoas; -import org.springframework.data.jpa.repository.JpaRepository; -import org.springframework.data.repository.CrudRepository; - -public interface IPessoas extends JpaRepository { - - -} diff --git a/API Teste/projeto/src/main/java/Service/Pessoa.java b/API Teste/projeto/src/main/java/Service/Pessoa.java deleted file mode 100644 index 4b22683..0000000 --- a/API Teste/projeto/src/main/java/Service/Pessoa.java +++ /dev/null @@ -1,6 +0,0 @@ -package Service; - -public class Pessoa { - public Object getIcpf() { - } -} diff --git a/API Teste/projeto/src/main/java/Service/PessoaService.java b/API Teste/projeto/src/main/java/Service/PessoaService.java deleted file mode 100644 index 59a194c..0000000 --- a/API Teste/projeto/src/main/java/Service/PessoaService.java +++ /dev/null @@ -1,48 +0,0 @@ -package Service; - -import DAO.IPessoas; -import com.sun.xml.bind.v2.model.core.ID; -import org.springframework.data.jpa.repository.JpaRepository; - -@Service -public final class PessoaService { - private IPessoas; - private PasswordEncoder passwordEncoder; - - public PessoaService(IPessoas){ - this.dao = dao; - this.passwordEncoder = new BCryptPasswordEncorder(); - } - - public static List listarPessoa(){ - List lista = dao.findAll(); - return lista; - } - - public Pessoa criarPessoa( Pessoa pessoa){ - String encorder = this.passwordEncoder.encode(pessoa.getIcpf()); - pessoa.setIcpf(encoder); - Pessoa pessoaNovo = dao.save(pessoa); - return pessoaNovo; - } - - public Pessoa editarPessoa( Pessoa pessoa){ - Pessoa pessoaNovo = dao.save(pessoa); - return pessoaNovo; - } - - public Boolean excluirPessoa(ID id, JpaRepository dao){ - dao.deleteById(id); - return true; - } - - public Boolean validarCPF(Pessoa pessoa, JpaRepository dao){ - String cpf = dao.getById(pessoa.getIid()).getIcpf(); - boolean valid = false; passwordEncoder.matches(pessoa.getIcpf(), cpf); - return valid; - } - - -} - - diff --git a/API Teste/projeto/src/main/java/br/com/criandoapi/projeto/PessoasController.java b/API Teste/projeto/src/main/java/br/com/criandoapi/projeto/PessoasController.java deleted file mode 100644 index 1db77e4..0000000 --- a/API Teste/projeto/src/main/java/br/com/criandoapi/projeto/PessoasController.java +++ /dev/null @@ -1,61 +0,0 @@ -package br.com.criandoapi.projeto; - -import Service.Pessoa; -import Service.PessoaService; -import com.sun.xml.bind.v2.model.core.ID; -import model.Pessoas; -import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.data.jpa.repository.JpaRepository; -import org.springframework.http.HttpStatus; -import org.springframework.http.ResponseEntity; -import org.springframework.web.bind.annotation.*; - -import java.util.List; - - - -@RestController -@CrossOrigin("*") -@RequestMapping("/pessoas") -public class PessoasController { - - @Autowired - - private JpaRepository dao; - private PessoaService pessoaService; - - public PessoasController(){ - this.pessoaService = null; - - - } - - @GetMapping - public ResponseEntity> listaPessoas() { - return ResponseEntity.status(200).body(PessoaService.listarPessoa(Pessoa)); - - } - - @PostMapping - public ResponseEntity criarPessoa(@RequestBody Pessoa pessoa, Pessoa Pessoa) { - return ResponseEntity.status(201).body(pessoaService.criarPessoa(Pessoa)); - - } - - @PutMapping - public ResponseEntity editarPessoa(@RequestBody Pessoa pessoa, Pessoa Pessoa) { - return ResponseEntity.status(200).body(pessoaService.editarPessoa(Pessoa); - } - - @PostMapping ("/cpf") - public ResponseEntity validarCPF(@RequestBody Pessoa pessoa){ - Boolean valid = pessoaService.validarCPF(pessoa, dao); - if (!valid) { - return ResponseEntity.status(HttpStatus.UNAUTHORIZED).build(); - } - return ResponseEntity.status(200).build(); - } - - private class T { - } -} diff --git a/API Teste/projeto/src/main/java/br/com/criandoapi/projeto/ProjetoApplication.java b/API Teste/projeto/src/main/java/br/com/criandoapi/projeto/ProjetoApplication.java index b171f08..56b9fe2 100644 --- a/API Teste/projeto/src/main/java/br/com/criandoapi/projeto/ProjetoApplication.java +++ b/API Teste/projeto/src/main/java/br/com/criandoapi/projeto/ProjetoApplication.java @@ -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); + } } diff --git a/API Teste/projeto/src/main/java/br/com/criandoapi/projeto/controller/PessoasController.java b/API Teste/projeto/src/main/java/br/com/criandoapi/projeto/controller/PessoasController.java new file mode 100644 index 0000000..818fdde --- /dev/null +++ b/API Teste/projeto/src/main/java/br/com/criandoapi/projeto/controller/PessoasController.java @@ -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> listaPessoas() { + return ResponseEntity.status(200).body(pessoaService.listarPessoa()); + } + + @PostMapping + public ResponseEntity criarPessoa(@RequestBody Pessoa pessoa) { + return ResponseEntity.status(201).body(pessoaService.criarPessoa(pessoa)); + } + + @PutMapping + public ResponseEntity 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 excluirPessoa(@RequestBody Pessoa pessoa) { + return ResponseEntity.status(200).body(pessoaService.excluirPessoa(pessoa.getId())); + } + + @PostMapping("/cpf") + public ResponseEntity validarCPF(@RequestBody Pessoa pessoa) { + Boolean valid = pessoaService.validarCPF(pessoa); + if (!valid) { + return ResponseEntity.status(HttpStatus.UNAUTHORIZED).build(); + } + return ResponseEntity.status(200).build(); + } +} diff --git a/API Teste/projeto/src/main/java/br/com/criandoapi/projeto/dao/PessoaDao.java b/API Teste/projeto/src/main/java/br/com/criandoapi/projeto/dao/PessoaDao.java new file mode 100644 index 0000000..3867a7b --- /dev/null +++ b/API Teste/projeto/src/main/java/br/com/criandoapi/projeto/dao/PessoaDao.java @@ -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 { + +} diff --git a/API Teste/projeto/src/main/java/br/com/criandoapi/projeto/model/Pessoa.java b/API Teste/projeto/src/main/java/br/com/criandoapi/projeto/model/Pessoa.java new file mode 100644 index 0000000..eff4ef6 --- /dev/null +++ b/API Teste/projeto/src/main/java/br/com/criandoapi/projeto/model/Pessoa.java @@ -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; + } +} + + diff --git a/API Teste/projeto/src/main/java/br/com/criandoapi/projeto/service/PessoaService.java b/API Teste/projeto/src/main/java/br/com/criandoapi/projeto/service/PessoaService.java new file mode 100644 index 0000000..379f162 --- /dev/null +++ b/API Teste/projeto/src/main/java/br/com/criandoapi/projeto/service/PessoaService.java @@ -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 listarPessoa() { + List 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; + } + + +} + + diff --git a/API Teste/projeto/src/main/java/model/Pessoas.java b/API Teste/projeto/src/main/java/model/Pessoas.java deleted file mode 100644 index f1cd0b3..0000000 --- a/API Teste/projeto/src/main/java/model/Pessoas.java +++ /dev/null @@ -1,72 +0,0 @@ -package model; - -import com.sun.istack.NotNull; - -import javax.persistence.*; - -@Entity -@Table(name = "pessoas") -public class Pessoas { - - @Id - @GeneratedValue(strategy = GenerationType.IDENTITY) - @Column(name = "id") - - private Integer ID; - - @NotNull - - - @Column(name = "nome", length = 200, nullable = false) - private String nome; - - @Column(name = "cpf", length = 11 , nullable = false) - private String cpf; - - @Column(name = "date", length = 8, nullable = false) - private String date; - @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 setDate(String date) { - this.date = date; - } - - public String getDate() { - return date; - } - - public void setEmail(String email) { - this.email = email; - } - - public String getEmail() { - return email; - } - } - -