package es.uv.garcosda.web;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;

import es.uv.garcosda.domain.Message;
import es.uv.garcosda.repositories.MessageRepository;



@Controller
public class HomeController {

	@Autowired
	private MessageRepository mr;
	
	@GetMapping()
	public String index(Model model){
		return "index";
	}
	
	@GetMapping("/messages")
	public String messages(Model model){
		model.addAttribute("msgs", mr.findAll());		
		return "messages";
	}
	
	@GetMapping("/admin")
	public String admin(Model model){
		return "admin";
	}
	
	@GetMapping("/messages/remove/{id}")
	public String removeMessage(Model model, @PathVariable("id") Integer id){
		mr.deleteById(id);
		return "redirect:/messages";
	}
	
	@PostMapping("/messages")
	public String saveMessage(Message message){
		mr.save(message);
		return "redirect:/messages";
	}
	
	
}
