view Agendas/trunk/src/Agendas.Web/Controllers/PatrocinadorApiController.cs @ 292:1408ac17cb64

Patrocinadores: Faltaba aplicar seguridad a la Api de Patrocinadores.
author juanjose.montesdeocaarbos
date Thu, 09 Feb 2012 08:40:38 -0300
parents c8f378272407
children 9bc60d166c8a
line wrap: on
line source

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