示例#1
0
        public async Task <Result <Bill> > PutMoneyAsync(string userid, string currency, decimal cashvalue)
        {
            currency = currency?.ToUpper();

            var getBillResult = await GetBillAsync(userid, currency);

            if (getBillResult.Status == ResultEnum.Error)
            {
                return(new Result <Bill>(getBillResult.Status, getBillResult.Value, getBillResult.Description));
            }

            if (cashvalue <= 0)
            {
                return(new Result <Bill>(ResultEnum.Error, getBillResult.Value, "Invalid cashvalue"));
            }

            var result = getBillResult.Value;

            if (getBillResult.Status == ResultEnum.Fail)
            {
                var checkCurrency = await _currencyConverter.CheckCurrency(currency);

                if (!checkCurrency)
                {
                    return(new Result <Bill>(ResultEnum.Error, getBillResult.Value, "This currency not found in DB"));
                }
                var dbBill = await _dbContext.Bills.AddAsync(new Bill
                                                             { CurrencyName = currency, PurseId = userid, Cash = cashvalue });

                result = dbBill.Entity;
            }
            else
            {
                result.Cash += cashvalue;
            }

            await _dbContext.SaveChangesAsync();

            return(new Result <Bill>(ResultEnum.Success, result, string.Empty));
        }