コード例 #1
0
        public async Task PutAsync(EmployeeDto employeeDto, ClaimsPrincipal admin)
        {
            var employee = await _context.Employee.FindAsync(employeeDto.EmployeeId);

            if (employee == null)
            {
                throw new InvalidOperationException();
            }

            var user = await _usersService.FindByEmailAsync(employee.WorkEmail);

            if (user == null)
            {
                throw new InvalidOperationException();
            }

            _context.Entry(employee).State = EntityState.Detached;

            var newBalance = (employeeDto.Balance ?? CulcBalance()) - employee.Balance;

            var newEmployee = _mapper.Map <EmployeeDto, Employee>(employeeDto);

            newEmployee.Balance = employeeDto.Balance ?? CulcBalance();

            newEmployee.ImgUrl = await _imagesService.GetUrlAsync(employeeDto.ImgUrl);

            using (var scope = new TransactionScope(TransactionScopeAsyncFlowOption.Enabled))
            {
                _context.Employee.Update(newEmployee);

                user.UserName = employeeDto.WorkEmail;

                user.Email = employeeDto.WorkEmail;

                await _transactionService.CreateTransactionByAdminAsync(newEmployee.EmployeeId, newBalance, admin);

                await _usersService.UpdateUser(user);

                await _usersService.UpdateUserRole(user, employeeDto.RoleId);

                await _context.SaveChangesAsync();

                scope.Complete();
            }
        }