public async Task ExecuteAsync(UpdateUserCommand command, IExecutionContext executionContext) { // Get User var user = await _dbContext .Users .FilterCanLogIn() .FilterById(command.UserId) .SingleOrDefaultAsync(); EntityNotFoundException.ThrowIfNull(user, command.UserId); // Validate var userArea = _userAreaRepository.GetByCode(user.UserAreaCode); ValidatePermissions(userArea, executionContext); ValidateCommand(command, userArea); await ValidateIsUniqueAsync(command, userArea, executionContext); // Role if (command.RoleId != user.RoleId) { user.Role = await _userCommandPermissionsHelper.GetAndValidateNewRoleAsync( command.RoleId, user.RoleId, user.UserAreaCode, executionContext ); } // Map Map(command, user, userArea); // Save await _dbContext.SaveChangesAsync(); }
public async Task ExecuteAsync(AddUserCommand command, IExecutionContext executionContext) { var userArea = _userAreaRepository.GetByCode(command.UserAreaCode); var dbUserArea = await QueryUserArea(userArea).SingleOrDefaultAsync(); dbUserArea = AddUserAreaIfNotExists(userArea, dbUserArea); ValidateCommand(command, userArea); var isUnique = await _queryExecutor.ExecuteAsync(GetUniqueQuery(command, userArea), executionContext); ValidateIsUnique(isUnique, userArea); var newRole = await _userCommandPermissionsHelper.GetAndValidateNewRoleAsync(command.RoleId, null, command.UserAreaCode, executionContext); var user = MapAndAddUser(command, executionContext, newRole, userArea, dbUserArea); await _dbContext.SaveChangesAsync(); command.OutputUserId = user.UserId; }