/// <summary> /// AddFoodStore(Food_Stores foodStore) adds the foodStore to the Food_Stores table /// </summary> /// <param name="foodStore"></param> /// <returns>integer value indicating the Food_Store_Id of the added foodStore</returns> public bool AddFoodStore(Food_Store foodStore) { try { //instantiating Online_Food_Ordering_SystemEntities Context class using (Online_Food_Ordering_SystemEntities1 db = new Online_Food_Ordering_SystemEntities1()) { //use LINQ query to Add Food Stores from table Food_Stores db.Food_Stores.Add(foodStore); //save changes to the database db.SaveChanges(); return(true); } } catch (Exception ex) { //throw user defined FoodOrderException throw new FoodOrderException(ex.Message); } }
/// <summary> /// Method GetAllFoodItems() to get a list of all the Food Items /// </summary> /// <returns>List of Food Items</returns> public List <Food_Item> GetAllFoodItems() { //instantiating Online_Food_Ordering_SystemEntities3 Context class try { using (Online_Food_Ordering_SystemEntities1 db = new Online_Food_Ordering_SystemEntities1()) { //use LINQ query to fetch list of Food Items from table Food_Items List <Food_Item> foodItemsList = db.usp_GetFoodItemDetails().ToList(); //assigning lazy loading to be false db.Configuration.LazyLoadingEnabled = false; //return obtained data return(foodItemsList); } } catch (Exception ex) { //throw user defined FoodOrderException throw new FoodOrderException(ex.Message); } }
/// <summary> /// Method GetAllEmployees() to get a list of all the Employees /// </summary> /// <returns>List of Employee</returns> public List <Employee> GetAllEmployees() { //instantiating Online_Food_Ordering_SystemEntities1 Context class try { using (Online_Food_Ordering_SystemEntities1 db = new Online_Food_Ordering_SystemEntities1()) { //use LINQ query to fetch list of employees from table Employees List <Employee> employeeList = db.Employees.Where(f => f.IsActive == true).ToList(); //assigning lazy loading to be false db.Configuration.LazyLoadingEnabled = false; //return obtained data return(employeeList); } } catch (Exception ex) { //throw user defined EmployeeException throw new EmployeeException(ex.Message); } }
/// <summary> /// AddEmployee(Employees employee) adds the employee to the Employees table /// </summary> /// <param name="employee">Employee type value that is to be added</param> /// <returns>bool value indicating the employeeId of the added employee</returns> public bool AddEmployee(Employee employee) { try { //instantiating Online_Food_Ordering_SystemEntities3 Context class using (Online_Food_Ordering_SystemEntities1 db = new Online_Food_Ordering_SystemEntities1()) { //check if the employee already exists Employee item = db.Employees.Where(f => f.Email.Equals(employee.Email, StringComparison.OrdinalIgnoreCase) && f.IsActive == true).FirstOrDefault(); if (item != null) { //if exists then throw exception throw new FoodOrderException("Employee already present"); } //set IsActive to true employee.IsActive = true; //set Creation Date to be the employee.Creation_Date = DateTime.Now; //use LINQ query to Add Employee to table Employees db.Employees.Add(employee); //save changes to the database db.SaveChanges(); return(true); } } catch (Exception ex) { //throw user defined EmployeeException throw new EmployeeException(ex.Message); } }
/// <summary> /// Method updates or edits the changes of the passed foodItem in the Food_Items table /// </summary> /// <param name="foodItem">object of type FoodItem </param> /// <returns>boolean value indicating whether fooItem is updated or not</returns> public bool UpdateFoodItem(Food_Item foodItem) { try { //instantiating Online_Food_Ordering_SystemEntities3 Context class using (Online_Food_Ordering_SystemEntities1 db = new Online_Food_Ordering_SystemEntities1()) { //use LINQ query to find the Food Item with id foodItem.Food_Item_Id Food_Item item = db.Food_Items.Where(f => f.Food_Item_Id == foodItem.Food_Item_Id && f.IsActive == true).FirstOrDefault(); if (item != null) { //update Food Item details item.Food_Name = foodItem.Food_Name; item.Food_Type = foodItem.Food_Type; item.Price = foodItem.Price; //save changes to the database db.SaveChanges(); return(true); } else { throw new FoodOrderException("Food item does not exist"); } } } catch (Exception ex) { //throw user defined FoodOrderException throw new FoodOrderException(ex.Message); } }
/// <summary> /// Provides history of all previous orders of a specific customer /// </summary> /// <param name="customerId">integer value to uniquely identify the customer</param> /// <param name="fromEntryNo">integer value that will query the database from which entry number to begin from</param> /// <param name="toEntryNo">integer value that will query the database till which entry number to get record</param> /// <returns>List of all the past orders</returns> public List <Order> GetOrdersByCustomerId(int customerId, int fromEntryNo, int toEntryNo) { List <Order> OrdersList = new List <Order>(); using (Online_Food_Ordering_SystemEntities1 db = new Online_Food_Ordering_SystemEntities1()) { try { int totalOrdersCount = db.Orders.Select(o => o.isActive && o.Customer_Id == customerId && o.Submit_Status).Count(); if (fromEntryNo > totalOrdersCount) { throw new FoodOrderException($"Not enough entries to start from {fromEntryNo}"); } if (toEntryNo > totalOrdersCount) { toEntryNo = totalOrdersCount; } IQueryable <Order> OrdersQuery = db.Orders.Where(o => o.isActive && o.Customer_Id == customerId && o.Submit_Status); if (OrdersQuery != null) { foreach (Order singleOrder in OrdersQuery) { Order orderToAdd = new Order() { Customer_Id = singleOrder.Customer_Id, Order_date = singleOrder.Order_date, Order_Id = singleOrder.Order_Id, Employee_Id = singleOrder.Employee_Id, Food_Store_Id = singleOrder.Food_Store_Id, Total_Price = singleOrder.Total_Price, Total_Quantity = singleOrder.Total_Quantity, isActive = singleOrder.isActive, Submit_Status = singleOrder.Submit_Status }; List <Order_Item> orderItemsQuery = db.Order_Items.Where(item => item.Order_Id == orderToAdd.Order_Id).ToList(); List <Order_Item> orderItems = new List <Order_Item>(); if (orderItemsQuery != null) { foreach (Order_Item singleItem in orderItemsQuery) { var currentItemFoodObject = db.Food_Items.FirstOrDefault(fitem => fitem.Food_Item_Id == singleItem.Food_Item_Id); Food_Item food = currentItemFoodObject != null ? new Food_Item() { Food_Name = currentItemFoodObject.Food_Name, Food_Type = currentItemFoodObject.Food_Type, ImagePath = currentItemFoodObject.ImagePath } : null; orderItems.Add(new Order_Item() { Order_Id = singleItem.Order_Id, Food_Item_Id = singleItem.Food_Item_Id, Food_Items = food, Price = singleItem.Price, Quantity = singleItem.Quantity }); } } orderToAdd.Order_Items = orderItems; OrdersList.Add(orderToAdd); } } } catch { throw new FoodOrderException("Not able to fetch Order List"); } OrdersList.Reverse(); return(OrdersList); } }
/// <summary> /// Updates /// </summary> /// <param name="customerId"></param> /// <param name="foodItemId"></param> /// <param name="foodItemQuantity"></param> /// <returns></returns> public bool UpdateCart(int customerId, ICollection <Order_Item> orderItems) { using (Online_Food_Ordering_SystemEntities1 db = new Online_Food_Ordering_SystemEntities1()) { bool isUpdated = false; try { //find if customer exists, if not then: if (db.Customers.Where(c => c.Customer_Id == customerId).Count() == 0) { throw new FoodOrderException("Customer Doesn't Exist"); } //find cart for given customer Order cart = db.Orders.FirstOrDefault(o => (o.isActive && o.Customer_Id == customerId && o.Submit_Status == false)); //if cart doesn't exist for customer int finalTotalPrice = 0; int finalTotalQuantity = 0; if (cart == null) { foreach (Order_Item o in orderItems) { //setting the latest price of the product in the cart decimal foodItemPrice = db.Food_Items.FirstOrDefault(item => item.IsActive && item.Food_Item_Id == o.Food_Item_Id).Price; o.Price = foodItemPrice; if (o.Quantity < 1) { orderItems.Remove(o); } } Order newCart = new Order() { Customer_Id = customerId, Order_date = DateTime.Now, Employee = null, Order_Items = orderItems, Food_Store_Id = null, Submit_Status = false, Creation_Date = DateTime.Now, isActive = true }; cart = newCart; db.Orders.Add(newCart); } //else if cart exists for customer else { foreach (Order_Item orderItem in orderItems) { //find latest price of given orderItem Food_Item foodItemForPrice = db.Food_Items.FirstOrDefault(item => item.IsActive && item.Food_Item_Id == orderItem.Food_Item_Id); if (foodItemForPrice == null) { continue; } decimal foodItemPrice = foodItemForPrice.Price; //find if item already exist in cart Order_Item existingOrderItem = db.Order_Items.FirstOrDefault(i => i.Order_Id == cart.Order_Id && i.Food_Item_Id == orderItem.Food_Item_Id); //if item doesn't already exist in the cart add new item if (existingOrderItem == null) { if (orderItem.Quantity > 0) { db.Order_Items.Add(new Order_Item() { Order_Id = cart.Order_Id, Food_Item_Id = orderItem.Food_Item_Id, Quantity = orderItem.Quantity, Price = foodItemPrice }); } } //else if item exists in the cart update quantity and latest price else { existingOrderItem.Quantity = orderItem.Quantity; existingOrderItem.Price = foodItemPrice; } } } db.SaveChanges(); //Updating Total_Quantity and Total_Price in the db IQueryable <Order_Item> orderItemList = db.Order_Items.Where(item => item.Order_Id == cart.Order_Id); foreach (Order_Item orderItem in orderItemList) { finalTotalQuantity += (int)orderItem.Quantity; finalTotalPrice += (int)orderItem.Price * (int)orderItem.Quantity; if (orderItem.Quantity < 1) { db.Order_Items.Remove(orderItem); } } cart.Total_Quantity = finalTotalQuantity; cart.Total_Price = finalTotalPrice; db.SaveChanges(); isUpdated = true; } catch { throw new FoodOrderException(); } return(isUpdated); } }