diff Agendas/trunk/src/Agendas.Web/Controllers/EventoController.cs @ 183:212c664db5aa

Generalización del manejo de las acciones sobre eventos
author nelopauselli
date Mon, 08 Aug 2011 22:27:00 -0300
parents beeb48ddb44a
children 2d02adb79322
line wrap: on
line diff
--- a/Agendas/trunk/src/Agendas.Web/Controllers/EventoController.cs	Mon Aug 08 21:57:10 2011 -0300
+++ b/Agendas/trunk/src/Agendas.Web/Controllers/EventoController.cs	Mon Aug 08 22:27:00 2011 -0300
@@ -47,40 +47,21 @@
 		[CustomAuthorize(Roles = Roles.Administrador)]
 		public ActionResult Nuevo(EventoNewModel model)
 		{
-			if (ModelState.IsValid)
-			{
-				var agenda = AgendaFactory.GetAgenda();
-
-				var r = agenda.Agendar(model.Titulo, model.Ponente, model.Fecha,
-				  model.UrlInvitacion, (TipoEvento)model.TipoEvento);
-				if (r.Succeful)
-				{
-					this.AddNotification("Evento creado");
-					foreach (var log in r.Warnings)
-						this.AddWarning(log.WarningMessage);
-					return RedirectToAction("Index");
-				}
-				ModelState.AddModelError("error", r.ToString());
-			}
-            return View("Defaulteditor", model);
+			return
+				GenericAction(
+					(agenda, m) => agenda.Agendar(m.Titulo, m.Ponente, m.Fecha, m.UrlInvitacion, (TipoEvento) m.TipoEvento),
+					m => View("Defaulteditor", m),
+					model);
 		}
 
-		[CustomAuthorize(Roles = Roles.Administrador)]
+    	[CustomAuthorize(Roles = Roles.Administrador)]
 		public ActionResult Confirmar(string id)
 		{
-			var agenda = AgendaFactory.GetAgenda();
-			
-			var r = agenda.Confirmar(new Guid(id));
-			if (r.Succeful)
-			{
-				this.AddNotification("Evento confirmado");
-				foreach (var log in r.Warnings)
-					this.AddWarning(log.WarningMessage);
-			}
-			else
-				this.AddError("Evento confirmado");
-
-			return RedirectToAction("Index");
+			return
+				GenericAction(
+					(agenda, m) => agenda.Confirmar(m),
+					m => View("Index", m),
+					new Guid(id));
 		}
 
 		[CustomAuthorize(Roles = Roles.Administrador)]
@@ -104,21 +85,11 @@
 		[CustomAuthorize(Roles = Roles.Administrador)]
 		public ActionResult Publicar(EventoPublicarModel model)
 		{
-			if (ModelState.IsValid)
-			{
-				var agenda = AgendaFactory.GetAgenda();
-
-				var r = agenda.Publicar(new Guid(model.Id), model.NroOrden, model.UrlWiki, model.DuracionReal);
-				if (r.Succeful)
-				{
-					this.AddNotification(string.Format("Evento publicado {0}", model.Titulo));
-					foreach (var log in r.Warnings)
-						this.AddWarning(log.WarningMessage);
-					return RedirectToAction("Index");
-				}
-				ModelState.AddModelError("error", r.ToString());
-			}
-			return View("Defaulteditor", model);
+			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)]
@@ -142,21 +113,11 @@
 		[CustomAuthorize(Roles = Roles.Administrador)]
 		public ActionResult Modificar(EventoEditModel model)
 		{
-			if (ModelState.IsValid)
-			{
-				var agenda = AgendaFactory.GetAgenda();
-
-				var r = agenda.ModificarEvento(new Guid(model.Id), model.Titulo, model.Ponente, model.Fecha.Value, model.UrlInvitacion);
-				if (r.Succeful)
-				{
-					this.AddNotification("evento modificado");
-					foreach (var log in r.Warnings)
-						this.AddWarning(log.WarningMessage);
-					return RedirectToAction("Index");
-				}
-				ModelState.AddModelError("error", r.ToString());
-			}
-            return View("Defaulteditor", model);
+			return
+			GenericAction(
+				(agenda, m) => agenda.ModificarEvento(new Guid(m.Id), m.Titulo, m.Ponente, m.Fecha.Value, m.UrlInvitacion),
+				m => View("Defaulteditor", m),
+				model);
 		}
 
 		[CustomAuthorize(Roles = Roles.Administrador)]
@@ -180,27 +141,16 @@
 		[CustomAuthorize(Roles = Roles.Administrador)]
 		public ActionResult Agendar(EventoAgendarModel model)
 		{
-			if (ModelState.IsValid)
-			{
-				var agenda = AgendaFactory.GetAgenda();
-
-				var r = agenda.ModificarEvento(new Guid(model.Id), model.Titulo, model.Ponente, model.Fecha.Value, model.UrlInvitacion);
-				if (r.Succeful)
-				{
-					this.AddNotification("evento agendado");
-					foreach (var log in r.Warnings)
-						this.AddWarning(log.WarningMessage);
-					return RedirectToAction("Index");
-				}
-				ModelState.AddModelError("error", r.ToString());
-			}
-            return View("Defaulteditor", model);
+			return
+				GenericAction(
+					(agenda, m) => agenda.ModificarEvento(new Guid(m.Id), m.Titulo, m.Ponente, m.Fecha.Value, m.UrlInvitacion),
+					m => View("Defaulteditor", m),
+					model);
 		}
 
 		[CustomAuthorize(Roles = Roles.Administrador)]
 		public ActionResult Proponer()
 		{
-			throw new NotImplementedException();
 			var model = new PropuestaNewModel();
             return View("Defaulteditor", model);
 		}
@@ -209,89 +159,72 @@
 		[CustomAuthorize(Roles = Roles.Administrador)]
 		public ActionResult Proponer(PropuestaNewModel model)
 		{
-			throw new NotImplementedException();
+			return
+				GenericAction(
+					(agenda, m) => agenda.Proponer(m.Titulo, m.Ponente, 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 = agenda.Proponer(model.Titulo, model.Ponente, model.UrlInvitacion, (TipoEvento)model.TipoEvento);
+				var r = action.Invoke(agenda, model);
 				if (r.Succeful)
 				{
-					this.AddNotification("Evento propuesto");
+					this.AddNotification(r.Message);
 					foreach (var log in r.Warnings)
 						this.AddWarning(log.WarningMessage);
 					return RedirectToAction("Index");
 				}
 				ModelState.AddModelError("error", r.ToString());
 			}
-            return View("Defaulteditor", model);
+			return actionresultOnFail.Invoke(model);
 		}
 
-		[CustomAuthorize(Roles = Roles.Administrador)]
-	    public ActionResult Cancelar(string id)
-	    {
-            var agenda = AgendaFactory.GetAgenda();
-            var r = agenda.Cancelar(new Guid(id));
 
-			if (r.Succeful)
-			{
-				this.AddNotification("Evento cancelado");
-				foreach (var log in r.Warnings)
-					this.AddWarning(log.WarningMessage);
-			}
-			else
-			this.AddError(r.Message);
-			return RedirectToAction("Index");
-	    }
-
-		[CustomAuthorize(Roles = Roles.Administrador)]
-	    public ActionResult Descartar(string id)
-	    {
-            var agenda = AgendaFactory.GetAgenda();
-            var r = agenda.Descartar(new Guid(id));
-
-			if (r.Succeful)
-            {this.AddNotification("Evento descartado");
-			foreach (var log in r.Warnings)
-				this.AddWarning(log.WarningMessage);
-			}
-			else
-				this.AddError(r.Message);
-            return RedirectToAction("Index");
-	    }
-
-		[CustomAuthorize(Roles = Roles.Administrador)]
-	    public ActionResult ReAgendar(string id)
-	    {
-            var agenda = AgendaFactory.GetAgenda();
-            var r = agenda.ReAgendar(new Guid(id));
-
-			if (r.Succeful)
-            {this.AddNotification("Evento re-agendado");
-			foreach (var log in r.Warnings)
-				this.AddWarning(log.WarningMessage);
-			}
-			else
-				this.AddError(r.Message);
-
-            return RedirectToAction("Index");
-	    }
-
-		[CustomAuthorize(Roles = Roles.Administrador)]
-	    public ActionResult ReProponer(string id)
-	    {
-            var agenda = AgendaFactory.GetAgenda();
-            var r = agenda.ReProponer(new Guid(id));
-
-			if (r.Succeful)
-			{
-				this.AddNotification("Evento re-propuesto");
-				foreach (var log in r.Warnings)
-					this.AddWarning(log.WarningMessage);
-			}
-			else
-				this.AddError(r.Message);
-            return RedirectToAction("Index");
-	    }
 	}
 }