NewsController.java 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. package es.uv.garcosda.controllers;
  2. import java.text.SimpleDateFormat;
  3. import java.util.Date;
  4. import org.springframework.beans.factory.annotation.Autowired;
  5. import org.springframework.web.bind.annotation.CrossOrigin;
  6. import org.springframework.web.bind.annotation.DeleteMapping;
  7. import org.springframework.web.bind.annotation.GetMapping;
  8. import org.springframework.web.bind.annotation.PathVariable;
  9. import org.springframework.web.bind.annotation.PostMapping;
  10. import org.springframework.web.bind.annotation.PutMapping;
  11. import org.springframework.web.bind.annotation.RequestBody;
  12. import org.springframework.web.bind.annotation.RequestMapping;
  13. import org.springframework.web.bind.annotation.RestController;
  14. import es.uv.garcosda.domain.Document;
  15. import es.uv.garcosda.services.NewsService;
  16. import reactor.core.publisher.Mono;
  17. @RestController
  18. @RequestMapping("/api/v1/news")
  19. @CrossOrigin
  20. public class NewsController {
  21. @Autowired
  22. private NewsService ns;
  23. @GetMapping("{id}")
  24. public Mono<Document> getOne(@PathVariable("id") Integer id) {
  25. return this.ns.findById(id);
  26. }
  27. @PostMapping()
  28. public Mono<Document> insert(@RequestBody Document n){
  29. n.setDateAdd(new SimpleDateFormat("dd-MM-yyyy").format(new Date()));
  30. return this.ns.insert(n);
  31. }
  32. @PutMapping()
  33. public Mono<Document> update(@RequestBody Document n){
  34. return this.ns.update(n);
  35. }
  36. @DeleteMapping("{id}")
  37. public Mono<Integer> delete(@PathVariable("id") Integer id){
  38. return this.ns.deleteById(id).then(Mono.just(id));
  39. }
  40. }