public ActionResult AddProductToShoppingbag(ulong productId) { CheckSession(); if (TempData["ErrorMessage"] != null) { ViewBag.Error = TempData["ErrorMessage"].ToString(); } bool amountChanged = false; Session ses = ((Session)this.Session["__MySessionObject"]); foreach (OrderLine ol in ses.ShoppingBag.OrderLines) { if (ol.Product.Id == productId) { ol.Amount++; amountChanged = true; } } if (!amountChanged) { using (DatabaseQuery query = new DatabaseQuery()) { Product product = query.GetProduct(productId); OrderLine ol = new OrderLine(); ol.Product = product; ol.Amount = 1; ses.ShoppingBag.OrderLines.Add(ol); } } this.Session["__MySessionObject"] = ses; return RedirectToAction("Products", "Home"); }
public ActionResult Manage() { try { ManagerViewModel viewModel = new ManagerViewModel(); DatabaseQuery dbq = new DatabaseQuery(); viewModel.Orders = dbq.GetOrders(); viewModel.Orderlines = dbq.GetOrderlines(); viewModel.OrderlinesAsc = dbq.GetOrderlines(); List<Product> products = dbq.GetProducts(); foreach (Product p in products) { if (viewModel.Orderlines.Any(ol => ol.Product.Id == p.Id)) { continue; } OrderLine o = new OrderLine { Product = p, Amount = 0 }; viewModel.OrderlinesAsc.Add(o); } viewModel.OrderlinesAsc.Reverse(); //take first 10 items from list viewModel.OrderlinesAsc.Take(10); viewModel.Orderlines.Take(10); return View(viewModel); } catch (Exception e) { ViewBag.Error = "Er is iets fout gegaan met het ophalen van de producten: " + e; return View(); } //return RedirectToAction("View", "Home"); //Return naar de Homepage }
internal List<OrderLine> getOrderlines(ulong orderId) { using (NpgsqlCommand cmd = new NpgsqlCommand()) { cmd.Connection = this._connection; cmd.CommandText = String.Format(@"SELECT * FROM orderline where order_id={0};", (long)orderId); NpgsqlDataReader reader = cmd.ExecuteReader(); List<OrderLine> orderlines = new List<OrderLine>(); while (reader.Read()) { OrderLine orderline = new OrderLine(); orderline.Product = this.GetProduct((ulong)reader.GetInt64(reader.GetOrdinal("product_id"))); orderline.Amount = (ulong)reader.GetInt64(reader.GetOrdinal("amount")); orderlines.Add(orderline); } reader.Close(); return orderlines; } }
internal bool CreateOrderline(OrderLine ol, ulong orderId) { using (NpgsqlCommand cmd = new NpgsqlCommand()) { cmd.Connection = _connection; _transaction = cmd.Connection.BeginTransaction(); cmd.CommandText = "INSERT INTO orderline(product_id, order_id, amount)" + "VALUES(@product_id, @order_id, @amount);"; cmd.Parameters.AddWithValue("product_id", (long)ol.Product.Id); cmd.Parameters.AddWithValue("order_id", (long)orderId); cmd.Parameters.AddWithValue("amount", (long)ol.Amount); bool success = parseNonqueryResult(cmd.ExecuteNonQuery()); if (success) { _transaction.Commit(); _transaction.Dispose(); return success; //Commit als het sucessvol is } _transaction.Rollback(); _transaction.Dispose(); return success; } }
internal List<OrderLine> GetOrderlines() { using (NpgsqlCommand cmd = new NpgsqlCommand()) { cmd.Connection = this._connection; cmd.CommandText = "SELECT product_id, sum(amount) as NumberOfOrders FROM orderline group by product_id order by NumberOfOrders desc;"; NpgsqlDataReader reader = cmd.ExecuteReader(); List<OrderLine> orderlines = new List<OrderLine>(); while (reader.Read()) { OrderLine orderline = new OrderLine(); orderline.Product = this.GetProduct((ulong)reader.GetInt64(reader.GetOrdinal("product_id"))); orderline.Amount = (ulong)reader.GetDecimal(reader.GetOrdinal("NumberOfOrders")); orderlines.Add(orderline); } reader.Close(); return orderlines; } }