Mercurial > altnet-hispano
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 | 17 private HttpServerUtilityBase _server; |
18 | |
19 public HttpServerUtilityBase HttpServer | |
20 { | |
21 get | |
22 { | |
23 if (_server == null) | |
24 { | |
25 var httpServer = HttpContext.Server; | |
26 _server = httpServer; | |
27 } | |
28 | |
29 return _server; | |
30 } | |
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 | 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 | 81 var tempLogoFile = Path.Combine(HttpServer.MapPath("~/App_Data"), |
82 model.Nombre + | |
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 | 88 if (patrocinadores.GetByNombre(model.Nombre) == null) |
89 { | |
90 var patrocinador = new Patrocinador(model.Nombre); | |
91 patrocinador.LoadLogo(tempLogoFile); | |
275
bf993f99cee3
Ticket #123: Patrocinadores de las vans.
juanjose.montesdeocaarbos
parents:
diff
changeset
|
92 |
278 | 93 patrocinadores.Save(patrocinador); |
94 | |
95 return RedirectToAction("Index"); | |
96 } | |
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 | 103 [CustomAuthorize(Roles = Roles.Administrador)] |
104 public ActionResult Modificar(string id) | |
105 { | |
106 var patrocinadores = AgendaFactory.GetPatrocinadorRepository(); | |
275
bf993f99cee3
Ticket #123: Patrocinadores de las vans.
juanjose.montesdeocaarbos
parents:
diff
changeset
|
107 |
278 | 108 var patrocinador = patrocinadores.GetById(id); |
109 if (patrocinador == null) | |
110 { | |
111 this.AddError("No se encontró el patrocinador que intenta modificar"); | |
112 return RedirectToAction("Index"); | |
113 } | |
114 var model = new PatrocinadorNewModel(); | |
115 return View("DefaultEditor", model); | |
116 } | |
117 | |
118 [HttpPost] | |
275
bf993f99cee3
Ticket #123: Patrocinadores de las vans.
juanjose.montesdeocaarbos
parents:
diff
changeset
|
119 [CustomAuthorize(Roles = Roles.Administrador)] |
278 | 120 public ActionResult Modificar(PatrocinadorEditModel model) |
121 { | |
122 if (ModelState.IsValid) | |
123 { | |
124 var tempLogoFile = Path.Combine(HttpServer.MapPath("~/App_Data"), | |
125 model.Nombre + | |
126 model.Logo.FileName.Substring(model.Logo.FileName.LastIndexOf("."))); | |
127 model.Logo.SaveAs(tempLogoFile); | |
128 | |
129 var patrocinadores = AgendaFactory.GetPatrocinadorRepository(); | |
130 | |
131 if (patrocinadores.GetByNombre(model.Nombre) != null) | |
132 { | |
133 var patrocinador = new Patrocinador(model.Nombre); | |
134 patrocinador.LoadLogo(tempLogoFile); | |
135 | |
136 patrocinadores.Save(patrocinador); | |
137 | |
138 return RedirectToAction("Index"); | |
139 } | |
140 this.AddError("No se encuentra el patrocinador."); | |
141 } | |
142 return View("DefaultEditor", model); | |
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 } |