/// <summary>
        /// Creates a transaction item for each product and associates that with a vending transaction
        /// </summary>
        /// <param name="db">Database interface used to create the data</param>
        public static void PopulateDatabaseWithTransactions(IVendingService db)
        {
            using (TransactionScope scope = new TransactionScope())
            {
                // Get a list of products and users
                List <ProductItem> products = db.GetProductItems();
                List <UserItem>    users    = db.GetUserItems();

                VendingTransaction vendTrans = new VendingTransaction()
                {
                    Date   = DateTime.UtcNow,
                    UserId = users[0].Id
                };
                vendTrans.Id = db.AddVendingTransaction(vendTrans);

                foreach (ProductItem prodItem in products)
                {
                    // Add Transaction Item
                    TransactionItem item = new TransactionItem()
                    {
                        SalePrice            = prodItem.Price,
                        VendingTransactionId = vendTrans.Id,
                        ProductId            = prodItem.Id
                    };
                    item.Id = db.AddTransactionItem(item);
                }

                scope.Complete();
            }
        }
Exemplo n.º 2
0
        public void CommitTransaction()
        {
            VendingTransaction vendTrans = new VendingTransaction();

            vendTrans.Date = DateTime.UtcNow;

            _db.AddTransactionSet(vendTrans, _transactionItems);
        }
        public void PostSingle([FromBody] VendingTransactionViewModel value)
        {
            VendingTransaction transaction = new VendingTransaction();

            transaction.UserId = value.UserId;
            transaction.Date   = value.Date;

            _db.AddVendingTransaction(transaction);
        }
Exemplo n.º 4
0
        private VendingTransaction GetVendingTransactionFromReader(SqlDataReader reader)
        {
            VendingTransaction item = new VendingTransaction();

            item.Id   = Convert.ToInt32(reader["Id"]);
            item.Date = Convert.ToDateTime(reader["Date"]);

            return(item);
        }
        public int AddTransactionSet(VendingTransaction vendTrans, List <TransactionItem> transItems)
        {
            int vendTransId = AddVendingTransaction(vendTrans.Clone());

            foreach (var item in transItems)
            {
                TransactionItem newItem = item.Clone();
                newItem.VendingTransactionId = vendTransId;
                AddTransactionItem(newItem);
            }
            return(vendTransId);
        }
        public VendingTransaction GetVendingTransaction(int id)
        {
            VendingTransaction item = null;

            if (_vendingTransactions.ContainsKey(id))
            {
                item = _vendingTransactions[id];
            }
            else
            {
                throw new Exception("Item does not exist.");
            }

            return(item.Clone());
        }
Exemplo n.º 7
0
        public int AddVendingTransaction(VendingTransaction item)
        {
            const string sql = "INSERT VendingTransaction (Date) VALUES (@Date);";

            using (SqlConnection conn = new SqlConnection(_connectionString))
            {
                conn.Open();

                SqlCommand cmd = new SqlCommand(sql + _getLastIdSQL, conn);
                cmd.Parameters.AddWithValue("@Date", item.Date);
                item.Id = (int)cmd.ExecuteScalar();
            }

            return(item.Id);
        }
Exemplo n.º 8
0
        public int AddTransactionSet(VendingTransaction vendTrans, List <TransactionItem> transItems)
        {
            int vendId = BaseItem.InvalidId;

            using (TransactionScope scope = new TransactionScope())
            {
                int vendTransId = AddVendingTransaction(vendTrans);
                foreach (var item in transItems)
                {
                    item.VendingTransactionId = vendTransId;
                    AddTransactionItem(item);
                }
            }

            return(vendId);
        }
        /// <summary>
        /// Handles all purchase transactions. Writes a transaction to the database and an entry
        /// into the log file. This also updates the running total property for the vending machine.
        /// </summary>
        /// <param name="productId">The id of the product being purchased</param>
        /// <param name="userId">The id of the user purchasing the product</param>
        /// <returns>The updated running total after the purchase has been handled.</returns>
        public double AddPurchaseTransaction(int productId, int userId)
        {
            ProductItem product = _db.GetProductItem(productId);

            if (product.Price > RunningTotal)
            {
                throw new Exception("More money is required.");
            }

            TransactionItem transactionItem = new TransactionItem();

            transactionItem.ProductId = productId;
            transactionItem.SalePrice = product.Price;

            if (_vendingTransactionId == BaseItem.InvalidId)
            {
                VendingTransaction vendTrans = new VendingTransaction();
                vendTrans.Date   = DateTime.UtcNow;
                vendTrans.UserId = userId;

                List <TransactionItem> transactionItems = new List <TransactionItem>();
                transactionItems.Add(transactionItem);

                _vendingTransactionId = _db.AddTransactionSet(vendTrans, transactionItems);
            }
            else
            {
                transactionItem.VendingTransactionId = _vendingTransactionId;
                _db.AddTransactionItem(transactionItem);
            }

            VendingOperation operation = new VendingOperation();

            operation.OperationType = VendingOperation.eOperationType.PurchaseItem;
            operation.Price         = product.Price;
            operation.RunningTotal  = RunningTotal;
            operation.ProductName   = product.Name;
            operation.ProductId     = product.Id;
            operation.UserId        = userId;
            _log.LogOperation(operation);

            RunningTotal -= product.Price;

            return(RunningTotal);
        }
        public void TestVendingTransaction()
        {
            // Test add vending transaction
            VendingTransaction item = new VendingTransaction();

            item.Date   = DateTime.UtcNow;
            item.UserId = _userId1;
            int id = _db.AddVendingTransaction(item);

            Assert.AreNotEqual(0, id);

            VendingTransaction itemGet = _db.GetVendingTransaction(id);

            Assert.AreEqual(item.Id, itemGet.Id);
            Assert.AreEqual(item.UserId, itemGet.UserId);
            Assert.AreEqual(item.Date.ToString(), itemGet.Date.ToString());

            // Test get vending transactions
            item        = new VendingTransaction();
            item.Date   = DateTime.UtcNow;
            item.UserId = _userId2;
            int id2 = _db.AddVendingTransaction(item);

            Assert.AreNotEqual(0, id2);

            var  vendingItems = _db.GetVendingTransactions();
            bool foundItem1   = false;
            bool foundItem2   = false;

            foreach (var vendingItem in vendingItems)
            {
                if (vendingItem.Id == id)
                {
                    foundItem1 = true;
                }
                else if (vendingItem.Id == id2)
                {
                    foundItem2 = true;
                }
            }
            Assert.IsTrue(foundItem1);
            Assert.IsTrue(foundItem2);
        }
        public void PostList([FromBody] VendingTransactionViewModel value)
        {
            VendingTransaction transaction = new VendingTransaction();

            transaction.UserId = value.UserId;
            transaction.Date   = value.Date;

            List <TransactionItem> transItems = new List <TransactionItem>();

            foreach (TransactionItemViewModel item in value.TransItems)
            {
                TransactionItem tranItem = new TransactionItem();
                tranItem.ProductId = item.ProductId;
                tranItem.SalePrice = item.SalePrice;
                transItems.Add(tranItem);
            }

            _db.AddTransactionSet(transaction, transItems);
        }
Exemplo n.º 12
0
        public VendingTransaction GetVendingTransaction(int id)
        {
            VendingTransaction item = new VendingTransaction();
            const string       sql  = "SELECT * FROM VendingTransaction WHERE Id = @Id;";

            using (SqlConnection conn = new SqlConnection(_connectionString))
            {
                conn.Open();
                SqlCommand cmd = new SqlCommand(sql, conn);
                cmd.Parameters.AddWithValue("@Id", id);
                var reader = cmd.ExecuteReader();

                while (reader.Read())
                {
                    item = GetVendingTransactionFromReader(reader);
                }
            }

            return(item);
        }
 public int AddVendingTransaction(VendingTransaction item)
 {
     item.Id = _vendingTransactionId++;
     _vendingTransactions.Add(item.Id, item.Clone());
     return(item.Id);
 }