view Agendas/trunk/src/Agendas.Web/Controllers/EventoController.cs @ 27:29b8a8893ba3

Mejoras menores en UI
author nelo@MTEySS.neluz.int
date Tue, 15 Mar 2011 09:14:52 -0300
parents 71b02443450a
children 475be11edf56
line wrap: on
line source

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

namespace 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();

                try
                {
                    agenda.Publicar(model.Titulo, model.Ponente, model.Fecha);

					return RedirectToAction("Index");
                }
                catch (ValidationException ex)
                {
                    ModelState.AddModelError("error", ex.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();

				try
				{
					agenda.ModificarEvento(new Guid(model.Id), model.Titulo, model.Ponente, model.Fecha);

					return RedirectToAction("Index");
				}
				catch (ValidationException ex)
				{
					ModelState.AddModelError("error", ex.ToString());
				}
			}
			return View(model);
		}
    }
}