view Agendas/trunk/src/Agendas.Web/Controllers/EventoController.cs @ 55:39f5258ebdcf

Agregando referencias con NuGet
author nelopauselli
date Mon, 16 May 2011 20:10:45 -0300
parents 3059a5f8930f
children c7264bfc4b71
line wrap: on
line source

using System;
using System.Linq;
using System.Web.Mvc;
using AltNetHispano.Agendas.Factories;
using AltNetHispano.Agendas.Web.Models;

namespace AltNetHispano.Agendas.Web.Controllers
{
    public class EventoController : Controller
    {
        public ActionResult Index()
        {
			var agenda = AgendaFactory.GetAgenda();

			var model = new EventoIndexModel
			{
				ProximosEventos = from e in agenda.GetEventosPublicados()
								  select new EventoDto { Id = e.Id.ToString(), Titulo = e.Titulo }
			};
			
            return View(model);
        }

		[Authorize]
		public ActionResult New()
        {
            return View();
        }

        [HttpPost]
		[Authorize]
		public ActionResult New(EventoNewModel model)
        {
            if (ModelState.IsValid)
            {
                var agenda = AgendaFactory.GetAgenda();

                var r = agenda.Publicar(model.Titulo, model.Ponente, model.Fecha);
				if (r.Succeful)
					return RedirectToAction("Index");
                ModelState.AddModelError("error", r.ToString());
            }
            return View(model);
        }

		[Authorize]
		public ActionResult Edit(string id)
		{
			var agenda = AgendaFactory.GetAgenda();
			var evento = agenda.GetEvento(new Guid(id));
			if (evento!=null)
			{
				var model = new EventoEditModel
				            	{
				            		Id = id,
				            		Titulo = evento.Titulo,
				            		Ponente = evento.Ponente != null ? evento.Ponente.Nombre : string.Empty,
				            		Fecha = evento.Fecha
				            	};
				return View(model);
			}
			ModelState.AddModelError("error","No se encontró el evento que quiere modificar");
			return View();
		}

		[HttpPost]
		[Authorize]
		public ActionResult Edit(EventoEditModel model)
		{
			if (ModelState.IsValid)
			{
				var agenda = AgendaFactory.GetAgenda();

				var r =agenda.ModificarEvento(new Guid(model.Id), model.Titulo, model.Ponente, model.Fecha);
				if (r.Succeful)
					return RedirectToAction("Index");
				ModelState.AddModelError("error", r.ToString());
			}
			return View(model);
		}
    }
}