public async Task <ResponseModel <RegisterAdminUserModel> > Handle(RegisterAdminUserCommand request, CancellationToken cancellationToken) { var salt = ByteArrayExtensions.GetRandomSalt(); var item = new AdminUser { Email = request.Email, Name = request.Name, Surname = request.Surname, TimeZone = request.TimeZone, CreatedDateTime = DateTimeOffset.Now, LastPasswordChangeDateTime = DateTimeOffset.Now, PasswordSalt = salt, Password = request.Password.GetSHA512(salt), }; using (IDbContextTransaction transaction = _context.Database.BeginTransaction()) { try { _context.Add(item); await _context.SaveChangesAsync(cancellationToken); transaction.Commit(); } catch (DbUpdateException ex) when((ex.InnerException is SqlException sqlException && (sqlException.Number == 2627 || sqlException.Number == 2601)) || (ex.InnerException is SqliteException sqliteException && sqliteException.SqliteErrorCode == 19)) { transaction.Rollback(); throw new ObjectAlreadyExistsException(nameof(AdminUser), nameof(AdminUser.Email)); } catch (Exception) { transaction.Rollback(); throw new TransactionException(); } } var model = new RegisterAdminUserModel(id: item.Id, email: item.Email, username: item.UserName, name: item.Name, surname: item.Surname, phoneNumber: item.PhoneNumber, timeZone: item.TimeZone, createDateTime: item.CreatedDateTime); return(new ResponseModel <RegisterAdminUserModel>(model)); }
public async Task <ResponseModel <UpdateAdminUserModel> > Handle(UpdateAdminUserCommand request, CancellationToken cancellationToken) { var item = await _context.AdminUsers.SingleOrDefaultAsync(au => au.Id == request.Id, cancellationToken); using (IDbContextTransaction transaction = _context.Database.BeginTransaction()) { try { _context.Add(item); await _context.SaveChangesAsync(cancellationToken); transaction.Commit(); } catch (DbUpdateException ex) when((ex.InnerException is SqlException sqlException && (sqlException.Number == 2627 || sqlException.Number == 2601)) || (ex.InnerException is SqliteException sqliteException && sqliteException.SqliteErrorCode == 19)) { transaction.Rollback(); throw new ObjectAlreadyExistsException(nameof(AdminUser), nameof(AdminUser.Email)); } catch (Exception) { transaction.Rollback(); throw new TransactionException(); } } var model = new UpdateAdminUserModel(id: item.Id, email: item.Email, username: item.UserName, name: item.Name, surname: item.Surname, phoneNumber: item.PhoneNumber, timeZone: item.TimeZone, createDateTime: item.CreatedDateTime); return(new ResponseModel <UpdateAdminUserModel>(model)); }