package org.sistema.springmvc.controllers;

import java.util.List;

import javax.validation.Valid;

import org.sistema.springmvc.dao.GenericDAO;
import org.sistema.springmvc.models.Series;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

/**
 * Controller for TV series.
 * 
 * @author Eugenia Pérez Martínez
 *
 */
@RestController
@RequestMapping("/series")
public class SeriesController {

	@Autowired
	private GenericDAO<Series> seriesDAO;


	/**
	 * Returns the list of TV series. handles /series
	 * 
	 * @return the list of all series
	 */
	@RequestMapping(method = RequestMethod.GET)
	public List<Series> getAll() {
		return seriesDAO.selectAll(Series.class);
	}

	/**
	 * Returns a TV show by its ID. handles /series/id
	 * 
	 * @param id
	 * @return the series by ID
	 */
	@RequestMapping(method = RequestMethod.GET, value = { "/{id}" })
	public Series getById(@PathVariable(value = "id") Integer id) {
		return seriesDAO.selectById(id, Series.class);
	}

	/**
	 * Creates a new TV show. handles /series by POST
	 * 
	 * @return true if it is created successfully
	 */
	@RequestMapping(method = RequestMethod.POST)
	public void insert(@RequestBody @Valid Series series) {
		seriesDAO.insert(series);
	}

	/**
	 * Updates a TV show by its ID. handles /series/{id} by PUT
	 * 
	 * @param id
	 * @param series
	 * @return true if it is updated successfully
	 */
	@RequestMapping(value = "/{id}", method = RequestMethod.PUT)
	public void update(@PathVariable Integer id,
			@RequestBody @Valid Series series) {
		series.setId(id);
		seriesDAO.update(series);
	}

	/**
	 * Deletes a TV show by its ID. handles /series/{id} by DELETE
	 * 
	 * @param id
	 */
	@RequestMapping(value = "/{id}", method = RequestMethod.DELETE)
	public void delete(@PathVariable Integer id) {
		seriesDAO.delete(seriesDAO.selectById(id, Series.class));
	}

	/**
	 * Get the DAO
	 * @return the Generic DAO of Series
	 */
	public GenericDAO<Series> getSeriesDAO() {
		return seriesDAO;
	}

	/**
	 * Set the DAO
	 * @param seriesDAO
	 */
	public void setSeriesDAO(GenericDAO<Series> seriesDAO) {
		this.seriesDAO = seriesDAO;
	}
	
	

}
