Esempio n. 1
0
        public async Task <Result <Account> > RegisterAccountAsync(int customerId, RegisterAccountOptions options)
        {
            if (string.IsNullOrWhiteSpace(options.Currency))
            {
                return new Result <Account>()
                       {
                           Code    = ResultCodes.BadRequest,
                           Message = $"Currency is empty!"
                       }
            }
            ;

            if (string.IsNullOrWhiteSpace(options.Description))
            {
                return new Result <Account>()
                       {
                           Code    = ResultCodes.BadRequest,
                           Message = $"Einai ipoxreotiko na doseis perigrafi"
                       }
            }
            ;


            var customer = (await _customer.GetCustomerByIdAsync(customerId)).Data;

            if (customer != null)
            {
                var account = new Account()
                {
                    Currency    = options.Currency,
                    Description = options.Description
                };

                customer.Accounts.Add(account);

                _dBContext.Update <Customer>(customer);
                await _dBContext.SaveChangesAsync();

                return(new Result <Account>()
                {
                    Code = ResultCodes.Success,
                    Data = account
                });
            }
            else
            {
                return(new Result <Account>()
                {
                    Code = ResultCodes.BadRequest,
                    Message = $"Den iparxei o pelatis me kodiko {customerId}."
                });
            }
        }
        public async Task <Result <Transaction> > RegisterTranactionAsync(int accountId, RegisterTransactionOptions options)
        {
            if (options.Amount < 0)
            {
                return new Result <Transaction>()
                       {
                           Code    = ResultCodes.BadRequest,
                           Message = $"To poso pou edoses einai < 0."
                       }
            }
            ;

            var account = (await _account.GetAccountByIdAsync(accountId)).Data;

            if (options.Type == TransactionType.Debit)
            {
                if (options.Amount > account.Balance)
                {
                    return(new Result <Transaction>()
                    {
                        Code = ResultCodes.BadRequest,
                        Message = $"Thes na xreoseis {options.Amount} evro eno o logariamos exei {account.Balance}. Akiro."
                    });
                }
            }

            if (account != null)
            {
                var transaction = new Transaction()
                {
                    Type   = options.Type,
                    Amount = options.Amount
                };



                account.Transactions.Add(transaction);

                account.Balance = account.Balance + ((int)options.Type) * options.Amount;

                _dBContext.Update <Account>(account);
                await _dBContext.SaveChangesAsync();

                return(new Result <Transaction>()
                {
                    Code = ResultCodes.Success,
                    Data = transaction
                });
            }
            else
            {
                return(new Result <Transaction>()
                {
                    Code = ResultCodes.BadRequest,
                    Message = $"Den vrethike account me kodiko {accountId}"
                });
            }
        }