10
|
1 using System;
|
|
2 using System.Collections.Generic;
|
|
3 using System.Linq;
|
|
4 using System.Web.Mvc;
|
15
|
5 using Agendas.Factories;
|
10
|
6 using Agendas.Web.Models;
|
|
7 using AltNetHispano.Agendas.Domain;
|
|
8 using AltNetHispano.Agendas.Domain.Exceptions;
|
|
9
|
|
10 namespace Agendas.Web.Controllers
|
|
11 {
|
|
12 public class AgendaController : Controller
|
|
13 {
|
|
14 public ActionResult Index()
|
|
15 {
|
|
16 var model = GetIndexModel();
|
|
17 return View(model);
|
|
18 }
|
|
19
|
|
20 private static AgendaIndexModel GetIndexModel()
|
|
21 {
|
|
22 var agenda = AgendaFactory.GetAgenda();
|
|
23
|
|
24 return new AgendaIndexModel
|
|
25 {
|
|
26 ProximosEventos = from e in agenda.GetEventosPublicados()
|
|
27 select new EventoDto {Id = e.Id.ToString(), Titulo = e.Titulo}
|
|
28 };
|
|
29 }
|
|
30
|
|
31 public ActionResult New()
|
|
32 {
|
|
33 return View();
|
|
34 }
|
|
35
|
|
36 [HttpPost]
|
|
37 public ActionResult New(AgendaNewModel model)
|
|
38 {
|
|
39 if (ModelState.IsValid)
|
|
40 {
|
|
41 var agenda = AgendaFactory.GetAgenda();
|
|
42
|
|
43 try
|
|
44 {
|
12
|
45 agenda.Publicar(model.Titulo, model.Ponente, model.Fecha);
|
10
|
46
|
|
47 return View("Index", GetIndexModel());
|
|
48 }
|
|
49 catch (ValidationException ex)
|
|
50 {
|
|
51 ModelState.AddModelError("error", ex.ToString());
|
|
52 }
|
|
53 }
|
|
54 return View(model);
|
|
55 }
|
14
|
56
|
|
57 public ActionResult Edit(string id)
|
|
58 {
|
|
59 var agenda = AgendaFactory.GetAgenda();
|
|
60 var evento = agenda.GetEventoPublicado(new Guid(id));
|
|
61 if (evento!=null)
|
|
62 {
|
|
63 var model = new AgendaEditModel {Id = id, Titulo = evento.Titulo, Ponente = evento.Ponente, Fecha = evento.Fecha};
|
|
64 return View(model);
|
|
65 }
|
|
66 ModelState.AddModelError("error","No se encontró el evento que quiere modificar");
|
|
67 return View();
|
|
68 }
|
|
69
|
|
70 [HttpPost]
|
|
71 public ActionResult Edit(AgendaEditModel model)
|
|
72 {
|
|
73 if (ModelState.IsValid)
|
|
74 {
|
|
75 var agenda = AgendaFactory.GetAgenda();
|
|
76
|
|
77 try
|
|
78 {
|
|
79 agenda.ModificarEvento(new Guid(model.Id), model.Titulo, model.Ponente, model.Fecha);
|
|
80
|
|
81 return View("Index", GetIndexModel());
|
|
82 }
|
|
83 catch (ValidationException ex)
|
|
84 {
|
|
85 ModelState.AddModelError("error", ex.ToString());
|
|
86 }
|
|
87 }
|
|
88 return View(model);
|
|
89 }
|
10
|
90 }
|
|
91 }
|