public IHttpActionResult PutconfigCards(Guid id, configCards configCards)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            if (id != configCards.CardID)
            {
                return(BadRequest());
            }

            db.Entry(configCards).State = EntityState.Modified;

            try
            {
                db.SaveChanges();
            }
            catch (DbUpdateConcurrencyException)
            {
                if (!configCardsExists(id))
                {
                    return(NotFound());
                }
                else
                {
                    throw;
                }
            }

            return(StatusCode(HttpStatusCode.NoContent));
        }
Beispiel #2
0
        public IHttpActionResult PerformTransaction(string cardQR, int Total, int SupplierID, int password)
        {
            var cQR = Guid.Parse(cardQR);
            //return class contains password or not found if there is no password
            Messages msg = new Messages();

            using (db = new HajjCoinsModel())
            {
                configCards configCards = new configCards();
                var         CoinsCount  = db.configCards.Where(a => a.CardID == cQR).Select(a => a.NoOfCoins).FirstOrDefault();
                if (CoinsCount >= Total)
                {
                    //var coinsList = db.Coins.Where(a => a.CardID == cQR).ToList();

                    //var validCoins = (from coinsList in db.Coins

                    //                  join usedList in db.TransactionsCoinsSupplier
                    //                  on coinsList.CoinID equals usedList.CoinsID
                    //                  where db.TransactionsCoinsSupplier.Contains(usedList.CoinsID)
                    //                  select new { coinsList.CoinID }).ToList();
                    var trans_coinList = db.TransactionsCoinsSupplier.Select(x => x.CoinsID).ToArray();
                    var validCoins     = db.Coins.Where(p => !trans_coinList.Contains(p.CoinID)).ToList();


                    if (validCoins.Count > 0)
                    {
                        var cardCoin = db.configCards.Where(a => a.CardID == cQR).FirstOrDefault();
                        db.Entry(cardCoin).State = EntityState.Modified;
                        cardCoin.NoOfCoins      -= Total;
                        db.SaveChanges();

                        Transaction tr = new Transaction();
                        tr.TotalCoinsUsed  = Total;
                        tr.TransactionDate = System.DateTime.Now;
                        tr.SupplyerID      = SupplierID;
                        db.Transaction.Add(tr);
                        db.SaveChanges();

                        for (int i = 0; i < Total; i++)
                        {
                            TransactionsCoinsSupplier coin = new TransactionsCoinsSupplier();
                            coin.CoinsID       = validCoins[i].CoinID;
                            coin.TransactionID = tr.TransactionID;
                            db.TransactionsCoinsSupplier.Add(coin);
                        }

                        msg.message = Total + " hajjCoins successfully received";
                        msg.status  = true;

                        return(Ok(msg));
                    }
                    else
                    {
                        msg.message = "Your hajjCoins balance is not enough";
                        msg.status  = false;

                        return(Ok(msg));
                    }
                }
                else
                {
                    msg.message = "Your hajjCoins balance is not enough";
                    msg.status  = false;

                    return(Ok(msg));
                }
            }
        }