Skip to content

Commit

Permalink
Processo route created
Browse files Browse the repository at this point in the history
  • Loading branch information
ailtonbsj committed Aug 31, 2022
1 parent 7da52e8 commit 173c5f2
Show file tree
Hide file tree
Showing 3 changed files with 125 additions and 0 deletions.
47 changes: 47 additions & 0 deletions src/main/java/ailtonbsj/sauteweb/sauteapi/model/Processo.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
package ailtonbsj.sauteweb.sauteapi.model;

import java.time.LocalDateTime;

import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.ManyToOne;
import javax.persistence.PrePersist;

import com.fasterxml.jackson.annotation.JsonFormat;

import lombok.Getter;
import lombok.Setter;

@Entity
@Getter
@Setter
public class Processo {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
Long id;

@Column(nullable = false)
String numero;

@ManyToOne(cascade = CascadeType.DETACH, optional = false)
Instituicao instituicao;

@Column(nullable = false)
@JsonFormat(pattern = "yyyy-MM-dd'T'HH:mm")
private LocalDateTime createdAt;

@Column(nullable = false)
@JsonFormat(pattern = "yyyy-MM-dd'T'HH:mm")
private LocalDateTime updatedAt;

@PrePersist
public void beforeSave() {
LocalDateTime now = LocalDateTime.now();
setCreatedAt(now);
setUpdatedAt(now);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package ailtonbsj.sauteweb.sauteapi.repository;

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

import ailtonbsj.sauteweb.sauteapi.model.Processo;

public interface ProcessoRepository extends JpaRepository<Processo, Long> {
public Iterable<Processo> findByNumeroContaining(String numero);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
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.Processo;
import ailtonbsj.sauteweb.sauteapi.repository.ProcessoRepository;
import ailtonbsj.sauteweb.sauteapi.utils.Utils;

@RestController
@RequestMapping("/api/processo")
@CrossOrigin("http://localhost:4200")
public class ProcessoController {

@Autowired
ProcessoRepository rep;

@PostMapping
public Long save(@RequestBody Processo processo) {
return rep.save(processo).getId();
}

@GetMapping
public Iterable<Processo> findAll(@RequestParam Optional<String> q) {
if (q.isEmpty()) {
return rep.findAll();
} else {
System.out.println(q.get());
return rep.findByNumeroContaining(q.get());
}
}

@GetMapping("{id}")
public Processo findById(@PathVariable Long id) {
return rep.findById(id).orElseThrow(
() -> new ResponseStatusException(HttpStatus.NOT_FOUND));
}

@PatchMapping
public Long update(@RequestBody Processo processo) {
Processo ent = rep.findById(processo.getId())
.orElseThrow(() -> new ResponseStatusException(HttpStatus.NOT_FOUND));
processo.setId(null);
BeanUtils.copyProperties(processo, ent, Utils.getNullPropertyNames(processo));
ent.setUpdatedAt(LocalDateTime.now());
return rep.save(ent).getId();
}

@DeleteMapping("{id}")
public void deleteById(@PathVariable Long id) {
rep.deleteById(id);
}

}

0 comments on commit 173c5f2

Please sign in to comment.