view Agendas/trunk/src/Agendas.Web/Controllers/PersonaController.cs @ 275:bf993f99cee3

Ticket #123: Patrocinadores de las vans.
author juanjose.montesdeocaarbos
date Wed, 14 Dec 2011 08:15:44 -0300
parents 730b80afa70d
children
line wrap: on
line source

using System;
using System.Linq;
using System.Web.Mvc;
using AltNetHispano.Agendas.Domain;
using AltNetHispano.Agendas.Factories;
using AltNetHispano.Agendas.Web.Models;

namespace AltNetHispano.Agendas.Web.Controllers
{
    public class PersonaController : Controller
    {
        public ActionResult Index()
        {
            var personas = AgendaFactory.GetPersonaService();

            var model = new PersonaIndexModel {Items = from p in personas.GetAll() select new PersonaDto{Id=p.Id, Nombre = p.Nombre}};

            return View(model);
        }

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

        [HttpPost]
		[CustomAuthorize(Roles = Roles.Administrador)]
        public ActionResult Nueva(PersonaNewModel model)
        {
            if (ModelState.IsValid)
            {
                var personas = AgendaFactory.GetPersonaService();
                var r = personas.Add(model.Nombre, model.Twitter, model.EMail,model.Blog, model.Roles);
                if (r.Succeful)
                {
                    this.AddNotification("Los datos fueron guardados");
                    return RedirectToAction("Index");
                }
            	this.AddError(r.Message);
            }

            return View("Defaulteditor", model);
        }

		[CustomAuthorize(Roles = Roles.Administrador)]
		public ActionResult Modificar(string id)
        {
            var personas = AgendaFactory.GetPersonaService();

            var persona = personas.GetById(new Guid(id));
            if (persona==null)
            {
                this.AddError("No se encontró la persona que intenta modificar");
                return RedirectToAction("Index");
            }

        	var model = new PersonaEditModel
        	            	{
        	            		Id = persona.Id.ToString(),
        	            		Nombre = persona.Nombre,
        	            		Twitter = persona.Twitter,
        	            		EMail = persona.Mail,
        	            		Blog = persona.Blog,
								Roles = persona.Roles.ToArray()
        	            	};

            return View("Defaulteditor", model);
        }

        [HttpPost]
		[CustomAuthorize(Roles = Roles.Administrador)]
		public ActionResult Modificar(PersonaEditModel model)
        {
            if (ModelState.IsValid)
            {
                var personas = AgendaFactory.GetPersonaService();
				var r = personas.Update(new Guid(model.Id), model.Nombre, model.Twitter, model.EMail, model.Blog, model.Roles);
                if (r.Succeful)
                {
                    this.AddNotification("Los datos fueron guardados");
                    return RedirectToAction("Index");
                }
            	this.AddError(r.Message);
            }

            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");
        }

        public ActionResult Ver(string id)
        {
            var personas = AgendaFactory.GetPersonaService();

            var persona = personas.GetById(new Guid(id));
            if (persona == null)
            {
                this.AddError("No se encontró la persona que intenta visualizar");
                return RedirectToAction("Index");
            }

            var model = new PersonaViewModel
            {
                Id = persona.Id.ToString(),
                Nombre = persona.Nombre,
                Twitter = persona.Twitter,
                EMail = persona.Mail,
                Blog = persona.Blog
            };

            return View("DefaultViewer", model);
        }
    }
}