-
Notifications
You must be signed in to change notification settings - Fork 3
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
3 changed files
with
125 additions
and
0 deletions.
There are no files selected for viewing
47 changes: 47 additions & 0 deletions
47
src/main/java/ailtonbsj/sauteweb/sauteapi/model/Processo.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,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); | ||
} | ||
} |
9 changes: 9 additions & 0 deletions
9
src/main/java/ailtonbsj/sauteweb/sauteapi/repository/ProcessoRepository.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,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); | ||
} |
69 changes: 69 additions & 0 deletions
69
src/main/java/ailtonbsj/sauteweb/sauteapi/rest/ProcessoController.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,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); | ||
} | ||
|
||
} |