diff Agendas/trunk/src/Agendas.Web/Controllers/PatrocinadorApiController.cs @ 285:c8f378272407

#123: Patrocinadores de las vans.
author juanjose.montesdeocaarbos
date Sat, 31 Dec 2011 14:45:55 -0300
parents
children 1408ac17cb64
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/Agendas/trunk/src/Agendas.Web/Controllers/PatrocinadorApiController.cs	Sat Dec 31 14:45:55 2011 -0300
@@ -0,0 +1,72 @@
+using System;
+using System.Collections.Generic;
+using System.Linq;
+using System.Web;
+using System.Web.Mvc;
+using System.IO;
+using AltNetHispano.Agendas.Domain;
+using AltNetHispano.Agendas.Factories;
+using AltNetHispano.Agendas.Web.Models;
+
+namespace AltNetHispano.Agendas.Web.Controllers
+{
+    public class PatrocinadorApiController : Controller
+    {
+		private HttpServerUtilityBase _server;
+
+		public HttpServerUtilityBase HttpServer
+		{
+			get
+			{
+				if (_server == null)
+				{
+					var httpServer = HttpContext.Server;
+					_server = httpServer;
+				}
+
+				return _server;
+			}
+		}
+
+		public PatrocinadorApiController()
+		{ }
+
+		public PatrocinadorApiController(HttpServerUtilityBase server)
+		{
+			_server = server;
+		}
+
+		//[CustomAuthorize(Roles = Roles.Administrador)]
+		public PartialViewResult Nuevo()
+        {
+			var model = new PatrocinadorNewModel();
+			return PartialView(model);
+        }
+
+		[HttpPost]
+		//[CustomAuthorize(Roles = Roles.Administrador)]
+		public JsonResult Nuevo(PatrocinadorNewModel model)
+		{
+			if (ModelState.IsValid)
+			{
+				var patrocinadores = AgendaFactory.GetPatrocinadorRepository();
+				if (patrocinadores.GetByNombre(model.Nombre) == null)
+				{
+					var tempLogoFile = Path.Combine(HttpServer.MapPath("~/App_Data"),
+													model.Nombre +
+													model.Logo.FileName.Substring(model.Logo.FileName.LastIndexOf(".")));
+					model.Logo.SaveAs(tempLogoFile);
+					var patrocinador = new Patrocinador(model.Nombre);
+					patrocinador.LoadLogo(tempLogoFile);
+
+					patrocinadores.Save(patrocinador);
+					
+					return Json(new Resultado(true, string.Empty));
+				}
+				return Json(new Resultado(false, "El patrocinador ya existe."));
+			}
+
+			return Json(new Resultado(false, string.Join(",", ModelState.Values)));
+		}
+	}
+}