/// <summary> /// Method Deletes the Customer with customerId from table Customers /// </summary> /// <param name="customerId"></param> /// <returns>boolean value</returns> public bool DeleteCustomerById(int customerId) { 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 Customer with id customerId Customer item = db.Customers.Where(c => c.Customer_Id == customerId && c.IsActive == true).FirstOrDefault(); if (item != null) { //remove item from Customers db.Customers.Remove(item); db.SaveChanges(); return(true); } return(false); } } catch (Exception ex) { //throw user defined CustomerException throw new CustomerException(ex.Message); } }
/// <summary> /// Method updates or edits the changes of the passed customer in the Customers table /// </summary> /// <param name="customer"></param> /// <returns>boolean value</returns> public bool UpdateCustomer(Customer customer) { 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 Customer item = db.Customers.Where(c => c.Customer_Id == customer.Customer_Id && c.IsActive == true).FirstOrDefault(); if (item != null) { //update Customer details item.Customer_Name = customer.Customer_Name; item.Age = customer.Age; item.Password = customer.Password; item.Mobile_No = customer.Mobile_No; item.Email = customer.Email; item.City = customer.City; //save changes to the database db.SaveChanges(); return(true); } return(false); } } catch (Exception ex) { //throw user defined CustomerException throw new CustomerException(ex.Message); } }
/// <summary> /// Method Deletes the Employee with employeeId from table Employees /// </summary> /// <param name="employeeId">the id of employee to be deleted</param> /// <returns>boolean value true denoting successful deletion and false for failure in such</returns> public bool DeleteEmployeeById(int employeeId) { 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 Employee with id employeeId Employee employee = db.Employees.Where(f => f.Employee_Id == employeeId && f.IsActive == true).FirstOrDefault(); if (employee != null) //use LINQ query to delete employee from table Employees { //remove employee from Employees db.Employees.Remove(employee); //save changes to the database db.SaveChanges(); return(true); } else { throw new EmployeeException("Employee does not exist"); } } } catch (Exception ex) { //throw user defined EmployeeException throw new EmployeeException(ex.Message); } }
/// <summary> /// Method Deletes the Food item with foodItemId from table Food_Items /// </summary> /// <param name="foodItemId">integer indicating id of Food item</param> /// <returns>boolean value indicating whether food item is deleted or not</returns> public bool DeleteFoodItemById(int foodItemId) { 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 foodItemId Food_Item item = db.Food_Items.Where(f => f.Food_Item_Id == foodItemId && f.IsActive == true).FirstOrDefault(); if (item != null) //use LINQ query to Add Food Items from table Food_Items { //remove item from Food_Items db.Food_Items.Remove(item); //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> /// Method updates or edits the changes of the passed foodStore in the Food_Stores table /// </summary> /// <param name="foodStore"></param> /// <returns>boolean value</returns> public bool UpdateFoodStore(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 find the Food Store with id foodStore.Food_Store_Id Food_Store store = db.Food_Stores.Where(f => f.Food_Store_Id == foodStore.Food_Store_Id && f.IsActive == true).FirstOrDefault(); if (store != null) { //update Food Store details store.Food_Store_Name = foodStore.Food_Store_Name; store.Location = foodStore.Location; store.Mobile_No = foodStore.Mobile_No; store.Email = foodStore.Email; store.Rating = foodStore.Rating; //save changes to the database db.SaveChanges(); return(true); } return(false); } } catch (Exception ex) { //throw user defined FoodOrderException throw new FoodOrderException(ex.Message); } }
/// <summary> /// Method Deletes the Food store with foodStoreId from table Food_Stores /// </summary> /// <param name="foodStoreId"></param> /// <returns>boolean value</returns> public bool DeleteFoodStoreById(int foodStoreId) { try { //instantiating Online_Food_Ordering_SystemEntities Context class using (Online_Food_Ordering_SystemEntities1 db = new Online_Food_Ordering_SystemEntities1()) { //use LINQ query to find the Food Store with id foodStoreId Food_Store store = db.Food_Stores.Where(f => f.Food_Store_Id == foodStoreId && f.IsActive == true).FirstOrDefault(); if (store != null) //use LINQ query to delete Food Stores from table Food_Stores { //remove item from Food_Stores db.Food_Stores.Remove(store); //save changes to the database db.SaveChanges(); return(true); } return(false); } } catch (Exception ex) { //throw user defined FoodOrderException throw new FoodOrderException(ex.Message); } }
/// <summary> /// Converts a "cart" into an order by submitting it /// </summary> /// <param name="customerId">integer value to uniquely identify the customer</param> /// <returns>boolean value true if successfully submitted, false if not submitted</returns> public bool SubmitOrder(int customerId) { using (Online_Food_Ordering_SystemEntities1 db = new Online_Food_Ordering_SystemEntities1()) { bool isSubmitted = false; try { Order cart = db.Orders.FirstOrDefault(o => (o.isActive && o.Customer_Id == customerId && o.Submit_Status == false)); cart.Submit_Status = true; cart.Order_date = DateTime.Now; db.SaveChanges(); isSubmitted = true; } catch { throw new FoodOrderException(); } return(isSubmitted); } }
/// <summary> /// AddCustomer(Customers customer) adds the customer to the Customers table /// </summary> /// <param name="customer">xxxxxxxx</param> /// <returns>integer value indicating the Customer_Id of the added foodItem</returns> public bool AddCustomer(Customer customer) { try { //instantiating Online_Food_Ordering_SystemEntities3 Context class using (Online_Food_Ordering_SystemEntities1 db = new Online_Food_Ordering_SystemEntities1()) { //use LINQ query to Add Customers into table Customers db.Customers.Add(customer); //save changes to the database db.SaveChanges(); return(true); } } catch (Exception ex) { //throw user defined CustomerException throw new CustomerException(ex.Message); } }
/// <summary> /// AddFoodItem(Food_Items foodItem) adds the foodItem to the Food_Items table /// </summary> /// <param name="foodItem">object of type Food_Item</param> /// <returns>integer value indicating the Id of the added foodItem</returns> public int AddFoodItem(Food_Item foodItem) { try { //instantiating Online_Food_Ordering_SystemEntities3 Context class using (Online_Food_Ordering_SystemEntities1 db = new Online_Food_Ordering_SystemEntities1()) { //check if the foodItem already exists Food_Item item = db.Food_Items.Where(f => f.Food_Name.Equals(foodItem.Food_Name, StringComparison.OrdinalIgnoreCase) && f.Food_Type.Equals(foodItem.Food_Type, StringComparison.OrdinalIgnoreCase) && f.IsActive == true).FirstOrDefault(); if (item != null) { //if exists then throw exception throw new FoodOrderException("Food Item already present"); } //set IsActive to true foodItem.IsActive = true; //set Creation Date to be the foodItem.Creation_Date = DateTime.Now; //use LINQ query to Add Food Items from table Food_Items db.Food_Items.Add(foodItem); //save changes to the database db.SaveChanges(); Food_Item fItem = db.Food_Items.Where(f => f.Food_Item_Id == foodItem.Food_Item_Id && f.IsActive == true).FirstOrDefault(); return(fItem.Food_Item_Id); } } catch (Exception ex) { //throw user defined FoodOrderException throw new FoodOrderException(ex.Message); } }
/// <summary> /// Method updates or edits the changes of the passed employee in the Employees table /// </summary> /// <param name="employee">Employee type value to be updated</param> /// <returns>boolean value true shows the value is updated and false shows it failed to do so</returns> public bool UpdateEmployee(Employee employee) { 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 employee with id employee.Employee_Id Employee item = db.Employees.Where(f => f.Employee_Id == employee.Employee_Id && f.IsActive == true).FirstOrDefault(); if (item != null) { //update Employee details item.Employee_Name = employee.Employee_Name; item.Age = employee.Age; item.Store_Id = employee.Store_Id; item.Password = employee.Password; item.Mobile_No = employee.Mobile_No; item.Email = employee.Email; item.City = employee.City; //save changes to the database db.SaveChanges(); return(true); } else { throw new EmployeeException("Employee does not exist"); } } } catch (Exception ex) { //throw user defined EmployeeException throw new EmployeeException(ex.Message); } }
/// <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> /// 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> /// 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); } }