Skip to content

Commit

Permalink
Finished Instituicao route
Browse files Browse the repository at this point in the history
  • Loading branch information
ailtonbsj committed Aug 30, 2022
1 parent 0ef245b commit 7da52e8
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 24 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,5 @@
import ailtonbsj.sauteweb.sauteapi.model.Instituicao;

public interface InstituicaoRepository extends JpaRepository<Instituicao, Long> {

Iterable<Instituicao> findByInstituicaoContainingIgnoreCase(String instituicao);
}
Original file line number Diff line number Diff line change
@@ -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")
Expand All @@ -30,29 +36,31 @@ public Long save(@RequestBody Instituicao instituicao) {
}

@GetMapping
public Iterable<Instituicao> findAll() {
return rep.findAll();
public Iterable<Instituicao> findAll(@RequestParam Optional<String> 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);
}
}
2 changes: 1 addition & 1 deletion src/main/resources/application.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ spring:
jpa:
show-sql: true
hibernate:
ddl-auto: create
ddl-auto: update
properties:
hibernate:
dialect: org.hibernate.dialect.MySQL8Dialect

0 comments on commit 7da52e8

Please sign in to comment.