Last active
November 26, 2017 19:05
-
-
Save vndmtrx/158e8359373ac72832dd8c57ad9b0081 to your computer and use it in GitHub Desktop.
Post saindo do forno... #Java9 #SpringBoot
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
package com.vndmtrx.springboot.tarefas.modelos; | |
import javax.validation.constraints.Size; | |
import java.util.Date; | |
import javax.validation.constraints.NotNull; | |
import org.springframework.data.annotation.Id; | |
import org.springframework.data.mongodb.core.index.Indexed; | |
import org.springframework.data.mongodb.core.mapping.Document; | |
import com.fasterxml.jackson.annotation.JsonIgnoreProperties; | |
@Document(collection="tarefas") | |
@JsonIgnoreProperties(value = {"criadoEm"}, allowGetters = true) | |
public class Tarefa { | |
@Id | |
private String id; | |
@NotNull | |
@Size(max=100) | |
@Indexed(unique=true) | |
private String titulo; | |
private Boolean completado; | |
private Date criadoEm = new Date(); | |
//getters/setters removidos para clareza de código | |
@Override | |
public String toString() { | |
return String.format("Tarefa [id=%s, título='%s', completada='%s'", id, titulo, completado); | |
} | |
} |
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
package com.vndmtrx.springboot.tarefas.controllers; | |
import java.util.List; | |
import java.util.Optional; | |
import javax.validation.Valid; | |
import org.springframework.beans.factory.annotation.Autowired; | |
import org.springframework.data.domain.Sort; | |
import org.springframework.http.ResponseEntity; | |
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.PathVariable; | |
import org.springframework.web.bind.annotation.PostMapping; | |
import org.springframework.web.bind.annotation.PutMapping; | |
import org.springframework.web.bind.annotation.RequestBody; | |
import org.springframework.web.bind.annotation.RequestMapping; | |
import org.springframework.web.bind.annotation.RestController; | |
import com.vndmtrx.springboot.tarefas.modelos.Tarefa; | |
import com.vndmtrx.springboot.tarefas.repos.TarefaRepo; | |
@RestController | |
@RequestMapping("/api/tarefas") | |
@CrossOrigin("*") | |
public class TarefaController { | |
@Autowired | |
TarefaRepo repo; | |
@GetMapping | |
public List<Tarefa> getTodasTarefas() { | |
//Sort ordem = new Sort(Sort.Direction.DESC, "criadoEm"); | |
//return repo.findAll(ordem); | |
return repo.findAllByOrderByCriadoEmDesc(); | |
} | |
@PostMapping | |
public Tarefa criaTarefa(@Valid @RequestBody Tarefa tarefa) { | |
tarefa.setCompletado(false); | |
return repo.save(tarefa); | |
} | |
@GetMapping("/{id}") | |
public ResponseEntity<Tarefa> getTarefaById(@PathVariable("id") String id) { | |
return Optional | |
.ofNullable(repo.findOne(id)) | |
.map(tarefa -> ResponseEntity.ok().body(tarefa)) | |
.orElseGet(() -> ResponseEntity.notFound().build()); | |
} | |
@PutMapping("/{id}") | |
public ResponseEntity<Tarefa> updateTarefa(@PathVariable("id") String id, @Valid @RequestBody Tarefa tarefaReq) { | |
return Optional | |
.ofNullable(repo.findOne(id)) | |
.map(tarefaBanco -> { | |
tarefaBanco.setTitulo(tarefaReq.getTitulo()); | |
tarefaBanco.setCompletado(tarefaReq.getCompletado()); | |
repo.save(tarefaBanco); | |
return ResponseEntity.ok().body(tarefaBanco); | |
}) | |
.orElseGet(() -> ResponseEntity.notFound().build()); | |
} | |
@DeleteMapping("/{id}") | |
public ResponseEntity<Tarefa> deleteTarefa(@PathVariable("id") String id) { | |
return ResponseEntity.ok().build(); | |
} | |
} |
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
package com.vndmtrx.springboot.tarefas.repos; | |
import java.util.List; | |
import org.springframework.data.mongodb.repository.MongoRepository; | |
import com.vndmtrx.springboot.tarefas.modelos.Tarefa; | |
public interface TarefaRepo extends MongoRepository<Tarefa, String> { | |
public List<Tarefa> findAllByOrderByCriadoEmDesc(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment