Created
May 11, 2009 18:27
-
-
Save cprieto/110089 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System.Collections.Generic; | |
using System.Web.Mvc; | |
using Rioshu.Educa.Extensions; | |
using Rioshu.Educa.Forms; | |
using Rioshu.Educa.Model; | |
using Rioshu.Educa.Services; | |
using Rioshu.Utils; | |
using Rioshu.Utils.Dbc; | |
namespace Rioshu.Educa.Controllers | |
{ | |
public class RegistrationController : Controller | |
{ | |
readonly IRegistrationService registrationService; | |
readonly IValidator validator; | |
public RegistrationController(IRegistrationService registration_service, IValidator validator) | |
{ | |
Check.Require(registration_service != null); | |
Check.Require(validator != null); | |
registrationService = registration_service; | |
this.validator = validator; | |
} | |
string CurrentCandidate | |
{ | |
get | |
{ | |
var candidate = Session["candidate_username"] as string; | |
Check.Ensure(!string.IsNullOrEmpty(candidate)); | |
return candidate; | |
} | |
set | |
{ | |
Check.Require(!string.IsNullOrEmpty(value)); | |
Session["candidate_username"] = value; | |
} | |
} | |
void ClearCandidateSession() | |
{ | |
Session.Remove("candidate_username"); | |
} | |
public ViewResult Signup() | |
{ | |
ViewData.Model = TempData.ContainsKey("member_form") | |
? TempData["member_form"] as MemberEntryForm | |
: new MemberEntryForm(); | |
return View(RegistrationViews.Signup); | |
} | |
[AcceptVerbs(HttpVerbs.Post)] | |
public ActionResult Register(MemberEntryForm member_form) | |
{ | |
MemberDto member = member_form.Member; | |
PasswordEntryForm password_entry = member_form.PasswordEntry; | |
Validate(member); | |
Validate(password_entry); | |
if (ModelState.IsValid) { | |
registrationService.RegisterCandidate(member, password_entry.Password); | |
CurrentCandidate = member.Username; | |
string view = Locate(member.Type); | |
return View(view, new MemberEntryForm {Member = member}); | |
} | |
TempData["member_form"] = member_form; | |
return this.RedirectToAction(x => x.Signup()); | |
} | |
[ActionName("Available")] | |
public bool UsernameAvailable(string username) | |
{ | |
return registrationService.Available(username); | |
} | |
[ActionName("Student")] | |
public ViewResult SignupStudent(StudyDto information) | |
{ | |
Validate(information); | |
if (ModelState.IsValid) { | |
string candidate = CurrentCandidate; | |
registrationService.RegisterStudent(candidate, information); | |
ClearCandidateSession(); | |
return View(RegistrationViews.Thanks); | |
} | |
return View(RegistrationViews.Student, new StudentEntryForm()); | |
} | |
[ActionName("Teacher")] | |
public ViewResult SignupTeacher(IEnumerable<TeachDto> teach_information) | |
{ | |
return View(RegistrationViews.Teacher, new TeacherInformationEntryForm()); | |
} | |
[ActionName("Parent")] | |
public ViewResult SignupParent(IEnumerable<StudyDto> children) | |
{ | |
return View(RegistrationViews.Parent); | |
} | |
[ActionName("Director")] | |
public ViewResult SignupDirectory(SchoolDto school) | |
{ | |
return View(RegistrationViews.Director); | |
} | |
[AcceptVerbs(HttpVerbs.Post)] | |
public JsonResult IsAvailable(string id) | |
{ | |
return Json(registrationService.Available(id)); | |
} | |
static string Locate(MemberType type) | |
{ | |
switch (type) { | |
case MemberType.Student: | |
return RegistrationViews.Student; | |
case MemberType.Teacher: | |
return RegistrationViews.Teacher; | |
case MemberType.Parent: | |
return RegistrationViews.Parent; | |
case MemberType.Director: | |
return RegistrationViews.Director; | |
default: | |
return string.Empty; | |
} | |
} | |
void Validate(object entity) | |
{ | |
ValidationResult errors = validator.Validate(entity); | |
if (errors.HasErrors) this.AddValidationErrors(errors); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment