annotate Agendas/trunk/src/Agendas.Web/Controllers/PatrocinadorController.cs @ 278:6f5ab71614d4

#125: ABM de patrocinadores.
author juanjose.montesdeocaarbos
date Wed, 21 Dec 2011 08:47:05 -0300
parents bf993f99cee3
children 1e889a2e45c5
rev   line source
275
bf993f99cee3 Ticket #123: Patrocinadores de las vans.
juanjose.montesdeocaarbos
parents:
diff changeset
1 using System;
bf993f99cee3 Ticket #123: Patrocinadores de las vans.
juanjose.montesdeocaarbos
parents:
diff changeset
2 using System.Collections.Generic;
bf993f99cee3 Ticket #123: Patrocinadores de las vans.
juanjose.montesdeocaarbos
parents:
diff changeset
3 using System.IO;
bf993f99cee3 Ticket #123: Patrocinadores de las vans.
juanjose.montesdeocaarbos
parents:
diff changeset
4 using System.Linq;
bf993f99cee3 Ticket #123: Patrocinadores de las vans.
juanjose.montesdeocaarbos
parents:
diff changeset
5 using System.Web;
bf993f99cee3 Ticket #123: Patrocinadores de las vans.
juanjose.montesdeocaarbos
parents:
diff changeset
6 using System.Web.Mvc;
bf993f99cee3 Ticket #123: Patrocinadores de las vans.
juanjose.montesdeocaarbos
parents:
diff changeset
7 using AltNetHispano.Agendas.Domain;
bf993f99cee3 Ticket #123: Patrocinadores de las vans.
juanjose.montesdeocaarbos
parents:
diff changeset
8 using AltNetHispano.Agendas.Factories;
bf993f99cee3 Ticket #123: Patrocinadores de las vans.
juanjose.montesdeocaarbos
parents:
diff changeset
9 using AltNetHispano.Agendas.Web.Models;
bf993f99cee3 Ticket #123: Patrocinadores de las vans.
juanjose.montesdeocaarbos
parents:
diff changeset
10 using AltNetHispano.Agendas.Web.Services;
bf993f99cee3 Ticket #123: Patrocinadores de las vans.
juanjose.montesdeocaarbos
parents:
diff changeset
11
bf993f99cee3 Ticket #123: Patrocinadores de las vans.
juanjose.montesdeocaarbos
parents:
diff changeset
12
bf993f99cee3 Ticket #123: Patrocinadores de las vans.
juanjose.montesdeocaarbos
parents:
diff changeset
13 namespace AltNetHispano.Agendas.Web.Controllers
bf993f99cee3 Ticket #123: Patrocinadores de las vans.
juanjose.montesdeocaarbos
parents:
diff changeset
14 {
bf993f99cee3 Ticket #123: Patrocinadores de las vans.
juanjose.montesdeocaarbos
parents:
diff changeset
15 public class PatrocinadorController : Controller
bf993f99cee3 Ticket #123: Patrocinadores de las vans.
juanjose.montesdeocaarbos
parents:
diff changeset
16 {
278
6f5ab71614d4 #125: ABM de patrocinadores.
juanjose.montesdeocaarbos
parents: 275
diff changeset
17 private HttpServerUtilityBase _server;
6f5ab71614d4 #125: ABM de patrocinadores.
juanjose.montesdeocaarbos
parents: 275
diff changeset
18
6f5ab71614d4 #125: ABM de patrocinadores.
juanjose.montesdeocaarbos
parents: 275
diff changeset
19 public HttpServerUtilityBase HttpServer
6f5ab71614d4 #125: ABM de patrocinadores.
juanjose.montesdeocaarbos
parents: 275
diff changeset
20 {
6f5ab71614d4 #125: ABM de patrocinadores.
juanjose.montesdeocaarbos
parents: 275
diff changeset
21 get
6f5ab71614d4 #125: ABM de patrocinadores.
juanjose.montesdeocaarbos
parents: 275
diff changeset
22 {
6f5ab71614d4 #125: ABM de patrocinadores.
juanjose.montesdeocaarbos
parents: 275
diff changeset
23 if (_server == null)
6f5ab71614d4 #125: ABM de patrocinadores.
juanjose.montesdeocaarbos
parents: 275
diff changeset
24 {
6f5ab71614d4 #125: ABM de patrocinadores.
juanjose.montesdeocaarbos
parents: 275
diff changeset
25 var httpServer = HttpContext.Server;
6f5ab71614d4 #125: ABM de patrocinadores.
juanjose.montesdeocaarbos
parents: 275
diff changeset
26 _server = httpServer;
6f5ab71614d4 #125: ABM de patrocinadores.
juanjose.montesdeocaarbos
parents: 275
diff changeset
27 }
6f5ab71614d4 #125: ABM de patrocinadores.
juanjose.montesdeocaarbos
parents: 275
diff changeset
28
6f5ab71614d4 #125: ABM de patrocinadores.
juanjose.montesdeocaarbos
parents: 275
diff changeset
29 return _server;
6f5ab71614d4 #125: ABM de patrocinadores.
juanjose.montesdeocaarbos
parents: 275
diff changeset
30 }
6f5ab71614d4 #125: ABM de patrocinadores.
juanjose.montesdeocaarbos
parents: 275
diff changeset
31 }
275
bf993f99cee3 Ticket #123: Patrocinadores de las vans.
juanjose.montesdeocaarbos
parents:
diff changeset
32
bf993f99cee3 Ticket #123: Patrocinadores de las vans.
juanjose.montesdeocaarbos
parents:
diff changeset
33 public PatrocinadorController()
bf993f99cee3 Ticket #123: Patrocinadores de las vans.
juanjose.montesdeocaarbos
parents:
diff changeset
34 {
bf993f99cee3 Ticket #123: Patrocinadores de las vans.
juanjose.montesdeocaarbos
parents:
diff changeset
35 }
bf993f99cee3 Ticket #123: Patrocinadores de las vans.
juanjose.montesdeocaarbos
parents:
diff changeset
36
278
6f5ab71614d4 #125: ABM de patrocinadores.
juanjose.montesdeocaarbos
parents: 275
diff changeset
37 public PatrocinadorController(HttpServerUtilityBase server)
275
bf993f99cee3 Ticket #123: Patrocinadores de las vans.
juanjose.montesdeocaarbos
parents:
diff changeset
38 {
bf993f99cee3 Ticket #123: Patrocinadores de las vans.
juanjose.montesdeocaarbos
parents:
diff changeset
39 _server = server;
bf993f99cee3 Ticket #123: Patrocinadores de las vans.
juanjose.montesdeocaarbos
parents:
diff changeset
40 }
bf993f99cee3 Ticket #123: Patrocinadores de las vans.
juanjose.montesdeocaarbos
parents:
diff changeset
41
bf993f99cee3 Ticket #123: Patrocinadores de las vans.
juanjose.montesdeocaarbos
parents:
diff changeset
42 public ActionResult GetLogo(string id)
bf993f99cee3 Ticket #123: Patrocinadores de las vans.
juanjose.montesdeocaarbos
parents:
diff changeset
43 {
bf993f99cee3 Ticket #123: Patrocinadores de las vans.
juanjose.montesdeocaarbos
parents:
diff changeset
44 var patrocinadores = AgendaFactory.GetPatrocinadorRepository();
bf993f99cee3 Ticket #123: Patrocinadores de las vans.
juanjose.montesdeocaarbos
parents:
diff changeset
45
bf993f99cee3 Ticket #123: Patrocinadores de las vans.
juanjose.montesdeocaarbos
parents:
diff changeset
46 var patrocinador = patrocinadores.Get(new Guid(id));
bf993f99cee3 Ticket #123: Patrocinadores de las vans.
juanjose.montesdeocaarbos
parents:
diff changeset
47
bf993f99cee3 Ticket #123: Patrocinadores de las vans.
juanjose.montesdeocaarbos
parents:
diff changeset
48 return File(patrocinador.Logo, "image/jpg");
bf993f99cee3 Ticket #123: Patrocinadores de las vans.
juanjose.montesdeocaarbos
parents:
diff changeset
49 }
bf993f99cee3 Ticket #123: Patrocinadores de las vans.
juanjose.montesdeocaarbos
parents:
diff changeset
50
bf993f99cee3 Ticket #123: Patrocinadores de las vans.
juanjose.montesdeocaarbos
parents:
diff changeset
51 public ActionResult Index()
bf993f99cee3 Ticket #123: Patrocinadores de las vans.
juanjose.montesdeocaarbos
parents:
diff changeset
52 {
bf993f99cee3 Ticket #123: Patrocinadores de las vans.
juanjose.montesdeocaarbos
parents:
diff changeset
53 var patrocinadores = AgendaFactory.GetPatrocinadorRepository();
bf993f99cee3 Ticket #123: Patrocinadores de las vans.
juanjose.montesdeocaarbos
parents:
diff changeset
54
bf993f99cee3 Ticket #123: Patrocinadores de las vans.
juanjose.montesdeocaarbos
parents:
diff changeset
55 var model = new PatrocinadorIndexModel
bf993f99cee3 Ticket #123: Patrocinadores de las vans.
juanjose.montesdeocaarbos
parents:
diff changeset
56 {
bf993f99cee3 Ticket #123: Patrocinadores de las vans.
juanjose.montesdeocaarbos
parents:
diff changeset
57 Items = from p in patrocinadores.GetAll()
bf993f99cee3 Ticket #123: Patrocinadores de las vans.
juanjose.montesdeocaarbos
parents:
diff changeset
58 select new PatrocinadorDto
bf993f99cee3 Ticket #123: Patrocinadores de las vans.
juanjose.montesdeocaarbos
parents:
diff changeset
59 {
bf993f99cee3 Ticket #123: Patrocinadores de las vans.
juanjose.montesdeocaarbos
parents:
diff changeset
60 Id = p.Id,
bf993f99cee3 Ticket #123: Patrocinadores de las vans.
juanjose.montesdeocaarbos
parents:
diff changeset
61 Nombre = p.Nombre
bf993f99cee3 Ticket #123: Patrocinadores de las vans.
juanjose.montesdeocaarbos
parents:
diff changeset
62 }
bf993f99cee3 Ticket #123: Patrocinadores de las vans.
juanjose.montesdeocaarbos
parents:
diff changeset
63 };
bf993f99cee3 Ticket #123: Patrocinadores de las vans.
juanjose.montesdeocaarbos
parents:
diff changeset
64
bf993f99cee3 Ticket #123: Patrocinadores de las vans.
juanjose.montesdeocaarbos
parents:
diff changeset
65 return View(model);
bf993f99cee3 Ticket #123: Patrocinadores de las vans.
juanjose.montesdeocaarbos
parents:
diff changeset
66 }
bf993f99cee3 Ticket #123: Patrocinadores de las vans.
juanjose.montesdeocaarbos
parents:
diff changeset
67
bf993f99cee3 Ticket #123: Patrocinadores de las vans.
juanjose.montesdeocaarbos
parents:
diff changeset
68 [CustomAuthorize(Roles = Roles.Administrador)]
bf993f99cee3 Ticket #123: Patrocinadores de las vans.
juanjose.montesdeocaarbos
parents:
diff changeset
69 public ActionResult Nuevo()
bf993f99cee3 Ticket #123: Patrocinadores de las vans.
juanjose.montesdeocaarbos
parents:
diff changeset
70 {
bf993f99cee3 Ticket #123: Patrocinadores de las vans.
juanjose.montesdeocaarbos
parents:
diff changeset
71 var model = new PatrocinadorNewModel();
bf993f99cee3 Ticket #123: Patrocinadores de las vans.
juanjose.montesdeocaarbos
parents:
diff changeset
72 return View("DefaultEditor", model);
bf993f99cee3 Ticket #123: Patrocinadores de las vans.
juanjose.montesdeocaarbos
parents:
diff changeset
73 }
bf993f99cee3 Ticket #123: Patrocinadores de las vans.
juanjose.montesdeocaarbos
parents:
diff changeset
74
bf993f99cee3 Ticket #123: Patrocinadores de las vans.
juanjose.montesdeocaarbos
parents:
diff changeset
75 [HttpPost]
bf993f99cee3 Ticket #123: Patrocinadores de las vans.
juanjose.montesdeocaarbos
parents:
diff changeset
76 [CustomAuthorize(Roles = Roles.Administrador)]
bf993f99cee3 Ticket #123: Patrocinadores de las vans.
juanjose.montesdeocaarbos
parents:
diff changeset
77 public ActionResult Nuevo(PatrocinadorNewModel model)
bf993f99cee3 Ticket #123: Patrocinadores de las vans.
juanjose.montesdeocaarbos
parents:
diff changeset
78 {
bf993f99cee3 Ticket #123: Patrocinadores de las vans.
juanjose.montesdeocaarbos
parents:
diff changeset
79 if (ModelState.IsValid)
bf993f99cee3 Ticket #123: Patrocinadores de las vans.
juanjose.montesdeocaarbos
parents:
diff changeset
80 {
278
6f5ab71614d4 #125: ABM de patrocinadores.
juanjose.montesdeocaarbos
parents: 275
diff changeset
81 var tempLogoFile = Path.Combine(HttpServer.MapPath("~/App_Data"),
6f5ab71614d4 #125: ABM de patrocinadores.
juanjose.montesdeocaarbos
parents: 275
diff changeset
82 model.Nombre +
6f5ab71614d4 #125: ABM de patrocinadores.
juanjose.montesdeocaarbos
parents: 275
diff changeset
83 model.Logo.FileName.Substring(model.Logo.FileName.LastIndexOf(".")));
275
bf993f99cee3 Ticket #123: Patrocinadores de las vans.
juanjose.montesdeocaarbos
parents:
diff changeset
84 model.Logo.SaveAs(tempLogoFile);
bf993f99cee3 Ticket #123: Patrocinadores de las vans.
juanjose.montesdeocaarbos
parents:
diff changeset
85
bf993f99cee3 Ticket #123: Patrocinadores de las vans.
juanjose.montesdeocaarbos
parents:
diff changeset
86 var patrocinadores = AgendaFactory.GetPatrocinadorRepository();
bf993f99cee3 Ticket #123: Patrocinadores de las vans.
juanjose.montesdeocaarbos
parents:
diff changeset
87
278
6f5ab71614d4 #125: ABM de patrocinadores.
juanjose.montesdeocaarbos
parents: 275
diff changeset
88 if (patrocinadores.GetByNombre(model.Nombre) == null)
6f5ab71614d4 #125: ABM de patrocinadores.
juanjose.montesdeocaarbos
parents: 275
diff changeset
89 {
6f5ab71614d4 #125: ABM de patrocinadores.
juanjose.montesdeocaarbos
parents: 275
diff changeset
90 var patrocinador = new Patrocinador(model.Nombre);
6f5ab71614d4 #125: ABM de patrocinadores.
juanjose.montesdeocaarbos
parents: 275
diff changeset
91 patrocinador.LoadLogo(tempLogoFile);
275
bf993f99cee3 Ticket #123: Patrocinadores de las vans.
juanjose.montesdeocaarbos
parents:
diff changeset
92
278
6f5ab71614d4 #125: ABM de patrocinadores.
juanjose.montesdeocaarbos
parents: 275
diff changeset
93 patrocinadores.Save(patrocinador);
6f5ab71614d4 #125: ABM de patrocinadores.
juanjose.montesdeocaarbos
parents: 275
diff changeset
94
6f5ab71614d4 #125: ABM de patrocinadores.
juanjose.montesdeocaarbos
parents: 275
diff changeset
95 return RedirectToAction("Index");
6f5ab71614d4 #125: ABM de patrocinadores.
juanjose.montesdeocaarbos
parents: 275
diff changeset
96 }
6f5ab71614d4 #125: ABM de patrocinadores.
juanjose.montesdeocaarbos
parents: 275
diff changeset
97 this.AddError("El patrocinador ya existe.");
275
bf993f99cee3 Ticket #123: Patrocinadores de las vans.
juanjose.montesdeocaarbos
parents:
diff changeset
98 }
bf993f99cee3 Ticket #123: Patrocinadores de las vans.
juanjose.montesdeocaarbos
parents:
diff changeset
99
bf993f99cee3 Ticket #123: Patrocinadores de las vans.
juanjose.montesdeocaarbos
parents:
diff changeset
100 return View("DefaultEditor", model);
bf993f99cee3 Ticket #123: Patrocinadores de las vans.
juanjose.montesdeocaarbos
parents:
diff changeset
101 }
bf993f99cee3 Ticket #123: Patrocinadores de las vans.
juanjose.montesdeocaarbos
parents:
diff changeset
102
278
6f5ab71614d4 #125: ABM de patrocinadores.
juanjose.montesdeocaarbos
parents: 275
diff changeset
103 [CustomAuthorize(Roles = Roles.Administrador)]
6f5ab71614d4 #125: ABM de patrocinadores.
juanjose.montesdeocaarbos
parents: 275
diff changeset
104 public ActionResult Modificar(string id)
6f5ab71614d4 #125: ABM de patrocinadores.
juanjose.montesdeocaarbos
parents: 275
diff changeset
105 {
6f5ab71614d4 #125: ABM de patrocinadores.
juanjose.montesdeocaarbos
parents: 275
diff changeset
106 var patrocinadores = AgendaFactory.GetPatrocinadorRepository();
275
bf993f99cee3 Ticket #123: Patrocinadores de las vans.
juanjose.montesdeocaarbos
parents:
diff changeset
107
278
6f5ab71614d4 #125: ABM de patrocinadores.
juanjose.montesdeocaarbos
parents: 275
diff changeset
108 var patrocinador = patrocinadores.GetById(id);
6f5ab71614d4 #125: ABM de patrocinadores.
juanjose.montesdeocaarbos
parents: 275
diff changeset
109 if (patrocinador == null)
6f5ab71614d4 #125: ABM de patrocinadores.
juanjose.montesdeocaarbos
parents: 275
diff changeset
110 {
6f5ab71614d4 #125: ABM de patrocinadores.
juanjose.montesdeocaarbos
parents: 275
diff changeset
111 this.AddError("No se encontró el patrocinador que intenta modificar");
6f5ab71614d4 #125: ABM de patrocinadores.
juanjose.montesdeocaarbos
parents: 275
diff changeset
112 return RedirectToAction("Index");
6f5ab71614d4 #125: ABM de patrocinadores.
juanjose.montesdeocaarbos
parents: 275
diff changeset
113 }
6f5ab71614d4 #125: ABM de patrocinadores.
juanjose.montesdeocaarbos
parents: 275
diff changeset
114 var model = new PatrocinadorNewModel();
6f5ab71614d4 #125: ABM de patrocinadores.
juanjose.montesdeocaarbos
parents: 275
diff changeset
115 return View("DefaultEditor", model);
6f5ab71614d4 #125: ABM de patrocinadores.
juanjose.montesdeocaarbos
parents: 275
diff changeset
116 }
6f5ab71614d4 #125: ABM de patrocinadores.
juanjose.montesdeocaarbos
parents: 275
diff changeset
117
6f5ab71614d4 #125: ABM de patrocinadores.
juanjose.montesdeocaarbos
parents: 275
diff changeset
118 [HttpPost]
275
bf993f99cee3 Ticket #123: Patrocinadores de las vans.
juanjose.montesdeocaarbos
parents:
diff changeset
119 [CustomAuthorize(Roles = Roles.Administrador)]
278
6f5ab71614d4 #125: ABM de patrocinadores.
juanjose.montesdeocaarbos
parents: 275
diff changeset
120 public ActionResult Modificar(PatrocinadorEditModel model)
6f5ab71614d4 #125: ABM de patrocinadores.
juanjose.montesdeocaarbos
parents: 275
diff changeset
121 {
6f5ab71614d4 #125: ABM de patrocinadores.
juanjose.montesdeocaarbos
parents: 275
diff changeset
122 if (ModelState.IsValid)
6f5ab71614d4 #125: ABM de patrocinadores.
juanjose.montesdeocaarbos
parents: 275
diff changeset
123 {
6f5ab71614d4 #125: ABM de patrocinadores.
juanjose.montesdeocaarbos
parents: 275
diff changeset
124 var tempLogoFile = Path.Combine(HttpServer.MapPath("~/App_Data"),
6f5ab71614d4 #125: ABM de patrocinadores.
juanjose.montesdeocaarbos
parents: 275
diff changeset
125 model.Nombre +
6f5ab71614d4 #125: ABM de patrocinadores.
juanjose.montesdeocaarbos
parents: 275
diff changeset
126 model.Logo.FileName.Substring(model.Logo.FileName.LastIndexOf(".")));
6f5ab71614d4 #125: ABM de patrocinadores.
juanjose.montesdeocaarbos
parents: 275
diff changeset
127 model.Logo.SaveAs(tempLogoFile);
6f5ab71614d4 #125: ABM de patrocinadores.
juanjose.montesdeocaarbos
parents: 275
diff changeset
128
6f5ab71614d4 #125: ABM de patrocinadores.
juanjose.montesdeocaarbos
parents: 275
diff changeset
129 var patrocinadores = AgendaFactory.GetPatrocinadorRepository();
6f5ab71614d4 #125: ABM de patrocinadores.
juanjose.montesdeocaarbos
parents: 275
diff changeset
130
6f5ab71614d4 #125: ABM de patrocinadores.
juanjose.montesdeocaarbos
parents: 275
diff changeset
131 if (patrocinadores.GetByNombre(model.Nombre) != null)
6f5ab71614d4 #125: ABM de patrocinadores.
juanjose.montesdeocaarbos
parents: 275
diff changeset
132 {
6f5ab71614d4 #125: ABM de patrocinadores.
juanjose.montesdeocaarbos
parents: 275
diff changeset
133 var patrocinador = new Patrocinador(model.Nombre);
6f5ab71614d4 #125: ABM de patrocinadores.
juanjose.montesdeocaarbos
parents: 275
diff changeset
134 patrocinador.LoadLogo(tempLogoFile);
6f5ab71614d4 #125: ABM de patrocinadores.
juanjose.montesdeocaarbos
parents: 275
diff changeset
135
6f5ab71614d4 #125: ABM de patrocinadores.
juanjose.montesdeocaarbos
parents: 275
diff changeset
136 patrocinadores.Save(patrocinador);
6f5ab71614d4 #125: ABM de patrocinadores.
juanjose.montesdeocaarbos
parents: 275
diff changeset
137
6f5ab71614d4 #125: ABM de patrocinadores.
juanjose.montesdeocaarbos
parents: 275
diff changeset
138 return RedirectToAction("Index");
6f5ab71614d4 #125: ABM de patrocinadores.
juanjose.montesdeocaarbos
parents: 275
diff changeset
139 }
6f5ab71614d4 #125: ABM de patrocinadores.
juanjose.montesdeocaarbos
parents: 275
diff changeset
140 this.AddError("No se encuentra el patrocinador.");
6f5ab71614d4 #125: ABM de patrocinadores.
juanjose.montesdeocaarbos
parents: 275
diff changeset
141 }
6f5ab71614d4 #125: ABM de patrocinadores.
juanjose.montesdeocaarbos
parents: 275
diff changeset
142 return View("DefaultEditor", model);
6f5ab71614d4 #125: ABM de patrocinadores.
juanjose.montesdeocaarbos
parents: 275
diff changeset
143 }
275
bf993f99cee3 Ticket #123: Patrocinadores de las vans.
juanjose.montesdeocaarbos
parents:
diff changeset
144
bf993f99cee3 Ticket #123: Patrocinadores de las vans.
juanjose.montesdeocaarbos
parents:
diff changeset
145 [CustomAuthorize(Roles = Roles.Administrador)]
bf993f99cee3 Ticket #123: Patrocinadores de las vans.
juanjose.montesdeocaarbos
parents:
diff changeset
146 public ActionResult Quitar(int id)
bf993f99cee3 Ticket #123: Patrocinadores de las vans.
juanjose.montesdeocaarbos
parents:
diff changeset
147 {
bf993f99cee3 Ticket #123: Patrocinadores de las vans.
juanjose.montesdeocaarbos
parents:
diff changeset
148 return View();
bf993f99cee3 Ticket #123: Patrocinadores de las vans.
juanjose.montesdeocaarbos
parents:
diff changeset
149 }
bf993f99cee3 Ticket #123: Patrocinadores de las vans.
juanjose.montesdeocaarbos
parents:
diff changeset
150
bf993f99cee3 Ticket #123: Patrocinadores de las vans.
juanjose.montesdeocaarbos
parents:
diff changeset
151 }
bf993f99cee3 Ticket #123: Patrocinadores de las vans.
juanjose.montesdeocaarbos
parents:
diff changeset
152 }