public void SaveUser(User user) { if (user.UserId == 0) Create(user); else Update(user); }
private void Update(User user) { using (var ctx = new FightFleetDataContext()) { ctx.Users.Attach(user, true); ctx.SubmitChanges(); } }
private void Create(User user) { ValidateUser(user); using (var ctx = new FightFleetDataContext()) { var existingUserName = ctx.Users.FirstOrDefault(c => c.UserName.ToLower() == user.UserName.ToLower()); if (existingUserName != null) throw new UserNameExistsException("Username Is In Use"); if (user.CreatedDate == DateTime.MinValue) user.CreatedDate = DateTime.Now; user.Password = HashPassword(user.Password); ctx.Users.InsertOnSubmit(user); ctx.SubmitChanges(); } }
public JsonResult CreateUser(string userName, string password) { var user = new FightFleet.User { UserName = userName, Password = password }; try { new UserManager().SaveUser(user); return(Authenticate(userName, password)); } catch (UserNameExistsException) { return(Json("username not unique", JsonRequestBehavior.AllowGet)); } catch (Exception ex) { return(Json("error: " + ex.Message, JsonRequestBehavior.AllowGet)); } }
private void ValidateUser(User user) { if (string.IsNullOrEmpty(user.UserName)) throw new MissingFieldException("UserName"); if(string.IsNullOrEmpty(user.Password)) throw new MissingFieldException("Password"); }
partial void DeleteUser(User instance);
partial void UpdateUser(User instance);
partial void InsertUser(User instance);