package org.artgasmator.controller;

import org.artgasmator.model.AlbumGenre;
import org.artgasmator.service.AlbumGenreService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

@Controller
public class AlbumGenreController {
	
	@Autowired
	private AlbumGenreService albumGenreService;
	
	@RequestMapping(value="/genres/view")
	public String loadGenres(Model model) {
		model.addAttribute("genres", albumGenreService.loadGenres());
		return "viewGenre";
	}
	
	@RequestMapping(value="/genres/create", method=RequestMethod.GET)
	public String createGenre(Model model) {
		model.addAttribute("albumGenre", new AlbumGenre());
		return "createGenre";
	}
	
	@RequestMapping(value="/genres/create", method=RequestMethod.POST)
	public String createGenre(@ModelAttribute("albumGenre") AlbumGenre albumGenre, 
				BindingResult bindingResult) {
		
		albumGenreService.createGenre(albumGenre);
		
		return "redirect:/genres/view";
	}
	
	@RequestMapping(value="/genres/delete/{genreId}")
	public String deleteGenre(@PathVariable("genreId") Integer genreId, Model model) {
		
		albumGenreService.deleteGenre(genreId);
		
		return "redirect:/genres/view";
	}
}
