changeset 278:6f5ab71614d4

#125: ABM de patrocinadores.
author juanjose.montesdeocaarbos
date Wed, 21 Dec 2011 08:47:05 -0300
parents 7439d7a5f8d0
children 1e889a2e45c5
files Agendas/trunk/db/v1.0/00 - Tablas.sql Agendas/trunk/src/Agendas.Domain/Patrocinador.cs Agendas/trunk/src/Agendas.Domain/Repositories/IPatrocinadorRepository.cs Agendas/trunk/src/Agendas.Repositories.Memory/PatrocinadorRepository.cs Agendas/trunk/src/Agendas.Repositories.NHibernate/PatrocinadorRepository.cs Agendas/trunk/src/Agendas.Web.Tests/Controllers/PatrocinadorControllerTests.cs Agendas/trunk/src/Agendas.Web.Tests/Helpers/HttpServerStub.cs Agendas/trunk/src/Agendas.Web/Agendas.Web.csproj Agendas/trunk/src/Agendas.Web/Controllers/PatrocinadorController.cs Agendas/trunk/src/Agendas.Web/Models/PatrocinadorModel.cs Agendas/trunk/src/Agendas.Web/Services/IHttpServer.cs Agendas/trunk/src/Agendas.Web/Views/Shared/_Menu.cshtml
diffstat 12 files changed, 277 insertions(+), 59 deletions(-) [+]
line wrap: on
line diff
--- a/Agendas/trunk/db/v1.0/00 - Tablas.sql	Tue Dec 20 08:28:33 2011 -0300
+++ b/Agendas/trunk/db/v1.0/00 - Tablas.sql	Wed Dec 21 08:47:05 2011 -0300
@@ -1,9 +1,20 @@
-DROP TABLE TrackLog
-DROP TABLE Track
-DROP TABLE Evento
-DROP TABLE Cuenta
-DROP TABLE Roles
-DROP TABLE Persona
+if (OBJECT_ID('TrackLog') is not null)
+	DROP TABLE TrackLog
+GO
+if (OBJECT_ID('Track') is not null)
+	DROP TABLE Track
+GO
+if (OBJECT_ID('Evento') is not null)
+	DROP TABLE Evento
+GO
+if (OBJECT_ID('Cuenta') is not null)
+	DROP TABLE Cuenta
+GO
+if (OBJECT_ID('Roles') is not null)
+	DROP TABLE Roles
+GO
+if (OBJECT_ID('Persona') is not null)
+	DROP TABLE Persona
 GO
 
 
--- a/Agendas/trunk/src/Agendas.Domain/Patrocinador.cs	Tue Dec 20 08:28:33 2011 -0300
+++ b/Agendas/trunk/src/Agendas.Domain/Patrocinador.cs	Wed Dec 21 08:47:05 2011 -0300
@@ -19,10 +19,12 @@
 
 		public virtual void LoadLogo(string path)
 		{
-			var image = Image.FromFile(path);
-			var ms = new MemoryStream();
-			image.Save(ms, image.RawFormat);
-			Logo = ms.ToArray();
+			using (var image = Image.FromFile(path))
+			{
+				var ms = new MemoryStream();
+				image.Save(ms, image.RawFormat);
+				Logo = ms.ToArray();
+			}
 		}
 	}
 }
\ No newline at end of file
--- a/Agendas/trunk/src/Agendas.Domain/Repositories/IPatrocinadorRepository.cs	Tue Dec 20 08:28:33 2011 -0300
+++ b/Agendas/trunk/src/Agendas.Domain/Repositories/IPatrocinadorRepository.cs	Wed Dec 21 08:47:05 2011 -0300
@@ -9,5 +9,7 @@
 		Patrocinador Get(Guid patrocinadorId);
 		void Delete(Patrocinador patrocinador);
 		IEnumerable<Patrocinador> GetAll();
+		Patrocinador GetByNombre(string nombre);
+		Patrocinador GetById(string id);
 	}
 }
\ No newline at end of file
--- a/Agendas/trunk/src/Agendas.Repositories.Memory/PatrocinadorRepository.cs	Tue Dec 20 08:28:33 2011 -0300
+++ b/Agendas/trunk/src/Agendas.Repositories.Memory/PatrocinadorRepository.cs	Wed Dec 21 08:47:05 2011 -0300
@@ -20,5 +20,15 @@
 
 			return patrocinadores;
 		}
+
+		public Patrocinador GetByNombre(string nombre)
+		{
+			return Objects.Values.SingleOrDefault(p => p.Nombre == nombre);
+		}
+
+		public Patrocinador GetById(string id)
+		{
+			return Objects.Values.SingleOrDefault(p => p.Id == new Guid(id));
+		}
 	}
 }
\ No newline at end of file
--- a/Agendas/trunk/src/Agendas.Repositories.NHibernate/PatrocinadorRepository.cs	Tue Dec 20 08:28:33 2011 -0300
+++ b/Agendas/trunk/src/Agendas.Repositories.NHibernate/PatrocinadorRepository.cs	Wed Dec 21 08:47:05 2011 -0300
@@ -13,11 +13,6 @@
 		{
 		}
 
-		public Patrocinador Get(Guid patrocinadorId)
-		{
-			return Session.Get<Patrocinador>(patrocinadorId);
-		}
-
 		public void Delete(Patrocinador patrocinador)
 		{
 			Session.Delete(patrocinador);
@@ -30,5 +25,15 @@
 
 			return patrocinadores;
 		}
+
+		public Patrocinador GetByNombre(string nombre)
+		{
+			return Session.QueryOver<Patrocinador>().Where(p => p.Nombre == nombre).SingleOrDefault();
+		}
+
+		public Patrocinador GetById(string id)
+		{
+			return Session.QueryOver<Patrocinador>().Where(p => p.Id == new Guid(id)).SingleOrDefault();
+		}
 	}
 }
\ No newline at end of file
--- a/Agendas/trunk/src/Agendas.Web.Tests/Controllers/PatrocinadorControllerTests.cs	Tue Dec 20 08:28:33 2011 -0300
+++ b/Agendas/trunk/src/Agendas.Web.Tests/Controllers/PatrocinadorControllerTests.cs	Wed Dec 21 08:47:05 2011 -0300
@@ -9,6 +9,7 @@
 using Agendas.NHibernate;
 using Agendas.Repositories.Tests.Infraestructure;
 using Agendas.Web.Tests.Helpers;
+using AltNetHispano.Agendas.Repositories.NHibernate;
 using AltNetHispano.Agendas.Web.Controllers;
 using AltNetHispano.Agendas.Web.Models;
 using Moq;
@@ -20,12 +21,37 @@
 	[TestFixture]
 	public class PatrocinadorControllerTests : MvcControllerTestsBase
 	{
-		[SetUp]
-		public void RegisterBinders()
+		internal void CrearPatrocinadorApress()
+		{
+			var patrocinadorController = new PatrocinadorController(new HttpServerStub())
+			{
+				ControllerContext = ControllerCtx
+			};
+			var form = new FormCollection
+			           	{
+			           		{"Nombre", "APRESS"},
+			           		{"Logo", @"images\apress.gif"}
+			           	};
+			var patrocinadorNew = BindModel<PatrocinadorNewModel>(patrocinadorController, form);
+
+			using (new RequestEmulator(NhHelper.GetSessionFactory()))
+			{
+				var resultNuevo = patrocinadorController.Nuevo(patrocinadorNew);
+			}
+		}
+
+		internal void RegisterBinders()
 		{
 			ModelBinders.Binders[typeof(HttpPostedFileBase)] = new HttpPostedFileBaseModelBinder();
 		}
 
+		[SetUp]
+		public void SetUpTests()
+		{
+			NhHelperTest.CleanDb();
+			RegisterBinders();
+		}
+
 		[Test]
 		public void PatrocinadorIndexGet()
 		{
@@ -98,7 +124,7 @@
 				var resultNuevo = patrocinadorController.Nuevo(patrocinadorNew);
 				Assert.IsInstanceOf(typeof(ViewResult), resultNuevo);
 				var viewResultNuevo = (ViewResult)resultNuevo;
-				Assert.AreEqual("DefaultEditor", viewResultNuevo.ViewName); //UploadFileEditor
+				Assert.AreEqual("DefaultEditor", viewResultNuevo.ViewName);
 				Assert.AreEqual(1, viewResultNuevo.ViewData.ModelState["Nombre"].Errors.Count);
 				Assert.AreEqual("El campo Nombre es obligatorio.", viewResultNuevo.ViewData.ModelState["Nombre"].Errors[0].ErrorMessage);
 				Assert.AreEqual(1, viewResultNuevo.ViewData.ModelState["Logo"].Errors.Count);
@@ -107,11 +133,125 @@
 		}
 
 		[Test]
-		[Ignore]
-		public void PatrocinadorNuevoPostExistente()
+		public void PatrocinadorNuevoPostExists()
+		{
+			CrearPatrocinadorApress();
+
+			var patrocinadorController = new PatrocinadorController(new HttpServerStub())
+			                             	{
+			                             		ControllerContext = ControllerCtx
+			                             	};
+			var form = new FormCollection
+			           	{
+			           		{"Nombre", "APRESS"},
+			           		{"Logo", @"images\apress.gif"}
+			           	};
+			var patrocinadorNew = BindModel<PatrocinadorNewModel>(patrocinadorController, form);
+
+			using (new RequestEmulator(NhHelper.GetSessionFactory()))
+			{
+				var resultNuevo = patrocinadorController.Nuevo(patrocinadorNew);
+				Assert.IsInstanceOf(typeof (ViewResult), resultNuevo);
+				var viewResultNuevo = (ViewResult) resultNuevo;
+
+				Assert.AreEqual("El patrocinador ya existe.", viewResultNuevo.TempData["error"]);
+			}
+		}
+
+		[Test]
+		public void PatrocinadorModificarGetDontExists()
+		{
+			var patrocinadorController = new PatrocinadorController(new HttpServerStub())
+			{
+				ControllerContext = ControllerCtx
+			};
+			using (new RequestEmulator(NhHelper.GetSessionFactory()))
+			{
+				var resultModificar = patrocinadorController.Modificar("11111111-1111-1111-1111-111111111111");
+				Assert.IsInstanceOf(typeof(RedirectToRouteResult), resultModificar);
+				Assert.AreEqual("Index", ((RedirectToRouteResult)resultModificar).RouteValues["action"]);
+				Assert.AreEqual("No se encontró el patrocinador que intenta modificar", patrocinadorController.TempData["error"]);
+			}
+		}
+
+		[Test]
+		public void PatrocinadorModificarGetExists()
 		{
+			CrearPatrocinadorApress();
 
+			var patrocinadorController = new PatrocinadorController(new HttpServerStub())
+			{
+				ControllerContext = ControllerCtx
+			};
+
+			using (new RequestEmulator(NhHelper.GetSessionFactory()))
+			{
+				var patrocinadorRepository = new PatrocinadorRepository(NhHelper.GetSessionFactory());
+				var resultModificar = patrocinadorController.Modificar(patrocinadorRepository.GetAll().FirstOrDefault().Id.ToString());
+				Assert.IsInstanceOf(typeof(ViewResult), resultModificar);
+				Assert.AreEqual("DefaultEditor", ((ViewResult)resultModificar).ViewName);
+			}
+		}
+
+		[Test]
+		public void PatrocinadorModificarPostSatisfactorio()
+		{
+			PatrocinadorEditModel patrocinadorEdit;
+
+			CrearPatrocinadorApress();
+			var patrocinadorController = new PatrocinadorController(new HttpServerStub())
+			                             	{
+			                             		ControllerContext = ControllerCtx
+			                             	};
+			using (new RequestEmulator(NhHelper.GetSessionFactory()))
+			{
+				var patrocinadorRepository = new PatrocinadorRepository(NhHelper.GetSessionFactory());
+
+				var form = new FormCollection
+				           	{
+				           		{"Id", patrocinadorRepository.GetAll().FirstOrDefault().Id.ToString()},
+				           		{"Nombre", "APRESS"},
+				           		{"Logo", @"images\apress.gif"}
+				           	};
+				patrocinadorEdit = BindModel<PatrocinadorEditModel>(patrocinadorController, form);
+			}
+
+			using (new RequestEmulator(NhHelper.GetSessionFactory()))
+			{
+				var resultModificar = patrocinadorController.Modificar(patrocinadorEdit);
+				Assert.IsInstanceOf(typeof(RedirectToRouteResult), resultModificar);
+				var viewResultEdit = (RedirectToRouteResult)resultModificar;
+				Assert.AreEqual("Index", viewResultEdit.RouteValues["action"]);
+			}
+		}
+
+		[Test]
+		public void PatrocinadorModificarPostRequiredFields()
+		{
+			CrearPatrocinadorApress();
+			var patrocinadorController = new PatrocinadorController(new HttpServerStub())
+			{
+				ControllerContext = ControllerCtx
+			};
+			var form = new FormCollection
+				        {
+				           	{"Id", ""},
+				           	{"Nombre", ""},
+				           	{"Logo", ""}
+				        };
+			var patrocinadorEdit = BindModel<PatrocinadorEditModel>(patrocinadorController, form);
+
+			using (new RequestEmulator(NhHelper.GetSessionFactory()))
+			{
+				var resultModificar = patrocinadorController.Modificar(patrocinadorEdit);
+				Assert.IsInstanceOf(typeof(ViewResult), resultModificar);
+				var viewResultNuevo = (ViewResult)resultModificar;
+				Assert.AreEqual("DefaultEditor", viewResultNuevo.ViewName);
+				Assert.AreEqual(1, viewResultNuevo.ViewData.ModelState["Nombre"].Errors.Count);
+				Assert.AreEqual("El campo Nombre es obligatorio.", viewResultNuevo.ViewData.ModelState["Nombre"].Errors[0].ErrorMessage);
+				Assert.AreEqual(1, viewResultNuevo.ViewData.ModelState["Logo"].Errors.Count);
+				Assert.AreEqual("El campo Logo es obligatorio.", viewResultNuevo.ViewData.ModelState["Logo"].Errors[0].ErrorMessage);
+			}
 		}
 	}
-
 }
--- a/Agendas/trunk/src/Agendas.Web.Tests/Helpers/HttpServerStub.cs	Tue Dec 20 08:28:33 2011 -0300
+++ b/Agendas/trunk/src/Agendas.Web.Tests/Helpers/HttpServerStub.cs	Wed Dec 21 08:47:05 2011 -0300
@@ -3,13 +3,14 @@
 using System.IO;
 using System.Linq;
 using System.Text;
+using System.Web;
 using AltNetHispano.Agendas.Web.Services;
 
 namespace Agendas.Web.Tests.Helpers
 {
-	public class HttpServerStub: IHttpServer
+	public class HttpServerStub : HttpServerUtilityBase
 	{
-		public string MapPath(string path)
+		public override string MapPath(string path)
 		{
 			var directory = new DirectoryInfo(AppDomain.CurrentDomain.BaseDirectory);
 			
--- a/Agendas/trunk/src/Agendas.Web/Agendas.Web.csproj	Tue Dec 20 08:28:33 2011 -0300
+++ b/Agendas/trunk/src/Agendas.Web/Agendas.Web.csproj	Wed Dec 21 08:47:05 2011 -0300
@@ -91,7 +91,6 @@
     <Compile Include="Properties\AssemblyInfo.cs" />
     <Compile Include="Services\FormsAuthenticationService.cs" />
     <Compile Include="Services\IFormsAuthenticationService.cs" />
-    <Compile Include="Services\IHttpServer.cs" />
   </ItemGroup>
   <ItemGroup>
     <Content Include="Content\AltNetHispanoVans.css" />
--- a/Agendas/trunk/src/Agendas.Web/Controllers/PatrocinadorController.cs	Tue Dec 20 08:28:33 2011 -0300
+++ b/Agendas/trunk/src/Agendas.Web/Controllers/PatrocinadorController.cs	Wed Dec 21 08:47:05 2011 -0300
@@ -14,14 +14,27 @@
 {
     public class PatrocinadorController : Controller
     {
-    	private IHttpServer _server;
+		private HttpServerUtilityBase _server;
+
+		public HttpServerUtilityBase HttpServer
+		{
+			get
+			{
+				if (_server == null)
+				{
+					var httpServer = HttpContext.Server;
+					_server =  httpServer;
+				}
+
+				return _server;
+			}
+		}
 
 		public PatrocinadorController()
 		{
-			_server = (IHttpServer)ControllerContext.HttpContext.Server;
 		}
 
-		public PatrocinadorController(IHttpServer server)
+		public PatrocinadorController(HttpServerUtilityBase server)
 		{
 			_server = server;
 		}
@@ -65,43 +78,69 @@
 		{
 			if (ModelState.IsValid)
 			{
-				var tempLogoFile = Path.Combine(_server.MapPath("~/App_Data"), model.Nombre + model.Logo.FileName.Substring(model.Logo.FileName.LastIndexOf(".")));
+				var tempLogoFile = Path.Combine(HttpServer.MapPath("~/App_Data"),
+												model.Nombre +
+												model.Logo.FileName.Substring(model.Logo.FileName.LastIndexOf(".")));
 				model.Logo.SaveAs(tempLogoFile);
 
 				var patrocinadores = AgendaFactory.GetPatrocinadorRepository();
 
-				var patrocinador = new Patrocinador(model.Nombre);
-				patrocinador.LoadLogo(tempLogoFile);
-				
-				patrocinadores.Save(patrocinador);
+				if (patrocinadores.GetByNombre(model.Nombre) == null)
+				{
+					var patrocinador = new Patrocinador(model.Nombre);
+					patrocinador.LoadLogo(tempLogoFile);
 
-				return RedirectToAction("Index");
+					patrocinadores.Save(patrocinador);
+
+					return RedirectToAction("Index");
+				}
+				this.AddError("El patrocinador ya existe.");
 			}
 
 			return View("DefaultEditor", model);
         }
 
-    	[CustomAuthorize(Roles = Roles.Administrador)]
-		public ActionResult Modificar(int id)
-        {
-            return View();
-        }
+		[CustomAuthorize(Roles = Roles.Administrador)]
+		public ActionResult Modificar(string id)
+		{
+			var patrocinadores = AgendaFactory.GetPatrocinadorRepository();
 
-        [HttpPost]
+			var patrocinador = patrocinadores.GetById(id);
+			if (patrocinador == null)
+			{
+				this.AddError("No se encontró el patrocinador que intenta modificar");
+				return RedirectToAction("Index");
+			}
+			var model = new PatrocinadorNewModel();
+			return View("DefaultEditor", model);
+		}
+
+		[HttpPost]
 		[CustomAuthorize(Roles = Roles.Administrador)]
-		public ActionResult Modificar(int id, FormCollection collection)
-        {
-            try
-            {
-                // TODO: Add update logic here
- 
-                return RedirectToAction("Index");
-            }
-            catch
-            {
-                return View();
-            }
-        }
+		public ActionResult Modificar(PatrocinadorEditModel model)
+		{
+			if (ModelState.IsValid)
+			{
+				var tempLogoFile = Path.Combine(HttpServer.MapPath("~/App_Data"),
+												model.Nombre +
+												model.Logo.FileName.Substring(model.Logo.FileName.LastIndexOf(".")));
+				model.Logo.SaveAs(tempLogoFile);
+
+				var patrocinadores = AgendaFactory.GetPatrocinadorRepository();
+
+				if (patrocinadores.GetByNombre(model.Nombre) != null)
+				{
+					var patrocinador = new Patrocinador(model.Nombre);
+					patrocinador.LoadLogo(tempLogoFile);
+
+					patrocinadores.Save(patrocinador);
+
+					return RedirectToAction("Index");
+				}
+				this.AddError("No se encuentra el patrocinador.");
+			}
+			return View("DefaultEditor", model);
+		}
 
 		[CustomAuthorize(Roles = Roles.Administrador)]
 		public ActionResult Quitar(int id)
--- a/Agendas/trunk/src/Agendas.Web/Models/PatrocinadorModel.cs	Tue Dec 20 08:28:33 2011 -0300
+++ b/Agendas/trunk/src/Agendas.Web/Models/PatrocinadorModel.cs	Wed Dec 21 08:47:05 2011 -0300
@@ -28,4 +28,17 @@
 		[Required]
 		public HttpPostedFileBase Logo { get; set; }
 	}
+
+	public class PatrocinadorEditModel
+	{
+		[HiddenInput(DisplayValue = false)]
+		public string Id { get; set; }
+
+		[Required]
+		[HiddenInput]
+		public string Nombre { get; set; }
+
+		[Required]
+		public HttpPostedFileBase Logo { get; set; }
+	}
 }
\ No newline at end of file
--- a/Agendas/trunk/src/Agendas.Web/Services/IHttpServer.cs	Tue Dec 20 08:28:33 2011 -0300
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,7 +0,0 @@
-namespace AltNetHispano.Agendas.Web.Services
-{
-	public interface IHttpServer
-	{
-		string MapPath(string path);
-	}
-}
--- a/Agendas/trunk/src/Agendas.Web/Views/Shared/_Menu.cshtml	Tue Dec 20 08:28:33 2011 -0300
+++ b/Agendas/trunk/src/Agendas.Web/Views/Shared/_Menu.cshtml	Wed Dec 21 08:47:05 2011 -0300
@@ -8,6 +8,9 @@
     <li class="rmItem">
         <a class="rmLink" href="@Url.Action("Index", "Historico")"><span class="rmText">Histórico</span></a>
     </li>
+    <li class="rmItem">
+        <a class="rmLink" href="@Url.Action("Index", "Patrocinador")"><span class="rmText">Patrocinadores</span></a>
+    </li>
 @if (Request.IsAuthenticated)
 {
     <li class="rmItem">