-
Notifications
You must be signed in to change notification settings - Fork 59
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
168 additions
and
0 deletions.
There are no files selected for viewing
27 changes: 27 additions & 0 deletions
27
src/main/java/br/com/zupacademy/fabiano/mercadolivre/controller/CategoriaController.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
43 changes: 43 additions & 0 deletions
43
src/main/java/br/com/zupacademy/fabiano/mercadolivre/dto/CategoriaDto.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
39 changes: 39 additions & 0 deletions
39
src/main/java/br/com/zupacademy/fabiano/mercadolivre/modelo/Categoria.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
7 changes: 7 additions & 0 deletions
7
src/main/java/br/com/zupacademy/fabiano/mercadolivre/repository/CategoriaRepository.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> { | ||
} |
18 changes: 18 additions & 0 deletions
18
src/main/java/br/com/zupacademy/fabiano/mercadolivre/validator/ChecaExistencia.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
|
34 changes: 34 additions & 0 deletions
34
src/main/java/br/com/zupacademy/fabiano/mercadolivre/validator/ChecaExistenciaValidator.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
} |