Last active
December 11, 2015 07:59
-
-
Save taylonr/4570557 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
public class User | |
{ | |
public string Email {get; set;} | |
public string FirstName {get; set;} | |
public string LastName {get; set;} | |
public string Password {get; set;} | |
public int Id {get; set;] | |
public void Validate(bool isUpdate = false) | |
{ | |
if(isUpdate && Id <= 0) | |
throw new InvalidUserIdException(); | |
if(string.IsNullOrWhiteSpace(Email)) | |
throw new InvalidEmailException(); | |
if(!new Regex("^[A-Za-z]\w{6,}[A-Za-z]$").IsMatch(Password)) | |
throw new InvalidPasswordException(); | |
if(string.IsNullOrWhiteSpace(FirstName) || string.IsNullOrWhiteSpace(Lastame)) | |
throw new InvalidNameException(); | |
} | |
public void Update(User newUser) | |
{ | |
Email = newUser.Email; | |
FirstName = newUser.FirstName; | |
LastName = newUser.LastName; | |
Password = newUser.Password; | |
} | |
} | |
public class UserController : Controller | |
{ | |
[HttpPost] | |
public ActionResult Create(User newUser) | |
{ | |
new UserLogic().Create(newUser); | |
return View("UserCreated"); | |
} | |
[HttpPost] | |
public ActionResult Update(User updateUser) | |
{ | |
new UserLogc().Update(updateUser); | |
return View("UserUpdated"); | |
} | |
} | |
public class UserLogic | |
{ | |
public void Create(User newUser) | |
{ | |
newUser.Validate(); | |
var repo = new UserRepository(); | |
repo.Create(newUser); | |
} | |
public void Update(User update) | |
{ | |
update.Validate(true); | |
var repo = new UserRepository(); | |
var currentUser = repo.GetById(update.Id); | |
currentUser.Update(update); | |
repo.Save(currentUser); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment