Ejemplo n.º 1
0
        public async Task <IActionResult> PutTransaction(int id, Transaction transaction)
        {
            if (id != transaction.TxnId)
            {
                return(BadRequest());
            }

            _context.Entry(transaction).State = EntityState.Modified;

            try
            {
                await _context.SaveChangesAsync();
            }
            catch (DbUpdateConcurrencyException)
            {
                if (!TransactionExists(id))
                {
                    return(NotFound());
                }
                else
                {
                    throw;
                }
            }

            return(NoContent());
        }
Ejemplo n.º 2
0
 private static Task HandleTimer(IQueryable <DepositCard> depositCards, IQueryable <CreditCard> creditCards, IQueryable <Transaction> transactions)
 {
     if (depositCards.LongCount() > 0)
     {
         Console.WriteLine($"deposit cards were updated at {DateTime.Now.ToString(CultureInfo.CurrentCulture)}");
         UpdateDeposit(depositCards);
         _context.SaveChangesAsync();
     }
     if (creditCards.LongCount() > 0)
     {
         Thread.Sleep(1000);
         Console.WriteLine($"credit cards were updated at {DateTime.Now.ToString(CultureInfo.CurrentCulture)}");
         UpdateCredit(creditCards, transactions);
         _context.SaveChangesAsync();
     }
     return(Task.CompletedTask);
 }
Ejemplo n.º 3
0
        public async Task <ActionResult> SetWithdrawn()
        {
            string body;

            using (var stream = new StreamReader(HttpContext.Request.Body))
            {
                body = await stream.ReadToEndAsync();
            }
            var myObject = Newtonsoft.Json.JsonConvert.DeserializeObject <Dictionary <string, string> >(body);
            int atmId    = Int32.Parse(myObject["atmId"]);
            int amount   = Int32.Parse(myObject["amount"]);

            var atm = await _context.Atm.SingleOrDefaultAsync(m => m.AtmId == atmId);

            if (atm != null)
            {
                atm.RemainingMoney -= amount;
                await _context.SaveChangesAsync();

                return(new OkObjectResult(new { Ok = true, Remaining = atm.RemainingMoney }));
            }

            return(new OkObjectResult(new { Ok = false }));
        }
Ejemplo n.º 4
0
        public async Task <ActionResult> Withdraw()
        {
            string body;

            using (var stream = new StreamReader(HttpContext.Request.Body))
            {
                body = await stream.ReadToEndAsync();
            }
            var     myObject = Newtonsoft.Json.JsonConvert.DeserializeObject <dynamic>(body);
            string  cardNum  = Convert.ToString(myObject.cardNum);
            decimal amount   = Convert.ToDecimal(myObject.amount);

            if (!IsAuthenticated(cardNum))
            {
                return(Unauthorized());
            }

            try
            {
                if (_context.CheckingCard.FirstOrDefault(c => c.CardNum == cardNum) != null)
                {
                    var         card        = _context.CheckingCard.FirstOrDefault(c => c.CardNum == cardNum);
                    Transaction transaction = new Transaction
                    {
                        TypeOfTxn     = 0,
                        Amount        = amount,
                        DatetimeOfTxn = DateTime.Now,
                        CardSenderNum = card.CardNum
                    };
                    _context.Transaction.Add(transaction);
                    await _context.SaveChangesAsync();

                    if (card.Balance < amount)
                    {
                        await ConfirmWithdraw(card.CardNum, transaction.TxnId, false);

                        return(new OkObjectResult(new { Ok = true, Allowed = false, Errors = new[] { "Not enough money" } }));
                    }
                    else
                    {
                        return(new OkObjectResult(new { Ok = true, Allowed = true, TxnId = transaction.TxnId }));
                    }
                }

                if (_context.CreditCard.FirstOrDefault(c => c.CardNum == cardNum) != null)
                {
                    var card = await _context.CreditCard.FirstOrDefaultAsync(c => c.CardNum == cardNum);

                    var transaction = new Transaction
                    {
                        TypeOfTxn     = 0,
                        Amount        = amount,
                        DatetimeOfTxn = DateTime.Now,
                        CardSenderNum = card.CardNum
                    };
                    _context.Transaction.Add(transaction);
                    await _context.SaveChangesAsync();

                    if (card.Balance < amount)
                    {
                        await ConfirmWithdraw(card.CardNum, transaction.TxnId, false);

                        new OkObjectResult(new { Ok = true, Allowed = false, Errors = new[] { "Not enough money" } });
                    }
                    else
                    {
                        return(new OkObjectResult(new { Ok = true, Allowed = true, transaction.TxnId }));
                    }
                }

                if (_context.DepositCard.FirstOrDefault(c => c.CardNum == cardNum) != null)
                {
                    var card        = _context.DepositCard.FirstOrDefault(c => c.CardNum == cardNum);
                    var transaction = new Transaction
                    {
                        TypeOfTxn     = 0,
                        Amount        = amount,
                        DatetimeOfTxn = DateTime.Now,
                        CardSenderNum = card.CardNum
                    };
                    _context.Transaction.Add(transaction);
                    await _context.SaveChangesAsync();

                    if (card.EndDeposit >= DateTime.Now)
                    {
                        if (card.Balance < amount)
                        {
                            await ConfirmWithdraw(card.CardNum, transaction.TxnId, false);

                            return(new OkObjectResult(new { Ok = true, Allowed = false, Errors = new[] { "Not enough money" } }));
                        }
                        return(new OkObjectResult(new { Ok = true, Allowed = true, Errors = new[] { "Discarding all benefits" } }));
                    }

                    if (card.TotalBalance < amount)
                    {
                        await ConfirmWithdraw(card.CardNum, transaction.TxnId, false);

                        return(new OkObjectResult(new { Ok = true, Allowed = false, Errors = new[] { "Not enough money" } }));
                    }

                    return(new OkObjectResult(new { Ok = true, Allowed = true }));
                }

                return(new OkObjectResult(new { Ok = false, Allowed = false, Errors = new[] { "Card not found" } }));
            }
            catch (Exception exc)
            {
                var res = new WithdrawResult {
                    Ok = false, Allowed = false, Errors = new List <string> {
                        exc.Message
                    }
                };
                return(new ObjectResult(res));
            }
        }