Skip to content

Commit

Permalink
Autorizacao route finished
Browse files Browse the repository at this point in the history
  • Loading branch information
ailtonbsj committed Sep 1, 2022
1 parent 173c5f2 commit 7d82dca
Show file tree
Hide file tree
Showing 4 changed files with 133 additions and 0 deletions.
25 changes: 25 additions & 0 deletions src/main/java/ailtonbsj/sauteweb/sauteapi/model/Autorizacao.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package ailtonbsj.sauteweb.sauteapi.model;

import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.ManyToOne;

import lombok.Getter;
import lombok.Setter;

@Entity
@Getter
@Setter
public class Autorizacao extends EntityBase {
String numero;

@ManyToOne(optional = false, cascade = CascadeType.DETACH)
Professor professor;

@ManyToOne(optional = false, cascade = CascadeType.DETACH)
Processo processo;

@Column(nullable = false)
String referendum;
}
39 changes: 39 additions & 0 deletions src/main/java/ailtonbsj/sauteweb/sauteapi/model/EntityBase.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package ailtonbsj.sauteweb.sauteapi.model;

import java.time.LocalDateTime;

import javax.persistence.Column;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.MappedSuperclass;
import javax.persistence.PrePersist;

import com.fasterxml.jackson.annotation.JsonFormat;

import lombok.Getter;
import lombok.Setter;

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

@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,8 @@
package ailtonbsj.sauteweb.sauteapi.repository;

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

import ailtonbsj.sauteweb.sauteapi.model.Autorizacao;

public interface AutorizacaoRepository extends JpaRepository<Autorizacao, Long> {
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
package ailtonbsj.sauteweb.sauteapi.rest;

import java.time.LocalDateTime;

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.RestController;
import org.springframework.web.server.ResponseStatusException;

import ailtonbsj.sauteweb.sauteapi.model.Autorizacao;
import ailtonbsj.sauteweb.sauteapi.repository.AutorizacaoRepository;
import ailtonbsj.sauteweb.sauteapi.utils.Utils;

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

@Autowired
AutorizacaoRepository rep;

@PostMapping
public Long save(@RequestBody Autorizacao autorizacao) {
return rep.save(autorizacao).getId();
}

@GetMapping
public Iterable<Autorizacao> findAll() {
return rep.findAll();
}

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

@PatchMapping
public Long update(@RequestBody Autorizacao autorizacao) {
Autorizacao ent = this.findById(autorizacao.getId());
autorizacao.setId(null);
BeanUtils.copyProperties(autorizacao, ent, Utils.getNullPropertyNames(autorizacao));
ent.setUpdatedAt(LocalDateTime.now());
return rep.save(ent).getId();
}

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

}

0 comments on commit 7d82dca

Please sign in to comment.