/// <summary> /// Konstruktor, amely a rendelés száma alapján feltölti a listát /// </summary> /// <param name="orderNumber">Rendelés azonosító</param> public RepositoryOrderItemsView(int orderNumber, List <Item> items, List <Pizza> pizzas) { roiv = new List <OrderItemsView>(); List <Item> iviews = items.FindAll(i => i.getOrderId() == orderNumber); finalPrice = 0; foreach (Item i in iviews) { Pizza pizza = pizzas.Find(p => p.getId() == i.getPizzaId()); finalPrice = finalPrice + i.getPiece() * pizza.getPrice(); OrderItemsView oiv = new OrderItemsView( orderNumber, i.getPiece(), pizza.getNeme(), pizza.getPrice() ); roiv.Add(oiv); } }
protected void Page_Load(object sender, EventArgs e) { // parse that query string! // ?restaurant=foo&delay=bar&food1=fish&qty1=1&food2=chips&qty2=1 string restaurant = Request.QueryString["restaurant"]; int delay = Convert.ToInt32(Request.QueryString["delay"]); // validate params //restaurant = validateString(restaurant); delay = Math.Max(delay, 20); // at least 20 delay = Math.Min(delay, 60); // at most 60 // create table to hold query results for our GridView DataTable foodTable = new DataTable("FoodTable"); DataTable orderTable = new DataTable("OrderTable"); DataColumn col; DataRow row; // build foodTable's columns // MenuItemName column col = new DataColumn(); col.DataType = Type.GetType("System.String"); col.ColumnName = "MenuItemName"; col.Caption = "Name"; col.ReadOnly = true; col.Unique = true; foodTable.Columns.Add(col); // Price column col = new DataColumn(); col.DataType = Type.GetType("System.String"); col.ColumnName = "Price"; col.Caption = "Price"; col.ReadOnly = true; col.Unique = false; foodTable.Columns.Add(col); // Quantity column col = new DataColumn(); col.DataType = Type.GetType("System.Int32"); col.ColumnName = "Quantity"; col.Caption = "Quantity"; col.ReadOnly = true; col.Unique = false; foodTable.Columns.Add(col); // Subtotal column col = new DataColumn(); col.DataType = Type.GetType("System.String"); col.ColumnName = "Subtotal"; col.Caption = "Subtotal"; col.ReadOnly = true; col.Unique = false; foodTable.Columns.Add(col); // build orderTable's columns // RestaurantName column col = new DataColumn(); col.DataType = Type.GetType("System.String"); col.ColumnName = "RestaurantName"; col.Caption = "Restaurant"; col.ReadOnly = true; col.Unique = false; orderTable.Columns.Add(col); // DelayTime column col = new DataColumn(); col.DataType = Type.GetType("System.Int32"); col.ColumnName = "DelayTime"; col.Caption = "Delivery Time"; col.ReadOnly = true; col.Unique = false; orderTable.Columns.Add(col); // Semitotal column col = new DataColumn(); col.DataType = Type.GetType("System.Decimal"); col.ColumnName = "Semitotal"; col.Caption = "Subtotal"; col.ReadOnly = true; col.Unique = false; orderTable.Columns.Add(col); // Taxes column col = new DataColumn(); col.DataType = Type.GetType("System.Decimal"); col.ColumnName = "Taxes"; col.Caption = "Tax"; col.ReadOnly = true; col.Unique = false; orderTable.Columns.Add(col); // ServiceCharge column col = new DataColumn(); col.DataType = Type.GetType("System.Decimal"); col.ColumnName = "ServiceCharge"; col.Caption = "Service Fee"; col.ReadOnly = true; col.Unique = false; orderTable.Columns.Add(col); // Total column col = new DataColumn(); col.DataType = Type.GetType("System.Decimal"); col.ColumnName = "Total"; col.Caption = "Total Cost"; col.ReadOnly = true; col.Unique = false; orderTable.Columns.Add(col); // keep track of price total double semiTotal = 0.0; // now the databasey stuff SqlConnection connection = new SqlConnection(ConfigurationManager.ConnectionStrings["OrderistaConnectionString"].ConnectionString); try { connection.Open(); // loop through all the food and qty pairs in query string, query the database for price // and insert the information into our foodTable // (we don't trust the query string to contain price as well because a user could hand-craft a string for free lunch) int i = 1; while (true) { string food = Request.QueryString["food" + i]; if (food == null) { break; // end of query params } int qty = Convert.ToInt32(Request.QueryString["qty" + i]); // validate params //food = validateString(food); qty = Math.Max(qty, 1); // minimum 1 // query database for menu item information SqlCommand cmd = new SqlCommand("SELECT Price FROM Menu_Items WHERE RestaurantName = @restaurant AND Name = @food"); cmd.Parameters.AddWithValue("@restaurant", restaurant); cmd.Parameters.AddWithValue("@food", food); cmd.Connection = connection; SqlDataAdapter sqlData = new SqlDataAdapter(cmd); DataTable temp = new DataTable(); sqlData.Fill(temp); // should only be one row due to RestaurantName + Name uniqueness if (temp.Rows.Count > 0) { double price = Math.Round(Convert.ToDouble(temp.Rows[0].ItemArray[0]), 2); // populate data table with menu item information and qty row = foodTable.NewRow(); row["MenuItemName"] = food; row["Price"] = price.ToString("c2"); row["Quantity"] = qty; row["Subtotal"] = (price * qty).ToString("c2"); foodTable.Rows.Add(row); // update running total for order semiTotal += price * qty; } else { // no such menu_item, no such code // I sincerely hope it wasn't peanut-butter and banana sandwiches // TODO hey front-end people! Do you want to display an error message? } // advance to check next parameter pair in query string i++; } // foodTable is full, now bind it to the OrderItemsView if (foodTable.Rows.Count > 0) { OrderItemsView.DataSource = foodTable; OrderItemsView.DataBind(); } } catch (SqlException ex) { string msg = "Fetch Error: "; msg += ex.Message; throw new Exception(msg); } finally { connection.Close(); } // pre-calculate values for orderTable semiTotal = Math.Round(semiTotal, 2); double tax = Math.Round(semiTotal * TAX_RATE, 2); double fee = calculateServiceCharge(semiTotal); double total = semiTotal + tax + fee; // order information row = orderTable.NewRow(); row["RestaurantName"] = restaurant; row["DelayTime"] = delay; row["Semitotal"] = semiTotal; row["Taxes"] = tax; row["ServiceCharge"] = fee; row["Total"] = total; orderTable.Rows.Add(row); // bind order information to the GridView OrderSummaryGridView.DataSource = orderTable; OrderSummaryGridView.DataBind(); } // Page_Load