public async void CreateMenuItem(string name, string description, double parsedPrice, int parsedWaitTime, string ingredients, int parsedCalories, bool parsedHalal, int parsedCatID, string resUsername, IFormFile file) { int redID = RestaurantController.GetResByUsername(resUsername).ID; string fileName = await ImageController.UploadImage(name, file); using (RestaurantContext context = new RestaurantContext()) { MenuItem newMenuItem = new MenuItem() { Name = Regex.Escape(name), Description = Regex.Escape(description), Price = parsedPrice, WaitTimeMins = parsedWaitTime, Ingredients = Regex.Escape(ingredients), Calories = parsedCalories, Halal = parsedHalal, CategoryID = parsedCatID, RestaurantID = redID, ImageName = fileName }; context.MenuItems.Add(newMenuItem); context.SaveChanges(); } }
// READ public List <MenuItem> ListMenuItems(string username) { username = username.Trim().ToLower(); List <MenuItem> menuList; Restaurant restaurant = RestaurantController.GetResByUsername(username); using (RestaurantContext context = new RestaurantContext()) { menuList = context.MenuItems.Where(m => m.RestaurantID == restaurant.ID).ToList(); } return(menuList); }
/* * Category Controller with all the methods that interact with the Restaurant table. (Restaurant = user) * Methods are seperated and ordered by CRUD functionalities (Create, read, update, delete) * This was created with the future in mind where users will be able to create update and delete their own categories. */ // CREATE public void CreateCategory(string catName, string username) { Restaurant theUser = RestaurantController.GetResByUsername(username); using (RestaurantContext context = new RestaurantContext()) { Category newCategory = new Category() { Name = catName, RestaurantID = theUser.ID }; context.Add(newCategory); context.SaveChanges(); } }
// READ public List <Category> ListCategories(string username) { if (string.IsNullOrWhiteSpace(username)) { throw new Exception("Username cannot be empty"); } username = username.Trim().ToLower(); List <Category> result; Restaurant restaurant = RestaurantController.GetResByUsername(username); using (RestaurantContext context = new RestaurantContext()) { result = context.Categories.Where(m => m.RestaurantID == restaurant.ID).ToList(); } return(result); }
/* * Menu Item methods/methods that interract with the menuitems table. * Seperated and ordered by CRUD functionalities. (Create, read, update, delete) */ // CREATE /* * Create Menu Validator validates all the data to make sure create menu item doesn't throw any exceptions. * This was necessary because Async methods return Task exceptions which not readable for humans. This method throws the exceptions before it gets to that. */ public void CreateMenuValidator(string name, string description, string price, string waitTimeMins, string ingredients, string calories, string halal, string catID, string resUsername, IFormFile file) { // INT VALIDATION if (string.IsNullOrWhiteSpace(price)) { throw new Exception("Price cannot be empty"); } double parsedPrice; if (!double.TryParse(price, out parsedPrice)) { throw new Exception("Price must be a number"); } if (!UserInt.IsPositiveNumber(parsedPrice)) { throw new Exception("Price can't be under $0"); } if (UserStr.IsLengthOverLimit(10, price)) { throw new Exception("Price connot be above 999999999"); } if (string.IsNullOrWhiteSpace(waitTimeMins)) { throw new Exception("Wait Time cannot be empty"); } int parsedWaitTime; if (!int.TryParse(waitTimeMins, out parsedWaitTime)) { throw new Exception("Price must be a number"); } if (!UserInt.IsPositiveNumber(parsedWaitTime)) { throw new Exception("Wait time can't be under 0 minutes"); } if (UserStr.IsLengthOverLimit(10, waitTimeMins)) { throw new Exception("Wait Time connot be above 999999999 Minutes"); } if (string.IsNullOrWhiteSpace(calories)) { throw new Exception("Calories cannot be empty"); } int parsedCalories; if (!int.TryParse(calories, out parsedCalories)) { throw new Exception("Calories must be a number"); } if (!UserInt.IsPositiveNumber(parsedCalories)) { throw new Exception("Calories can't be under 0 Calories. You wish"); } if (UserStr.IsLengthOverLimit(10, calories)) { throw new Exception("Calories connot be above 999999999 Calories"); } if (string.IsNullOrWhiteSpace(catID)) { throw new Exception("Category cannot be empty"); } int parsedCatID; if (!int.TryParse(catID, out parsedCatID)) { throw new Exception("Category ID must be a Number"); } //BOOL VALIDATION if (string.IsNullOrWhiteSpace(halal)) { throw new Exception("Restrictions cannot be empty"); } bool parsedHalal; halal = halal.ToLower().Trim(); if (!bool.TryParse(halal, out parsedHalal)) { throw new Exception("Halal must be either true or false"); } // STRING VALIDATION if (string.IsNullOrWhiteSpace(name)) { throw new Exception("Name cannot be empty"); } name = name.Trim(); if (UserStr.IsLengthOverLimit(100, name)) { throw new Exception("Name cannot exceed 100 characters"); } if (string.IsNullOrWhiteSpace(description)) { throw new Exception("Description cannot be empty"); } description = description.Trim(); if (UserStr.IsLengthOverLimit(1000, description)) { throw new Exception("Description cannot exceed 100 characters"); } if (string.IsNullOrWhiteSpace(ingredients)) { throw new Exception("Ingredients cannot be empty"); } ingredients = ingredients.Trim(); if (UserStr.IsLengthOverLimit(1000, ingredients)) { throw new Exception("Ingredients cannot exceed 100 characters"); } if (file == null) { throw new Exception("Must upload an image"); } RestaurantController.GetResByUsername(resUsername); CreateMenuItem(name, description, parsedPrice, parsedWaitTime, ingredients, parsedCalories, parsedHalal, parsedCatID, resUsername, file); }