Skip to content

Commit

Permalink
feat: cadastro categoria
Browse files Browse the repository at this point in the history
  • Loading branch information
fabiomsrs committed Jun 30, 2021
1 parent e5e6e4a commit 64bb4c4
Show file tree
Hide file tree
Showing 6 changed files with 168 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package br.com.zupacademy.fabiano.mercadolivre.controller;

import br.com.zupacademy.fabiano.mercadolivre.dto.CategoriaDto;
import br.com.zupacademy.fabiano.mercadolivre.modelo.Categoria;
import br.com.zupacademy.fabiano.mercadolivre.repository.CategoriaRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;

@RestController
@RequestMapping("/categorias")
public class CategoriaController {
@Autowired
CategoriaRepository repository;

@PostMapping
public ResponseEntity<Categoria> cadastrar(@RequestBody @Valid CategoriaDto dto){
Categoria categoria = dto.converter(repository);
repository.save(categoria);
return ResponseEntity.ok(categoria);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package br.com.zupacademy.fabiano.mercadolivre.dto;

import br.com.zupacademy.fabiano.mercadolivre.modelo.Categoria;
import br.com.zupacademy.fabiano.mercadolivre.repository.CategoriaRepository;
import br.com.zupacademy.fabiano.mercadolivre.validator.ChecaExistencia;
import br.com.zupacademy.fabiano.mercadolivre.validator.ValorUnico;

import javax.validation.constraints.NotEmpty;
import javax.validation.constraints.NotNull;

public class CategoriaDto {
@NotNull
@NotEmpty
@ValorUnico(field = "nome", instanceClass = Categoria.class)
private String nome;
@ChecaExistencia(identityField = "id", instanceClass = Categoria.class)
private Long categoriaMae;

public CategoriaDto() {
}

public CategoriaDto(@NotNull @NotEmpty String nome,
Long categoriaMae) {
this.nome = nome;
this.categoriaMae = categoriaMae;
}

public String getNome() {
return nome;
}

public Long getCategoriaMae() {
return categoriaMae;
}

public Categoria converter(CategoriaRepository repository) {
Categoria categoria = null;
if(this.categoriaMae != null){
categoria = repository.findById(this.categoriaMae).get();
}
return new Categoria(this.nome, categoria);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package br.com.zupacademy.fabiano.mercadolivre.modelo;

import javax.persistence.*;
import javax.validation.constraints.NotEmpty;
import javax.validation.constraints.NotNull;

@Entity
public class Categoria {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
@NotNull
@NotEmpty
@Column(nullable = false, unique = true)
private String nome;
@ManyToOne
private Categoria categoriaMae;

public Categoria() {
}

public Categoria(@NotNull @NotEmpty String nome,
Categoria categoriaMae) {
this.nome = nome;
this.categoriaMae = categoriaMae;
}

public Long getId() {
return id;
}

public String getNome() {
return nome;
}

public Categoria getCategoriaMae() {
return categoriaMae;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package br.com.zupacademy.fabiano.mercadolivre.repository;

import br.com.zupacademy.fabiano.mercadolivre.modelo.Categoria;
import org.springframework.data.jpa.repository.JpaRepository;

public interface CategoriaRepository extends JpaRepository<Categoria, Long> {
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package br.com.zupacademy.fabiano.mercadolivre.validator;

import javax.validation.Constraint;
import javax.validation.Payload;
import java.lang.annotation.*;

@Documented
@Constraint(validatedBy = ChecaExistenciaValidator.class)
@Target({ ElementType.FIELD })
@Retention(RetentionPolicy.RUNTIME)
public @interface ChecaExistencia {
String message() default "Instancia não encontrada";
Class<?>[] groups() default {};
Class<? extends Payload>[] payload() default {};
String identityField();
Class instanceClass();
}

Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package br.com.zupacademy.fabiano.mercadolivre.validator;

import javax.persistence.EntityManager;
import javax.validation.ConstraintValidator;
import javax.validation.ConstraintValidatorContext;

public class ChecaExistenciaValidator implements ConstraintValidator<ChecaExistencia, Object> {
private EntityManager em;

private Class<?> object;
private String identityField;

public ChecaExistenciaValidator(EntityManager em) {
this.em = em;
}

@Override
public void initialize(ChecaExistencia constraintAnnotation) {
this.object = constraintAnnotation.instanceClass();
this.identityField = constraintAnnotation.identityField();
ConstraintValidator.super.initialize(constraintAnnotation);
}

@Override
public boolean isValid(Object value, ConstraintValidatorContext context) {
if (value == null){
return true;
}
return !this.em.createQuery("select 1 from "+this.object.getName()+" where "+ this.identityField + "=:field")
.setParameter("field",value)
.getResultList()
.isEmpty();
}
}

0 comments on commit 64bb4c4

Please sign in to comment.