// READ public string Login(string email, string password) { email = email.Trim().ToLower(); if (!UserStr.IsValidEmail(email)) { throw new Exception("Please enter a valid Email address"); } password = Regex.Escape(password); string username; using (RestaurantContext context = new RestaurantContext()) { if (!context.Restaurants.Any(x => x.Email == email)) { throw new Exception("There is no account under this email"); } else { Restaurant account = context.Restaurants.Where(r => r.Email == email).SingleOrDefault(); if (account.Password == password) { username = account.ResUsername; } else { throw new Exception("Password is incorrect"); } } } return(username); }
/* * 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); }
/* * Restaurant 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) */ // CREATE public string Register(string resName, string resUsername, string email, string password, string resLocation) { if (string.IsNullOrWhiteSpace(resName)) { throw new Exception("Restaurant Name cannot be empty"); } resName = resName.Trim(); if (UserStr.IsLengthOverLimit(75, resName)) { throw new Exception("Restaurant Name cannot exceed 75 Characters"); } if (string.IsNullOrWhiteSpace(resUsername)) { throw new Exception("Restaurant Username cannot be empty"); } resUsername = resUsername.Trim().ToLower(); if (UserStr.IsLengthOverLimit(75, resUsername)) { throw new Exception("Restaurant Username cannot exceed 75 Characters"); } if (resUsername.Contains(" ")) { throw new Exception("Username cannot contain a space"); } if (UserStr.ContainsSpecialChar(resUsername)) { throw new Exception("Username cannot contain special characters"); } if (string.IsNullOrWhiteSpace(email)) { throw new Exception("Email cannot be empty"); } email = email.Trim(); if (UserStr.IsLengthOverLimit(64, email)) { throw new Exception("Email cannot exceed 64 Characters"); } if (!UserStr.IsValidEmail(email)) { throw new Exception("Please enter a valid email address"); } if (string.IsNullOrWhiteSpace(password)) { throw new Exception("Password cannot be empty"); } if (UserStr.IsLengthOverLimit(50, password)) { throw new Exception("Password cannot exceed 50 Characters"); } if (string.IsNullOrWhiteSpace(resLocation)) { throw new Exception("Restaurant Location cannot be empty"); } resLocation = resLocation.Trim(); if (UserStr.IsLengthOverLimit(75, resLocation)) { throw new Exception("Address cannot exceed 75 Characters"); } using (RestaurantContext context = new RestaurantContext()) { if (context.Restaurants.Any(x => x.ResUsername == resUsername)) { throw new Exception("Restaurant username is taken"); } if (context.Restaurants.Any(x => x.Email == email)) { throw new Exception("An account by that email already exists"); } Restaurant newRestaurant = new Restaurant() { // Citation: [1] Microsoft Docs for Regex escape ResName = Regex.Escape(resName), ResUsername = resUsername, Email = email, Password = Regex.Escape(password), ResLocation = Regex.Escape(resLocation) }; context.Restaurants.Add(newRestaurant); context.SaveChanges(); } /* * 4 Categories are created for user by default. */ new CategoryController().CreateCategory("Starters", resUsername); new CategoryController().CreateCategory("Main Course", resUsername); new CategoryController().CreateCategory("Dessert", resUsername); new CategoryController().CreateCategory("Drinks", resUsername); return(resUsername); }