コード例 #1
0
ファイル: OperationController.cs プロジェクト: Dahub/DaGetV2
        public IActionResult Put([FromHeader(Name = "username")] string userName, UpdateOperationDto toUpdateBankAccount)
        {
            using (var context = _contextFactory.CreateContext())
            {
                _operationService.Update(context, userName, toUpdateBankAccount);
            }

            return(Ok());
        }
コード例 #2
0
        public void Update(IContext context, string userName, UpdateOperationDto updateOperationDto)
        {
            var operationRepository     = context.GetRepository <Operation>();
            var operationTypeRepository = context.GetRepository <OperationType>();
            var bankAccountRepository   = context.GetRepository <BankAccount>();

            var operation = operationRepository.GetById(updateOperationDto.Id);

            if (operation == null)
            {
                throw new DaGetNotFoundException("Opération inconnue");
            }

            var bankAccount = bankAccountRepository.GetById(operation.BankAccountId);

            CheckIfUserCanAccesBankAccount(context, userName, bankAccount, true, true);

            var operationType = operationTypeRepository.GetById(updateOperationDto.OperationTypeId);

            if (operationType == null || !operationType.BankAccountId.Equals((bankAccount.Id)))
            {
                throw new DaGetNotFoundException("Type d'opération inconnue");
            }

            bankAccount.Balance -= operation.Amount;
            bankAccount.Balance += updateOperationDto.Amount;

            bankAccount.ActualBalance -= operation.Amount;
            bankAccount.ActualBalance += updateOperationDto.Amount;

            if (operation.IsClosed != updateOperationDto.IsClosed)
            {
                if (updateOperationDto.IsClosed)
                {
                    bankAccount.ActualBalance += updateOperationDto.Amount;
                }
                else
                {
                    bankAccount.ActualBalance -= updateOperationDto.Amount;
                }
            }

            operation.Amount          = updateOperationDto.Amount;
            operation.IsClosed        = updateOperationDto.IsClosed;
            operation.OperationDate   = updateOperationDto.OperationDate;
            operation.OperationTypeId = updateOperationDto.OperationTypeId;
            operation.Wording         = updateOperationDto.Wording;

            operationRepository.Update(operation);
            bankAccountRepository.Update(bankAccount);

            context.Commit();
        }
コード例 #3
0
        public async Task UpdateOperationAsync(int userId, UpdateOperationDto updateOperation)
        {
            ValidateOperationCategory(updateOperation.CategoryId, await GetUser(userId));

            var operationToUpdate = await UnitOfWork.OperationRepository.GetAsync(updateOperation.OperationId);

            ThrowIfNotUserOperation(operationToUpdate, GetUserWallets(userId));

            updateOperation.OperationDate = updateOperation.OperationDate.ToUniversalTime();
            Mapper.Map(updateOperation, operationToUpdate);

            UnitOfWork.OperationRepository.Update(operationToUpdate);
            await UnitOfWork.CommitAsync();
        }