public async Task <CommandResult> ExecuteAsync(UserUpdateContext context) { var user = await _db.Users.FirstOrDefaultAsync(x => x.Id == context.Id); var login = await _db.Logins.FirstOrDefaultAsync(x => x.Id == user.LoginId); var cleanPhone = _phoneCleaner.Clean(context.User.Phone); context.User.Phone = cleanPhone.Clean; if (user == null) { return(CommandResult.Fail("USER_NOT_FOUND", "Пользователь не найден")); } if (!DAL.Entities.User.Statuses.Available.Contains(context.User.Status)) { return(CommandResult.Fail("STATUS_NOT_CORRECT", "Неверный статус")); } var uniqueEmail = await _userSpecs.IsUserEmailUnique(context.User.UserName); if (!uniqueEmail) { return(CommandResult.FromSpec(uniqueEmail)); } var model = context.User; user.FirstName = model.FirstName; user.LastName = model.LastName; user.PatronymicName = model.PatronymicName; user.Status = model.Status; user.BirthDate = model.BirthDate; user.Phone = model.Phone; user.PhotoUploadId = model.PhotoUploadId; login.Type = context.Type; await _db.SaveChangesAsync(); return(CommandResult.Success()); }
public async Task <CommandResult> ExecuteAsync(UserCreateContext context) { context.User.CreatedDate = DateTime.UtcNow; context.User.Status = DAL.Entities.User.Statuses.Active; var cleanPhone = _phoneCleaner.Clean(context.User.Phone); context.User.Phone = cleanPhone.Clean; var uniqueSpec = await _userSpecs.IsUserPhoneUnique(cleanPhone); if (!uniqueSpec) { return(CommandResult.FromSpec(uniqueSpec)); } var uniqueEmail = await _userSpecs.IsUserEmailUnique(context.User.UserName); if (!uniqueEmail) { return(CommandResult.FromSpec(uniqueEmail)); } using (var transaction = await _db.Database.BeginTransactionAsync()) { context.Login.UserName = context.Login.UserName; _db.Logins.Add(context.Login); await _db.SaveChangesAsync(); context.User.LoginId = context.Login.Id; context.User.Email = context.User.UserName; _db.Users.Add(context.User); await _db.SaveChangesAsync(); transaction.Commit(); } return(CommandResult.Success()); }