Skip to content

Instantly share code, notes, and snippets.

@taylonr
Last active December 11, 2015 07:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save taylonr/4570557 to your computer and use it in GitHub Desktop.
Save taylonr/4570557 to your computer and use it in GitHub Desktop.
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