From 7da52e81a75488700c18d24f919573a478d5ca24 Mon Sep 17 00:00:00 2001 From: ailtonbsj Date: Tue, 30 Aug 2022 01:10:19 -0300 Subject: [PATCH] Finished Instituicao route --- .../sauteweb/sauteapi/model/Instituicao.java | 1 - .../repository/InstituicaoRepository.java | 2 +- .../sauteapi/rest/InstituicaoController.java | 50 +++++++++++-------- src/main/resources/application.yml | 2 +- 4 files changed, 31 insertions(+), 24 deletions(-) diff --git a/src/main/java/ailtonbsj/sauteweb/sauteapi/model/Instituicao.java b/src/main/java/ailtonbsj/sauteweb/sauteapi/model/Instituicao.java index a0e140d..b7801e4 100644 --- a/src/main/java/ailtonbsj/sauteweb/sauteapi/model/Instituicao.java +++ b/src/main/java/ailtonbsj/sauteweb/sauteapi/model/Instituicao.java @@ -10,7 +10,6 @@ import javax.persistence.GeneratedValue; import javax.persistence.GenerationType; import javax.persistence.Id; -import javax.persistence.JoinColumn; import javax.persistence.ManyToOne; import javax.persistence.PrePersist; diff --git a/src/main/java/ailtonbsj/sauteweb/sauteapi/repository/InstituicaoRepository.java b/src/main/java/ailtonbsj/sauteweb/sauteapi/repository/InstituicaoRepository.java index c59ee9e..7777a10 100644 --- a/src/main/java/ailtonbsj/sauteweb/sauteapi/repository/InstituicaoRepository.java +++ b/src/main/java/ailtonbsj/sauteweb/sauteapi/repository/InstituicaoRepository.java @@ -5,5 +5,5 @@ import ailtonbsj.sauteweb.sauteapi.model.Instituicao; public interface InstituicaoRepository extends JpaRepository { - + Iterable findByInstituicaoContainingIgnoreCase(String instituicao); } diff --git a/src/main/java/ailtonbsj/sauteweb/sauteapi/rest/InstituicaoController.java b/src/main/java/ailtonbsj/sauteweb/sauteapi/rest/InstituicaoController.java index 45ff038..b8874a3 100644 --- a/src/main/java/ailtonbsj/sauteweb/sauteapi/rest/InstituicaoController.java +++ b/src/main/java/ailtonbsj/sauteweb/sauteapi/rest/InstituicaoController.java @@ -1,20 +1,26 @@ package ailtonbsj.sauteweb.sauteapi.rest; +import java.time.LocalDateTime; +import java.util.Optional; + +import org.springframework.beans.BeanUtils; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.http.HttpStatus; import org.springframework.web.bind.annotation.CrossOrigin; +import org.springframework.web.bind.annotation.DeleteMapping; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.PatchMapping; +import org.springframework.web.bind.annotation.PathVariable; 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.RequestParam; import org.springframework.web.bind.annotation.RestController; import org.springframework.web.server.ResponseStatusException; import ailtonbsj.sauteweb.sauteapi.model.Instituicao; -import ailtonbsj.sauteweb.sauteapi.model.NivelEscolar; import ailtonbsj.sauteweb.sauteapi.repository.InstituicaoRepository; -import ailtonbsj.sauteweb.sauteapi.repository.NivelEscolarRepository; +import ailtonbsj.sauteweb.sauteapi.utils.Utils; @RestController @RequestMapping("/api/instituicao") @@ -30,29 +36,31 @@ public Long save(@RequestBody Instituicao instituicao) { } @GetMapping - public Iterable findAll() { - return rep.findAll(); + public Iterable findAll(@RequestParam Optional q) { + if (q.isEmpty()) + return rep.findAll(); + else + return rep.findByInstituicaoContainingIgnoreCase(q.get()); + } + + @GetMapping("{id}") + public Instituicao findById(@PathVariable Long id) { + return rep.findById(id).orElseThrow( + () -> new ResponseStatusException(HttpStatus.NOT_FOUND)); } @PatchMapping - public Instituicao update(@RequestBody Instituicao instituicao) { - rep.findById(instituicao.getId()).orElseThrow( + public Long update(@RequestBody Instituicao instituicao) { + Instituicao ent = rep.findById(instituicao.getId()).orElseThrow( () -> new ResponseStatusException(HttpStatus.NOT_FOUND)); - return rep.save(instituicao); - - // Professor ent = rep.findById(professor.getId()).orElseThrow( - // () -> new ResponseStatusException(HttpStatus.NOT_FOUND)); - // professor.setId(null); - // BeanUtils.copyProperties(professor, ent, - // Utils.getNullPropertyNames(professor)); - // ent.setUpdatedAt(LocalDateTime.now()); - // return rep.save(ent).getId(); + instituicao.setId(null); + BeanUtils.copyProperties(instituicao, ent, Utils.getNullPropertyNames(instituicao)); + ent.setUpdatedAt(LocalDateTime.now()); + return rep.save(ent).getId(); } - // @PostMapping - // public Instituicao save(@RequestBody Instituicao instituicao) { - // Instituicao i = rep.save(instituicao); - // i.setNivelEscolar(repNivel.findById(i.getNivelEscolar().getId()).orElseThrow()); - // return i; - // } + @DeleteMapping("{id}") + public void deleteById(@PathVariable Long id) { + rep.deleteById(id); + } } diff --git a/src/main/resources/application.yml b/src/main/resources/application.yml index e96f490..5f1d8cc 100644 --- a/src/main/resources/application.yml +++ b/src/main/resources/application.yml @@ -6,7 +6,7 @@ spring: jpa: show-sql: true hibernate: - ddl-auto: create + ddl-auto: update properties: hibernate: dialect: org.hibernate.dialect.MySQL8Dialect