/
EventController.cs
133 lines (111 loc) · 4.56 KB
/
EventController.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using PeerLearn.Data.Entities;
using PeerLearn.Web.Models;
using PeerLearn.Web.Service;
namespace PeerLearn.Web.Controllers
{
public class EventController : Controller
{
private readonly IEventService _eventService;
public EventController(IEventService eventService)
{
_eventService = eventService;
}
//
// GET: /Event/
public ActionResult Index()
{
return RedirectToAction("Index", "Home");
}
//
// GET: /Event/Details/5
public ActionResult Details(int id)
{
var ev = _eventService.GetEvent(id);
if (ev == null)
return RedirectToAction("Unknown");
return View(ev);
}
public ActionResult Unknown()
{
return View();
}
//
// GET: /Event/Create
[Authorize]
public ActionResult Create()
{
return View();
}
//
// POST: /Event/Create
[Authorize]
[HttpPost]
public ActionResult Create(NewEvent newEvent)
{
try
{
if(ModelState.IsValid)
{
//Should probably refactor this into a static method in IEventService /shrug
var insertedEvent = new Event
{
EventName = Server.HtmlEncode(newEvent.EventName),
EventDescription = Server.HtmlEncode(newEvent.EventDescription),
EventBeginDate = newEvent.EventBeginTime,
EventEndDate = newEvent.EventEndTime,
RegistrationOpenDate = newEvent.RegistrationBegins,
Address = new Address
{
StreetAddress =
Server.HtmlEncode(newEvent.StreetAddress),
StreetAddress2 =
Server.HtmlEncode(newEvent.StreetAddress2 ??
string.Empty),
City = Server.HtmlEncode(newEvent.City),
PostalCode = Server.HtmlEncode(newEvent.PostalCode),
State = Server.HtmlEncode(newEvent.State)
}
};
var user = _eventService.Repository.GetUserByName(HttpContext.User.Identity.Name);
insertedEvent.AddAttendee(user);
insertedEvent.AddOrganizer(user);
var result = _eventService.Repository.CreateEvent(insertedEvent);
if(result)
{
return RedirectToAction("Details", new {id = insertedEvent.EventId});
}
else
{
ModelState.AddModelError("", "Unable to add event to calendar.");
return View(newEvent);
}
}
ModelState.AddModelError("", "Invalid event - please correct errors before re-submitting.");
return View(newEvent);
}
catch
{
ModelState.AddModelError("", "Unable to add event to calendar.");
return View();
}
}
#region Validation Actions
public JsonResult EventNameAvailable(string eventname)
{
var existingEvent = _eventService.GetEvent(eventname);
if (existingEvent == null)
return Json(true, JsonRequestBehavior.AllowGet);
else
{
return Json(string.Format("An event already exists with name {0}", eventname),
JsonRequestBehavior.AllowGet);
}
}
#endregion
}
}