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

Armazendo as informações de cadastro #12

Merged
merged 1 commit into from
Jan 28, 2024
Merged
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
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ Veja mais informações das lições aprendidas na [Wiki](https://github.com/jal
* [Construindo DTOs com funções Java](https://github.com/jalussa-santos/formacao-spring-boot/issues/3)
* [POST - Enviando dados para a API](https://github.com/jalussa-santos/formacao-spring-boot/issues/6)
* [Configuração banco de dados](https://github.com/jalussa-santos/formacao-spring-boot/issues/8)
* [Armanezando dados com Spring Data JPA](https://github.com/jalussa-santos/formacao-spring-boot/issues/11)

>### Tecnologias:

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
package com.formacao.springbootapi.controller;

import com.formacao.springbootapi.model.medico.CadastroMedicoDto;
import com.formacao.springbootapi.model.medico.Medico;
import com.formacao.springbootapi.model.medico.MedicoRepository;
import jakarta.validation.Valid;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
Expand All @@ -10,8 +14,11 @@
@RequestMapping("medicos")
public class MedicoController {

@Autowired
private MedicoRepository repository;

@PostMapping
public void cadastrar(@RequestBody CadastroMedicoDto dados) {
System.out.println(dados);
public void cadastrar(@RequestBody @Valid CadastroMedicoDto dados) {
repository.save(new Medico(dados));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package com.formacao.springbootapi.model.endereco;

import jakarta.persistence.Embeddable;
import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.NoArgsConstructor;

@Embeddable
@Getter
@NoArgsConstructor
@AllArgsConstructor
public class Endereco {

public Endereco(EnderecoDto endereco) {
this.logradouro = endereco.logradouro();
this.bairro = endereco.bairro();
this.cep = endereco.cep();
this.uf = endereco.uf();
this.cidade = endereco.cidade();
this.numero = endereco.numero();
this.complemento = endereco.complemento();
}

private String logradouro;
private String bairro;
private String cep;
private String numero;
private String complemento;
private String cidade;
private String uf;
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,21 @@
package com.formacao.springbootapi.model.endereco;

public record EnderecoDto(String logradouro, String bairro, String cep, String cidade, String uf, String complemento, String numero) {
import jakarta.validation.constraints.NotBlank;
import jakarta.validation.constraints.Pattern;

public record EnderecoDto(
@NotBlank
String logradouro,
@NotBlank
String bairro,
@NotBlank
@Pattern(regexp = "\\d{8}")
String cep,
@NotBlank
String cidade,
@NotBlank
String uf,
String complemento,
String numero) {

}
Original file line number Diff line number Diff line change
@@ -1,6 +1,27 @@
package com.formacao.springbootapi.model.medico;

import com.formacao.springbootapi.model.endereco.EnderecoDto;
import jakarta.validation.Valid;
import jakarta.validation.constraints.Email;
import jakarta.validation.constraints.NotBlank;
import jakarta.validation.constraints.NotNull;
import jakarta.validation.constraints.Pattern;

public record CadastroMedicoDto(
@NotBlank
String nome,
@NotBlank
@Email
String email,
@NotBlank
String telefone,
@NotBlank
@Pattern(regexp = "\\d{4,6}")
String crm,
@NotNull
Especialidade especialidade,
@NotNull
@Valid
EnderecoDto endereco) {

public record CadastroMedicoDto(String nome, String email, String crm, Especialidade especialidade, EnderecoDto endereco) {
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
package com.formacao.springbootapi.model.medico;

import com.formacao.springbootapi.model.endereco.Endereco;
import jakarta.persistence.Embedded;
import jakarta.persistence.Entity;
import jakarta.persistence.EnumType;
import jakarta.persistence.Enumerated;
import jakarta.persistence.GeneratedValue;
import jakarta.persistence.GenerationType;
import jakarta.persistence.Id;
import jakarta.persistence.Table;
import lombok.AllArgsConstructor;
import lombok.EqualsAndHashCode;
import lombok.Getter;
import lombok.NoArgsConstructor;

@Table(name = "medicos")
@Entity(name = "Medico")
@Getter
@NoArgsConstructor
@AllArgsConstructor
@EqualsAndHashCode(of = "id")
public class Medico {

@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
private String nome;
private String email;
private String crm;
private String telefone;
@Enumerated(EnumType.STRING)
private Especialidade especialidade;
@Embedded
private Endereco endereco;
public Medico(CadastroMedicoDto medico) {
this.nome = medico.nome();
this.email = medico.email();
this.crm = medico.crm();
this.telefone = medico.telefone();
this.especialidade = medico.especialidade();
this.endereco = new Endereco(medico.endereco());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package com.formacao.springbootapi.model.medico;

import org.springframework.data.jpa.repository.JpaRepository;

public interface MedicoRepository extends JpaRepository<Medico, Long> {

}
6 changes: 3 additions & 3 deletions spring-boot-api/src/main/resources/application.properties
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
spring.datasource.url=jdbc:mysql://localhost:3306/formacao_spring_boot
spring.datasource.username=
spring.datasource.password=
spring.datasource.url=jdbc:mysql://localhost:3306/{nome_base_dados}
spring.datasource.username={usuario}
spring.datasource.password={senha}
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
create table medicos(

id bigint not null auto_increment,
nome varchar(100) not null,
email varchar(100) not null unique,
crm varchar(6) not null unique,
especialidade varchar(100) not null,
logradouro varchar(100) not null,
bairro varchar(100) not null,
cep varchar(9) not null,
complemento varchar(100),
numero varchar(20),
uf char(2) not null,
cidade varchar(100) not null,

primary key(id)

);
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
alter table medicos add telefone varchar(20) not null;