public Contracts.User.User Update(Contracts.User.UpdateUser update) { using (var db = new RAAPMasterEntities()) { var user = db.Users.FirstOrDefault(u => u.UserId == update.UserId); if (user == null) { throw new RAAPNotFoundException("Item not found."); } if (db.Users.Any(a => a.Email == update.Email && a.UserId != update.UserId)) { throw new RAAPConflictException("Email is already in use!"); } if (db.Users.Any(a => a.Username == update.Username && a.UserId != update.UserId)) { throw new RAAPConflictException("Username is already in use!"); } user.ApplyUpdate(update); user.Roles.Clear(); db.SaveChanges(); foreach (var role in update.Roles) { var roleToAdd = db.Roles.FirstOrDefault(r => r.Role1 == role); roleToAdd.Users.Add(user); } db.SaveChanges(); return(user.ToContract(true)); } }
public static void ApplyUpdate(this User dataItem, Contracts.User.UpdateUser update) { dataItem.FirstName = update.FirstName; dataItem.LastName = update.LastName; dataItem.CompanyId = update.CompanyId; dataItem.Email = update.Email; dataItem.Username = update.Username; dataItem.UpdatedOn = DateTime.Now; dataItem.Title = update.Title; dataItem.Phone = update.Phone; dataItem.Department = update.Department; }