/* * 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); }
/* * Refer to explanation for CreateMenuItemValidator method for explanation. */ public void UpdateMenuValidator(string menuID, string name, string description, string price, string waitTimeMins, string ingredients, string calories, string halal, string catID, IFormFile file, IWebHostEnvironment hostEnvironment) { // INT VALIDATION int parsedMenuID; if (!int.TryParse(menuID, out parsedMenuID)) { throw new Exception("Menu ID must be a number"); } GetMenuItemByID(menuID); if (!string.IsNullOrWhiteSpace(price)) { 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"); } } int parsedWaitTime; if (!string.IsNullOrWhiteSpace(waitTimeMins)) { 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)) { 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)) { int parsedCatID; if (!int.TryParse(catID, out parsedCatID)) { throw new Exception("Category ID must be a Number"); } } //BOOL VALIDATION bool parsedHalal; if (!string.IsNullOrWhiteSpace(halal)) { if (!bool.TryParse(halal, out parsedHalal)) { throw new Exception("Halal must be either true or false"); } } // STRING VALIDATION AND SANITIZATION if (!string.IsNullOrWhiteSpace(name)) { if (UserStr.IsLengthOverLimit(100, name)) { throw new Exception("Name cannot exceed 100 characters"); } } if (!string.IsNullOrWhiteSpace(description)) { if (UserStr.IsLengthOverLimit(1000, description)) { throw new Exception("Description cannot exceed 100 characters"); } } if (!string.IsNullOrWhiteSpace(ingredients)) { if (UserStr.IsLengthOverLimit(1000, ingredients)) { throw new Exception("Ingredients cannot exceed 100 characters"); } } UpdateMenuItem(menuID, name, description, price, waitTimeMins, ingredients, calories, halal, catID, file, hostEnvironment); }