view Agendas/trunk/src/Agendas.Web/Controllers/EventoController.cs @ 284:79942e030118

#123: Patrocinadores de las vans: Se verifica que para publicar una Van, debe tener al menos un Patrocinador.
author juanjose.montesdeocaarbos
date Wed, 28 Dec 2011 09:35:27 -0300
parents c61954d24c8c
children
line wrap: on
line source

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web.Mvc;
using AltNetHispano.Agendas.Domain;
using AltNetHispano.Agendas.Factories;
using AltNetHispano.Agendas.Twitter;
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.GetEventosActivos()
		                                          orderby e.FechaInicio
		                                          select new EventoDto
		                                                     {
		                                                         Id = e.Id.ToString(),
		                                                         Titulo = e.Titulo,
		                                                         Fecha =
		                                                             e.FechaInicio.HasValue
		                                                                 ? e.FechaInicio.Value.ToShortDateString() + " " +
                                                                           e.FechaInicio.Value.ToShortTimeString()
		                                                                 : string.Empty,
		                                                         Duracion =
		                                                             e.FechaInicio.HasValue && e.FechaTermino.HasValue
		                                                                 ? e.FechaTermino.Value.Subtract(e.FechaInicio.Value).
		                                                                       ToString("c")
		                                                                 : string.Empty,
		                                                         Estado = e.Estado.Descripcion,
		                                                         PuedeAgendar = e.Estado.PuedePromover(Accion.Agendar),
		                                                         PuedeModificar = e.Estado.PuedePromover(Accion.Modificar),
		                                                         PuedeConfirmar = e.Estado.PuedePromover(Accion.Confirmar),
		                                                         PuedePublicar = e.Estado.PuedePromover(Accion.Publicar),
		                                                         PuedeCancelar = e.Estado.PuedePromover(Accion.Cancelar),
		                                                         PuedeDescartar = e.Estado.PuedePromover(Accion.Descartar),
		                                                         PuedeReAgendar = e.Estado.PuedePromover(Accion.ReAgendar),
		                                                         PuedeReProponer = e.Estado.PuedePromover(Accion.ReProponer)
		                                                     }
		                    };
			return View(model);
		}

		[CustomAuthorize(Roles = Roles.Administrador)]
		public ActionResult Nuevo()
		{
            var model = new EventoNewModel { Fecha = DateTime.Now, Duracion = new TimeSpan(2, 0, 0), Hora = new TimeSpan(18, 0, 0) };
		    return View("Defaulteditor", model);
		}

		[HttpPost]
		[CustomAuthorize(Roles = Roles.Administrador)]
		public ActionResult Nuevo(EventoNewModel model)
		{
		    return
		        GenericAction(
					(agenda, m) => agenda.Agendar(m.Titulo, model.Ponentes, GenerarFechaInicio(m.Fecha, m.Hora),
		                                          GenerarFechaTermino(m.Fecha, m.Hora, m.Duracion), m.UrlInvitacion,
		                                          (TipoEvento) m.TipoEvento),
		            m => View("Defaulteditor", m),
		            model);
		}

    	[CustomAuthorize(Roles = Roles.Administrador)]
		public ActionResult Confirmar(string id)
		{
			var agenda = AgendaFactory.GetAgenda();
			var evento = agenda.GetEvento(new Guid(id));

			var model = new EventoConfirmModel
			{
				Id = id,
				Titulo = evento.Titulo,
				Recordatorios = new Recordatorios(evento)
			};
			return View("Defaulteditor", model);
		}

		[HttpPost]
		[CustomAuthorize(Roles = Roles.Administrador)]
		public ActionResult Confirmar(EventoConfirmModel model)
		{
			return
				GenericAction(
					(agenda, m) => agenda.Confirmar(new Guid(m.Id)),
					m => View("Defaulteditor", m),
					model);
		}

		[CustomAuthorize(Roles = Roles.Administrador)]
		public ActionResult Publicar(string id)
		{
			var agenda = AgendaFactory.GetAgenda();
			var evento = agenda.GetEvento(new Guid(id));

			var model = new EventoPublicarModel
			{
				Id = id,
				Titulo = evento.Titulo,
				NroOrden = 0, //TODO: Obtener el último y sumarle uno.
				UrlWiki = evento.UrlWiki,
				DuracionReal = new TimeSpan(0, 0, 0)
			};
			return View("Defaulteditor", model);
		}

		[HttpPost]
		[CustomAuthorize(Roles = Roles.Administrador)]
		public ActionResult Publicar(EventoPublicarModel model)
		{
			var a = AgendaFactory.GetAgenda();
			a.IndicarPatrocinadores(new Guid(model.Id), model.Patrocinadores);

			return
					GenericAction(
						(agenda, m) => agenda.Publicar(new Guid(m.Id), m.NroOrden, m.UrlWiki, m.DuracionReal),
						m => View("Defaulteditor", m),
						model);
		}

		[CustomAuthorize(Roles = Roles.Administrador)]
		public ActionResult Modificar(string id)
		{
			var agenda = AgendaFactory.GetAgenda();
			var evento = agenda.GetEvento(new Guid(id));

			var model = new EventoEditModel
			{
				Id = id,
				Titulo = evento.Titulo,
				Ponentes = evento.Ponentes.Select(p=>p.Id),
				Fecha = evento.FechaInicio,
                Hora = evento.FechaInicio != null ? evento.FechaInicio.Value.TimeOfDay : (TimeSpan?)null,
                Duracion = evento.FechaInicio!=null && evento.FechaTermino!=null ? evento.FechaTermino.Value.Subtract(evento.FechaInicio.Value) : (TimeSpan?) null,
				UrlInvitacion = evento.UrlInvitacion
			};
            return View("Defaulteditor", model);
		}
		
		[HttpPost]
		[CustomAuthorize(Roles = Roles.Administrador)]
		public ActionResult Modificar(EventoEditModel model)
		{
		    return
		        GenericAction(
		            (agenda, m) =>
		            agenda.ModificarEvento(new Guid(m.Id), m.Titulo, m.Ponentes, GenerarFechaInicio(m.Fecha, m.Hora),
		                                   GenerarFechaTermino(m.Fecha, m.Hora, m.Duracion), m.UrlInvitacion),
		            m => View("Defaulteditor", m),
		            model);
		}

		[CustomAuthorize(Roles = Roles.Administrador)]
		public ActionResult Agendar(string id)
		{
			var agenda = AgendaFactory.GetAgenda();
			var evento = agenda.GetEvento(new Guid(id));

			var model = new EventoAgendarModel
			{
				Id = id,
				Titulo = evento.Titulo,
				Ponentes = evento.Ponentes.Select(p=>p.Id),
				Fecha = evento.FechaInicio,
                Hora = evento.FechaInicio != null ? evento.FechaInicio.Value.TimeOfDay : (TimeSpan?)null,
                Duracion = evento.FechaInicio != null && evento.FechaTermino != null ? evento.FechaTermino.Value.Subtract(evento.FechaInicio.Value) : (TimeSpan?)null,
				UrlInvitacion = evento.UrlInvitacion
			};
            return View("Defaulteditor", model);
		}

		[HttpPost]
		[CustomAuthorize(Roles = Roles.Administrador)]
		public ActionResult Agendar(EventoAgendarModel model)
		{
		    return
		        GenericAction(
		            (agenda, m) =>
		            agenda.ModificarEvento(new Guid(m.Id), m.Titulo, m.Ponentes, GenerarFechaInicio(m.Fecha, m.Hora),
		                                   GenerarFechaTermino(m.Fecha, m.Hora, m.Duracion), m.UrlInvitacion),
		            m => View("Defaulteditor", m),
		            model);
		}

		[CustomAuthorize(Roles = Roles.Administrador)]
		public ActionResult Proponer()
		{
			var model = new PropuestaNewModel();
            return View("Defaulteditor", model);
		}

		[HttpPost]
		[CustomAuthorize(Roles = Roles.Administrador)]
		public ActionResult Proponer(PropuestaNewModel model)
		{
			return
				GenericAction(
					(agenda, m) => agenda.Proponer(m.Titulo, m.Ponentes, m.UrlInvitacion, (TipoEvento)m.TipoEvento),
					m => View("Defaulteditor", m),
					model);
		}

		[CustomAuthorize(Roles = Roles.Administrador)]
	    public ActionResult Cancelar(string id)
		{
			return
				GenericAction(
					(agenda, m) => agenda.Cancelar(m),
					m => View("Index", m),
					new Guid(id));
		}

    	[CustomAuthorize(Roles = Roles.Administrador)]
	    public ActionResult Descartar(string id)
    	{
    		return
    			GenericAction(
    				(agenda, m) => agenda.Descartar(m),
    				m => View("Index", m),
    				new Guid(id));
    	}

    	[CustomAuthorize(Roles = Roles.Administrador)]
	    public ActionResult ReAgendar(string id)
    	{
    		return
    			GenericAction(
    				(agenda, m) => agenda.ReAgendar(m),
    				m => View("Index", m),
    				new Guid(id));
    	}

    	[CustomAuthorize(Roles = Roles.Administrador)]
	    public ActionResult ReProponer(string id)
    	{
			return
    			GenericAction(
    				(agenda, m) => agenda.ReProponer(m),
    				m => View("Index", m),
    				new Guid(id));
    	}

		private ActionResult GenericAction<TModel>(Func<Agenda, TModel, EventoResultado> action, Func<TModel, ActionResult> actionresultOnFail, TModel model)
		{
			if (ModelState.IsValid)
			{
				var agenda = AgendaFactory.GetAgenda();

				var r = action.Invoke(agenda, model);
				if (r.Succeful)
				{
					this.AddNotification(r.Message);
					foreach (var log in r.Warnings)
						this.AddWarning(log.WarningMessage);
					return RedirectToAction("Index");
				}
				ModelState.AddModelError(string.Empty, r.Message);
			}
			return actionresultOnFail.Invoke(model);
		}

        private DateTime? GenerarFechaInicio(DateTime? fecha, TimeSpan? hora)
        {
            return fecha.HasValue && hora.HasValue
                       ? new DateTime(fecha.Value.Year, fecha.Value.Month, fecha.Value.Day, hora.Value.Hours,
                                      hora.Value.Minutes, hora.Value.Seconds, DateTimeKind.Utc)
                       : (DateTime?) null;
        }
        private DateTime? GenerarFechaTermino(DateTime? fecha, TimeSpan? hora, TimeSpan? duracion)
        {
            return fecha.HasValue && hora.HasValue && duracion.HasValue
                       ? new DateTime(fecha.Value.Year, fecha.Value.Month, fecha.Value.Day, hora.Value.Hours,
                                      hora.Value.Minutes, hora.Value.Seconds, DateTimeKind.Utc).Add(duracion.Value)
                       : (DateTime?) null;
        }

	}
}