package es.uv.garcosda.controllers;

import org.springframework.beans.factory.annotation.Autowired;
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 es.uv.garcosda.domain.Document;
import es.uv.garcosda.services.NewsService;
import reactor.core.publisher.Mono;

@RestController
@RequestMapping("/api/v1/news")
@CrossOrigin
public class NewsController {
	
	@Autowired
	private NewsService ns;
	
	@GetMapping("{id}")
    public Mono<Document> getOne(@PathVariable("id") Integer id) {
        return this.ns.findById(id);
    }
	
	@PostMapping()
	public Mono<Document> insert(@RequestBody Document n){
		return this.ns.insert(n);	
	}
	
	@PutMapping()
	public Mono<Document> update(@RequestBody Document n){
		return this.ns.update(n);
	}
	
	@DeleteMapping("{id}")
	public Mono<Void> delete(@PathVariable("id") Integer id){
		return this.ns.deleteById(id);
	}
	
}
