Esempio n. 1
0
        public IActionResult Put([FromHeader(Name = "username")] string userName, UpdateBankAccountDto toUpdateBankAccount)
        {
            using (var context = _contextFactory.CreateContext())
            {
                _service.Update(context, userName, toUpdateBankAccount);
            }

            return(Ok());
        }
Esempio n. 2
0
 public IActionResult Update(UpdateBankAccountDto updateBankAccountDto)
 {
     try
     {
         var bankAccountDto = _bankAccountService.Update(updateBankAccountDto);
         return(Ok(bankAccountDto));
     }
     catch (Exception e)
     {
         return(BadRequest(e.Message));
     }
 }
Esempio n. 3
0
        public GetBankAccountDto Update(UpdateBankAccountDto bankAcc)
        {
            var bankAccountInDb = _unitOfWork.BankAccountRepository.Get(bankAcc.Id);

            if (bankAccountInDb == null)
            {
                throw new Exception("Not Found");
            }

            bankAccountInDb.AccountNumber = bankAcc.AccountNumber;
            bankAccountInDb.BranchId      = bankAcc.BranchId;
            bankAccountInDb.Code          = bankAcc.Code;
            bankAccountInDb.Type          = bankAcc.Type;
            bankAccountInDb.CreateDate    = DateTime.Now;

            _unitOfWork.BankAccountRepository.Update(bankAccountInDb);
            _unitOfWork.SaveChanges();
            return(_mapper.Map <GetBankAccountDto>(bankAccountInDb));
        }
Esempio n. 4
0
        public void Update(IContext context, string userName, UpdateBankAccountDto toEditBankAccount)
        {
            Validate(toEditBankAccount);

            var bankAccountRepository     = context.GetRepository <BankAccount>();
            var operationTypeRepository   = context.GetRepository <OperationType>();
            var bankAccountTypeRepository = context.GetRepository <BankAccountType>();
            var operationRepository       = context.GetRepository <Operation>();

            var bankAccount = bankAccountRepository.GetById(toEditBankAccount.Id.Value);

            CheckIfUserCanAccesBankAccount(context, userName, bankAccount, true);

            var bankAccountType = bankAccountTypeRepository.GetById(toEditBankAccount.BankAccountTypeId.Value);

            if (bankAccountType == null)
            {
                throw new DaGetNotFoundException("Type de compte en banque inconnu");
            }

            if (toEditBankAccount.InitialBalance.HasValue && bankAccount.OpeningBalance != toEditBankAccount.InitialBalance.Value)
            {
                var delta = toEditBankAccount.InitialBalance.Value - bankAccount.OpeningBalance;

                bankAccount.OpeningBalance = toEditBankAccount.InitialBalance.Value;
                bankAccount.Balance       += delta;
                bankAccount.ActualBalance += delta;
            }

            bankAccount.ModificationDate  = DateTime.Now;
            bankAccount.BankAccountTypeId = toEditBankAccount.BankAccountTypeId.Value;
            bankAccount.Wording           = toEditBankAccount.Wording;

            // manage operations types
            var operationTypes = operationTypeRepository.List(new OperationTypeByBankAccountIdSpecification(bankAccount.Id));

            var toDeleteOperationsTypes = operationTypes.Where(ot => !(toEditBankAccount.OperationsTypes.Where(eot => eot.Key.HasValue).Select(eot => eot.Key.Value).Contains(ot.Id)));

            var toUpdateOperationsTypes = operationTypes.Where(ot => toEditBankAccount.OperationsTypes.Where(eot => eot.Key.HasValue).Select(eot => eot.Key.Value).Contains(ot.Id));

            var newOperationsTypes = toEditBankAccount.OperationsTypes.Where(eot => !eot.Key.HasValue).Select(eot =>
                                                                                                              new OperationType()
            {
                BankAccountId    = bankAccount.Id,
                CreationDate     = DateTime.Now,
                ModificationDate = DateTime.Now,
                Id      = Guid.NewGuid(),
                Wording = eot.Value
            });

            foreach (var newOperationType in newOperationsTypes)
            {
                operationTypeRepository.Add(newOperationType);
            }

            foreach (var toUpdateOperationType in toUpdateOperationsTypes)
            {
                var newWording = toEditBankAccount.OperationsTypes.
                                 Where(eot => eot.Key.HasValue && eot.Key.Value.Equals(toUpdateOperationType.Id)).
                                 Select(eot => eot.Value).Single();

                if (toUpdateOperationType.Wording != newWording)
                {
                    toUpdateOperationType.Wording          = newWording;
                    toUpdateOperationType.ModificationDate = DateTime.Now;

                    operationTypeRepository.Update(toUpdateOperationType);
                }
            }

            foreach (var toDeleteOperationType in toDeleteOperationsTypes)
            {
                if (operationRepository.List(new FirstOperationByOperationTypeIdSpecification(toDeleteOperationType.Id)).Any())
                {
                    throw new DaGetServiceException($"Le type d'opération {toDeleteOperationType.Wording} possède une ou plusieurs opérations associées");
                }

                operationTypeRepository.Delete(toDeleteOperationType);
            }

            context.Commit();
        }