public
Created

  • Download Gist
ConferenceController.cs
C#
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
public class ConferenceController : Controller
{
private readonly ISession _session;
 
public ConferenceController(ISession session)
{
_session = session;
}
 
public ActionResult Index(int? minSessions)
{
minSessions = minSessions ?? 0;
 
var list = (from conf in _session.Query<Conference>()
where conf.SessionCount >= minSessions
select new ConferenceListModel
{
Id = conf.Id,
Name = conf.Name,
SessionCount = conf.SessionCount,
AttendeeCount = conf.AttendeeCount
}).ToArray();
 
return View(list);
}
 
public ViewResult Show(string eventName)
{
var conf = _session
.Query<Conference>()
.SingleOrDefault(c => c.Name == eventName);
 
var model = new ConferenceShowModel
{
Name = conf.Name,
Sessions = conf.GetSessions()
.Select(s => new ConferenceShowModel.SessionModel
{
Title = s.Title,
SpeakerFirstName = s.Speaker.FirstName,
SpeakerLastName = s.Speaker.LastName,
}).ToArray()
};
 
return View(model);
}
 
public ActionResult Edit(string eventName)
{
var conf = _session
.Query<Conference>()
.SingleOrDefault(c => c.Name == eventName);
 
var model = new ConferenceEditModel
{
Id = conf.Id,
Name = conf.Name,
Attendees = conf.GetAttendees()
.Select(a => new ConferenceEditModel.AttendeeEditModel
{
Id = a.Id,
FirstName = a.FirstName,
LastName = a.LastName,
Email = a.Email,
}).ToArray()
};
 
return View(model);
}
 
 
[HttpPost]
public ActionResult Edit(ConferenceEditModel form)
{
if (!ModelState.IsValid)
{
return View(form);
}
 
var conf = _session.Get<Conference>(form.Id);
 
conf.ChangeName(form.Name);
 
foreach (var attendeeEditModel in form.Attendees)
{
var attendee = conf.GetAttendee(attendeeEditModel.Id);
 
attendee.ChangeName(attendeeEditModel.FirstName, attendeeEditModel.LastName);
attendee.Email = attendeeEditModel.Email;
}
 
return this.RedirectToAction(c => c.Index(null), "Default");
}
}

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.