package com.meujornal.controllers;

import static java.lang.String.format;

import javax.inject.Inject;
import javax.validation.Valid;

import br.com.caelum.vraptor.Controller;
import br.com.caelum.vraptor.Delete;
import br.com.caelum.vraptor.Get;
import br.com.caelum.vraptor.Post;
import br.com.caelum.vraptor.Result;
import br.com.caelum.vraptor.validator.Validator;

import com.meujornal.infrastructure.persistence.dao.FeedsDAO;
import com.meujornal.models.noticias.Feed;

/**
 * Controller responsável pelas URIs administrativas do sistema.
 * 
 */
@Controller
public class AdminController {

	@Inject
	private Result result;
	@Inject
	private Validator validator;
	@Inject
	private FeedsDAO feedsDAO;

	@Get("/admin")
	public void index() {
		result.include("feeds", feedsDAO.buscarTodos());
	}

	@Get("/admin/novo-feed")
	public void formulario() {
		result.include("categorias",
				feedsDAO.buscarTodasAsCategoriasExistentes());
	}

	@Post("/admin/feeds")
	public void criarFeed(@Valid Feed feed) {
		validator.onErrorRedirectTo(this).formulario();
		feedsDAO.salvar(feed);
		result.include(
				"mensagem",
				format("O Feed RSS %s foi criado com sucesso!",
						feed.getTitulo())).redirectTo(this).index();
	}

	@Get("/admin/editar-feed/{id}")
	public void editarFeed(Long id) {
		Feed feed = feedsDAO.buscarPorId(id);

		if (feed == null) {
			result.notFound();
		} else {
			result.include("feed", feed)
					.include("categorias",
							feedsDAO.buscarTodasAsCategoriasExistentes())
					.of(this).formulario();
		}
	}

	@Post("/admin/feeds/{id}")
	public void editarFeed(Long id, @Valid Feed feed) {
		if (!feedsDAO.existeFeedComId(id)) {
			result.notFound();
		} else {
			validator.onErrorRedirectTo(this).formulario();
			feedsDAO.atualizar(feed);
			result.include(
					"mensagem",
					format("O Feed RSS %s foi atualizado com sucesso!",
							feed.getTitulo())).redirectTo(this).index();
		}
	}

	@Get("/admin/feeds/{id}")
	public void visualizar(Long id) {
		Feed feed = feedsDAO.buscarPorId(id);
		if (feed == null) {
			result.notFound();
		} else {
			result.include("feed", feed);
		}
	}

	@Delete("/admin/feeds/{id}")
	public void removerFeed(Long id) {
		Feed feed = feedsDAO.buscarPorId(id);

		if (feed == null) {
			result.notFound();
		} else {
			feedsDAO.remover(feed);
			result.include(
					"mensagem",
					format("O feed %s foi removido com sucesso!",
							feed.getTitulo())).redirectTo(this).index();
		}
	}

}

@@ -701,16 +701,42 @@
 dsDAO;%0A%0A
+%09// Indexa todos os feeds%0A
 %09@Get(%22/
@@ -743,16 +743,16 @@
 admin%22)%0A
-
 %09public 
@@ -817,24 +817,113 @@
 dos());%0A%09%7D%0A%0A
+%09// Gera um novo formul%C3%A1rio para cadastro de feed, considerando as categorias existentes%0A
 %09@Get(%22/admi
@@ -1046,24 +1046,70 @@
 tes());%0A%09%7D%0A%0A
+%09// Recebe um objeto Feed e cria um novo feed%0A
 %09@Post(%22/adm
@@ -1374,24 +1374,61 @@
 ndex();%0A%09%7D%0A%0A
+%09// Recebe o id de um feed e o edita%0A
 %09@Get(%22/admi
@@ -1729,16 +1729,59 @@
 %09%09%7D%0A%09%7D%0A%0A
+%09// Recebe o id e um objeto Feed e o edita%0A
 %09@Post(%22
@@ -2158,16 +2158,74 @@
 %09%09%7D%0A%09%7D%0A%0A
+%09// Recebe o id de um feed e o assinala para visualiza%C3%A7%C3%A3o%0A
 %09@Get(%22/
@@ -2414,16 +2414,54 @@
 %09%09%7D%0A%09%7D%0A%0A
+%09// Recebe o id de um feed e o remove%0A
 %09@Delete

Update AdminController.java