changeset 209:a36a76bd6ec3

Se soluciona ticket 173. Se agrega eliminación de personas siempre y cuando no esté asociada a ninguna van.
author alabra
date Wed, 24 Aug 2011 19:27:30 -0300
parents 607384590bf8
children 75eefd82760f 7d838e7d83b3
files Agendas/trunk/src/Agendas.Domain/Repositories/IEventoRepository.cs Agendas/trunk/src/Agendas.Domain/Repositories/IPersonaRepository.cs Agendas/trunk/src/Agendas.Domain/Services/PersonaService.cs Agendas/trunk/src/Agendas.Factories/AgendaFactory.cs Agendas/trunk/src/Agendas.Repositories.Memory/EventoRepository.cs Agendas/trunk/src/Agendas.Repositories.NHibernate/EventoRepository.cs Agendas/trunk/src/Agendas.Repositories.NHibernate/PonenteRepository.cs Agendas/trunk/src/Agendas.Tests/Cruds/PersonaCrud.cs Agendas/trunk/src/Agendas.Tests/Cruds/PersonaCrudMemoryTests.cs Agendas/trunk/src/Agendas.Tests/Cruds/PersonaCrudNhTests.cs Agendas/trunk/src/Agendas.Tests/PersonaServiceTests.cs Agendas/trunk/src/Agendas.Web.Tests/AutorizationsTests.cs Agendas/trunk/src/Agendas.Web/Controllers/PersonaController.cs
diffstat 13 files changed, 148 insertions(+), 18 deletions(-) [+]
line wrap: on
line diff
--- a/Agendas/trunk/src/Agendas.Domain/Repositories/IEventoRepository.cs	Wed Aug 24 11:30:06 2011 -0300
+++ b/Agendas/trunk/src/Agendas.Domain/Repositories/IEventoRepository.cs	Wed Aug 24 19:27:30 2011 -0300
@@ -12,5 +12,6 @@
 		Evento GetPropuestaByTitulo(string titulo);
 		void Save(Evento evento);
 		void Update(Evento evento);
+	    bool ExistePonente(Persona ponente);
 	}
 }
\ No newline at end of file
--- a/Agendas/trunk/src/Agendas.Domain/Repositories/IPersonaRepository.cs	Wed Aug 24 11:30:06 2011 -0300
+++ b/Agendas/trunk/src/Agendas.Domain/Repositories/IPersonaRepository.cs	Wed Aug 24 19:27:30 2011 -0300
@@ -11,5 +11,6 @@
 		Cuenta GetCuenta(IdentityProviderEnum identityProvider, string username);
 	    Persona GetByTwitter(string username);
 	    Persona Get(Guid id);
+	    void Delete(Persona persona);
 	}
 }
\ No newline at end of file
--- a/Agendas/trunk/src/Agendas.Domain/Services/PersonaService.cs	Wed Aug 24 11:30:06 2011 -0300
+++ b/Agendas/trunk/src/Agendas.Domain/Services/PersonaService.cs	Wed Aug 24 19:27:30 2011 -0300
@@ -8,10 +8,12 @@
 	public class PersonaService
 	{
 		private readonly IPersonaRepository _personaRepository;
+	    private readonly IEventoRepository _eventoRepository;
 
-		public PersonaService(IPersonaRepository personaRepository)
+		public PersonaService(IPersonaRepository personaRepository, IEventoRepository eventoRepository)
 		{
 			_personaRepository = personaRepository;
+		    _eventoRepository = eventoRepository;
 		}
 
 		public bool CreateIfNotExist(IdentityProviderEnum identityProvider, string username, string nombre)
@@ -126,5 +128,22 @@
 
 			return new Resultado(true, "Datos actualizados");
 	    }
+
+        public Resultado Delete(Guid id)
+        {
+            var persona = _personaRepository.Get(id);
+            if (persona == null)
+                return new Resultado(false, string.Format("No se pudo encontrar la persona cuyo Id sea {0}", id));
+
+            var existeEnEvento = _eventoRepository.ExistePonente(persona);
+            if (existeEnEvento)
+                return new Resultado(false,
+                                     string.Format(
+                                         "La persona {0} está asociada a un Evento por lo que es imposible eliminarla.",
+                                         persona.Nombre));
+
+            _personaRepository.Delete(persona);
+            return new Resultado(true, "");
+        }
 	}
 }
\ No newline at end of file
--- a/Agendas/trunk/src/Agendas.Factories/AgendaFactory.cs	Wed Aug 24 11:30:06 2011 -0300
+++ b/Agendas/trunk/src/Agendas.Factories/AgendaFactory.cs	Wed Aug 24 19:27:30 2011 -0300
@@ -33,9 +33,15 @@
 			return new PersonaRepository(sessionFactory);
 		}
 
-		public static PersonaService GetPersonaService()
-		{
-			return new PersonaService(GetPersonaRepository());
-		}
+        public static IEventoRepository GetEventoRespository()
+        {
+            ISessionFactory sessionFactory = NhHelper.GetSessionFactory();
+            return new EventoRepository(sessionFactory);
+        }
+
+	    public static PersonaService GetPersonaService()
+	    {
+	        return new PersonaService(GetPersonaRepository(), GetEventoRespository());
+	    }
 	}
 }
\ No newline at end of file
--- a/Agendas/trunk/src/Agendas.Repositories.Memory/EventoRepository.cs	Wed Aug 24 11:30:06 2011 -0300
+++ b/Agendas/trunk/src/Agendas.Repositories.Memory/EventoRepository.cs	Wed Aug 24 19:27:30 2011 -0300
@@ -13,7 +13,12 @@
 			//nada que hacer en este método para este repositorio
 		}
 
-		public IList<Evento> GetByState(EventoState state)
+	    public bool ExistePonente(Persona ponente)
+	    {
+	        return Objects.Values.Any(ev => ev.Ponente == ponente);
+	    }
+
+	    public IList<Evento> GetByState(EventoState state)
 		{
 			return Objects.Values.Where(e => e.Estado == state).ToList();
 		}
--- a/Agendas/trunk/src/Agendas.Repositories.NHibernate/EventoRepository.cs	Wed Aug 24 11:30:06 2011 -0300
+++ b/Agendas/trunk/src/Agendas.Repositories.NHibernate/EventoRepository.cs	Wed Aug 24 19:27:30 2011 -0300
@@ -40,7 +40,12 @@
 			//No es necesario implementarlo
 		}
 
-		public IList<Evento> GetByState(EventoState state)
+	    public bool ExistePonente(Persona ponente)
+	    {
+            return Session.QueryOver<Evento>().Where(ev => ev.Ponente == ponente).RowCount() > 0;
+	    }
+
+	    public IList<Evento> GetByState(EventoState state)
 		{
 			return Session.QueryOver<Evento>().Where(e => e.Estado == state).List();
 		}
--- a/Agendas/trunk/src/Agendas.Repositories.NHibernate/PonenteRepository.cs	Wed Aug 24 11:30:06 2011 -0300
+++ b/Agendas/trunk/src/Agendas.Repositories.NHibernate/PonenteRepository.cs	Wed Aug 24 19:27:30 2011 -0300
@@ -33,5 +33,10 @@
 	    {
 	        return Session.QueryOver<Persona>().Where(p => p.Twitter == username).SingleOrDefault();
 	    }
+
+	    public void Delete(Persona persona)
+	    {
+	        Session.Delete(persona);
+	    }
 	}
 }
\ No newline at end of file
--- a/Agendas/trunk/src/Agendas.Tests/Cruds/PersonaCrud.cs	Wed Aug 24 11:30:06 2011 -0300
+++ b/Agendas/trunk/src/Agendas.Tests/Cruds/PersonaCrud.cs	Wed Aug 24 19:27:30 2011 -0300
@@ -12,11 +12,13 @@
 		private readonly IPersonaRepository _personaRepository;
 		private readonly Func<IDisposable> _requestEmulator;
 		private readonly PersonaService _personaService;
+	    private readonly IEventoRepository _eventoRepository;
 
-		public PersonaCrud(Func<IPersonaRepository> createpersonaRepository, Func<IDisposable> requestEmulator)
+		public PersonaCrud(Func<IPersonaRepository> createpersonaRepository, Func<IDisposable> requestEmulator, Func<IEventoRepository> createEventoRespository)
 		{
 			_personaRepository = createpersonaRepository.Invoke();
-			_personaService = new PersonaService(_personaRepository);
+		    _eventoRepository = createEventoRespository.Invoke();
+			_personaService = new PersonaService(_personaRepository, _eventoRepository);
 			_requestEmulator = requestEmulator;
 		}
 
@@ -118,5 +120,20 @@
 				Assert.AreEqual("Nelo Mariano Pauselli", persona.Nombre);
 			}
 		}
+        public void Delete()
+        {
+            var personaId = CreateUsuario();
+            using (_requestEmulator.Invoke())
+            {
+                var persona = _personaRepository.Get(personaId);
+                Assert.IsNotNull(persona);
+                _personaService.Delete(personaId);
+            }
+            using (_requestEmulator.Invoke())
+            {
+                var persona = _personaRepository.Get(personaId);
+                Assert.IsNull(persona);
+            }
+        }
 	}
 }
\ No newline at end of file
--- a/Agendas/trunk/src/Agendas.Tests/Cruds/PersonaCrudMemoryTests.cs	Wed Aug 24 11:30:06 2011 -0300
+++ b/Agendas/trunk/src/Agendas.Tests/Cruds/PersonaCrudMemoryTests.cs	Wed Aug 24 19:27:30 2011 -0300
@@ -30,7 +30,8 @@
 		[SetUp]
 		public void CreateCrud()
 		{
-			_test = new PersonaCrud(() => new PersonaRepository(), () => new Mock<IDisposable>().Object);
+		    _test = new PersonaCrud(() => new PersonaRepository(), () => new Mock<IDisposable>().Object,
+		                            () => new EventoRepository());
 		}
 
 		#endregion
@@ -64,5 +65,11 @@
 		{
 			_test.Update();
 		}
+
+        [Test]
+        public void Delete()
+        {
+            _test.Delete();
+        }
 	}
 }
\ No newline at end of file
--- a/Agendas/trunk/src/Agendas.Tests/Cruds/PersonaCrudNhTests.cs	Wed Aug 24 11:30:06 2011 -0300
+++ b/Agendas/trunk/src/Agendas.Tests/Cruds/PersonaCrudNhTests.cs	Wed Aug 24 19:27:30 2011 -0300
@@ -32,7 +32,8 @@
 		public void CreateCrud()
 		{
 			ISessionFactory sessionFactory = NhHelper.GetSessionFactory();
-			_test = new PersonaCrud(() => new PersonaRepository(sessionFactory), () => new RequestEmulator(sessionFactory));
+		    _test = new PersonaCrud(() => new PersonaRepository(sessionFactory), () => new RequestEmulator(sessionFactory),
+		                            () => new EventoRepository(sessionFactory));
 		}
 
 		#endregion
@@ -66,5 +67,11 @@
 		{
 			_test.Update();
 		}
+
+        [Test]
+        public void Delete()
+        {
+            _test.Delete();
+        }
 	}
 }
\ No newline at end of file
--- a/Agendas/trunk/src/Agendas.Tests/PersonaServiceTests.cs	Wed Aug 24 11:30:06 2011 -0300
+++ b/Agendas/trunk/src/Agendas.Tests/PersonaServiceTests.cs	Wed Aug 24 19:27:30 2011 -0300
@@ -18,7 +18,7 @@
 			Persona persona=null;
 			personaRepository.Setup(r => r.Save(It.IsAny<Persona>())).Callback<Persona>(p => persona = p);
 
-			var personaService = new PersonaService(personaRepository.Object);
+            var personaService = new PersonaService(personaRepository.Object, DefaultEventoRepository);
 			
 			Assert.IsTrue(personaService.CreateIfNotExist(IdentityProviderEnum.Twitter, "nelopauselli", "Nelo Pauselli"));
 			personaRepository.Verify(r=>r.Save(It.IsAny<Persona>()), Times.Once());
@@ -43,7 +43,7 @@
             var personaRepository = DefaultPersonaRepository;
 
             personaRepository.Save(new Persona("Mariano Pauselli", "marianopauselli@gmail.com", "marianopauselli", "http://marianopauselli.blogspot.com"));
-            var personaService = new PersonaService(personaRepository);
+            var personaService = new PersonaService(personaRepository, DefaultEventoRepository);
 
             Assert.IsTrue(personaService.CreateIfNotExist(IdentityProviderEnum.Twitter, "marianopauselli", "Mariano Pauselli"));
 
@@ -66,7 +66,7 @@
         {
             var personaRepository = DefaultPersonaRepository;
 
-            var personaService = new PersonaService(personaRepository);
+            var personaService = new PersonaService(personaRepository, DefaultEventoRepository);
 			var r = personaService.Add("Mariano Pauselli", "marianopauselli", string.Empty, string.Empty, null);
             Assert.IsTrue(r.Succeful);
 
@@ -97,8 +97,8 @@
             var p1 = personaRepository.GetByTwitter("nelopauselli");
             Assert.IsNotNull(p1);
             Assert.AreEqual(1, p1.Cuentas.Count());
-            
-            var personaService = new PersonaService(personaRepository);
+
+            var personaService = new PersonaService(personaRepository, DefaultEventoRepository);
 
             personaService.AddCuenta(IdentityProviderEnum.Google, "nelopauselli");
 
@@ -136,10 +136,51 @@
 		public void Modificar_persona_inexistente()
 		{
 			var personaRepository = DefaultPersonaRepository;
-			var personaService = new PersonaService(personaRepository);
+            var personaService = new PersonaService(personaRepository, DefaultEventoRepository);
 			var r = personaService.Update(new Guid(), "Mariano Pauselli", "marianopauselli", "marianopauselli@gmail.com", string.Empty, null);
 
 			Assert.IsFalse(r.Succeful);
 		}
+
+        [Test]
+        public void Eliminar_persona_no_asociado_van()
+        {
+            var personaRepository = DefaultPersonaRepository;
+
+            var personaService = new PersonaService(personaRepository, DefaultEventoRepository);
+            var r = personaService.Add("Alejandro Labra", "alabras", "alejandro.labra@gmail.com", string.Empty, null);
+            Assert.IsTrue(r.Succeful);
+            
+            var persona = personaRepository.GetByNombre("Alejandro Labra");
+            Assert.IsNotNull(persona);
+
+            var delete = personaService.Delete(persona.Id);
+            Assert.IsTrue(delete.Succeful);
+        }
+
+        [Test]
+        public void Eliminar_persona_asociado_vans()
+        {
+            var publicador = new Mock<IPublicador>();
+            var personaService = new PersonaService(DefaultPersonaRepository, DefaultEventoRepository);
+
+            var agenda = new Agenda(publicador.Object, DefaultEventoRepository, DefaultPersonaRepository);
+
+            var fechaInicio = DateTime.Now.ToUniversalTime();
+            var fechaTermino = fechaInicio.AddHours(2);
+            agenda.Agendar("Van para publicar", TestsHelper.GetOrCreatePonente("alabra"), fechaInicio, fechaTermino,
+                           "http://groups.google.com/group/altnet-hispano/browse_thread/thread/65d6d26eb381386e", TipoEvento.Van);
+
+            Assert.AreEqual(0, agenda.GetEventosActivos(EventoPropuestoState.GetInstance()).Count);
+
+            var eventos = agenda.GetEventosActivos();
+            Assert.AreEqual(1, eventos.Count);
+
+            var evento = eventos[0];
+            Assert.IsNotNull(evento.Ponente);
+
+            var delete = personaService.Delete(evento.Ponente.Id);
+            Assert.IsFalse(delete.Succeful);
+        }
 	}
 }
\ No newline at end of file
--- a/Agendas/trunk/src/Agendas.Web.Tests/AutorizationsTests.cs	Wed Aug 24 11:30:06 2011 -0300
+++ b/Agendas/trunk/src/Agendas.Web.Tests/AutorizationsTests.cs	Wed Aug 24 19:27:30 2011 -0300
@@ -92,7 +92,8 @@
 			               		"EventoController.Agendar", "EventoController.Confirmar", "EventoController.Nuevo",
 			               		"EventoController.Publicar", "EventoController.Modificar", "EventoController.Proponer",
 			               		"EventoController.Cancelar", "EventoController.Descartar", "EventoController.ReAgendar",
-			               		"EventoController.ReProponer", "PersonaController.Nueva", "PersonaController.Modificar"
+			               		"EventoController.ReProponer", "PersonaController.Nueva", "PersonaController.Modificar",
+                                "PersonaController.Quitar"
 			               	};
 
 			VerficarAccionesSeguras(acciones, Roles.Administrador, "debe ser de uso exclusivo de los administradores");
--- a/Agendas/trunk/src/Agendas.Web/Controllers/PersonaController.cs	Wed Aug 24 11:30:06 2011 -0300
+++ b/Agendas/trunk/src/Agendas.Web/Controllers/PersonaController.cs	Wed Aug 24 19:27:30 2011 -0300
@@ -87,5 +87,20 @@
 
             return View("Defaulteditor", model);
         }
+
+        [CustomAuthorize(Roles = Roles.Administrador)]
+		public ActionResult Quitar(string id)
+        {
+            var personas = AgendaFactory.GetPersonaService();
+            var  result = personas.Delete(new Guid(id));
+            if (result.Succeful)
+            {
+                this.AddNotification("Se ha eliminado correctamente a la persona");
+                
+            }
+            this.AddError(result.Message);
+
+            return RedirectToAction("Index");
+        }
     }
 }
\ No newline at end of file