view Agendas/trunk/src/Agendas.Web/Controllers/AgendaController.cs @ 15:08b9e96132a5

Persistimos los eventos de la agenda
author nelo@MTEySS.neluz.int
date Mon, 14 Mar 2011 00:14:09 -0300
parents ed6d842abf42
children 74eb4577d447
line wrap: on
line source

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

namespace Agendas.Web.Controllers
{
    public class AgendaController : Controller
    {
        public ActionResult Index()
        {
            var model = GetIndexModel();
            return View(model);
        }

        private static AgendaIndexModel GetIndexModel()
        {
            var agenda = AgendaFactory.GetAgenda();

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

        public ActionResult New()
        {
            return View();
        }

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

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

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

		public ActionResult Edit(string id)
		{
			var agenda = AgendaFactory.GetAgenda();
			var evento = agenda.GetEventoPublicado(new Guid(id));
			if (evento!=null)
			{
				var model = new AgendaEditModel {Id = id, Titulo = evento.Titulo, Ponente = evento.Ponente, Fecha = evento.Fecha};
				return View(model);
			}
			ModelState.AddModelError("error","No se encontró el evento que quiere modificar");
			return View();
		}

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

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

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