From 15a09e0d30dfff9ed8ea0b0dc34e35e7adfbb65f Mon Sep 17 00:00:00 2001 From: Jeas0001 Date: Tue, 6 May 2025 13:56:46 +0200 Subject: [PATCH] Comments for the logic --- backend/API/BusinessLogic/RecipeLogic.cs | 25 +++++++++++++++---- .../API/BusinessLogic/ShoppingListLogic.cs | 10 ++++++-- backend/API/BusinessLogic/UserLogic.cs | 18 +++++++++++++ backend/API/DBAccess/RecipeDBaccess.cs | 4 +-- 4 files changed, 48 insertions(+), 9 deletions(-) diff --git a/backend/API/BusinessLogic/RecipeLogic.cs b/backend/API/BusinessLogic/RecipeLogic.cs index 8d02900..48aaa3f 100644 --- a/backend/API/BusinessLogic/RecipeLogic.cs +++ b/backend/API/BusinessLogic/RecipeLogic.cs @@ -16,8 +16,8 @@ namespace API.BusinessLogic /// /// Gets all the recipes from _dbaccess and checks if there are any /// - /// - /// + /// the usér connected to the recipes + /// a list of recipes in a ok objectresult public async Task GetRecipes(int userId) { var recipes = await _dbAccess.ReadRecipes(userId); @@ -26,7 +26,7 @@ namespace API.BusinessLogic return new OkObjectResult(recipes); } - // + // Gets a specifik with recipe with the ingredient and directions public async Task GetRecipe(int recipeId) { var recipe = await _dbAccess.ReadRecipe(recipeId); @@ -36,6 +36,13 @@ namespace API.BusinessLogic return new OkObjectResult(recipe); } + /// + /// Creates a recipe and checks if the recipe name is in use + /// Converts the recipeDTO to a normal recipe + /// + /// The recipeDTO that does not include all the id tags + /// The user that is going to get that recipe + /// returns what recipedbaccess gives back public async Task CreateRecipe(RecipeDTO recipe, int userId) { var recipes = await _dbAccess.ReadRecipes(userId); @@ -71,6 +78,13 @@ namespace API.BusinessLogic return await _dbAccess.CreateRecipe(dish, userId); } + /// + /// Updates the recipe that is saved in the db + /// + /// The updated recipe + /// The recipeId on the recipe to be updated + /// The userÍd of the owner of the to be updated recipe + /// returns what recipedbaccess gives back public async Task EditRecipe(RecipeDTO recipe, int recipeId, int userId) { var recipes = await _dbAccess.ReadRecipes(userId); @@ -105,13 +119,14 @@ namespace API.BusinessLogic return await _dbAccess.UpdateRecipe(dish); } + // Deletes the recipe public async Task DeleteRecipe(int recipeId) { var recipe = await _dbAccess.ReadRecipe(recipeId); - if (recipe != null) { return await _dbAccess.DeleteUser(recipe); } + if (recipe != null) { return await _dbAccess.DeleteRecipe(recipe); } - return new ConflictObjectResult(new { message = "Invalid user" }); + return new ConflictObjectResult(new { message = "Invalid recipe" }); } } } diff --git a/backend/API/BusinessLogic/ShoppingListLogic.cs b/backend/API/BusinessLogic/ShoppingListLogic.cs index 53f2da9..1651896 100644 --- a/backend/API/BusinessLogic/ShoppingListLogic.cs +++ b/backend/API/BusinessLogic/ShoppingListLogic.cs @@ -15,6 +15,7 @@ namespace API.BusinessLogic _recipeDBAccess = recipeDBAccess; } + // Reads the current shooping list of the user public async Task ReadShoppingList(int userId) { var user = await _dbAccess.ReadShoppingList(userId); @@ -22,6 +23,7 @@ namespace API.BusinessLogic return new OkObjectResult(user.ShoppingList); } + // Adds an item to the shoppinglist and checks if the unit should be changed and if the name is the same as an item already on the shoppinglist public async Task AddItemToShoppingList(ShoppingListItemDTO listItemDTO, int userId) { var user = await _dbAccess.ReadShoppingList(userId); @@ -42,12 +44,12 @@ namespace API.BusinessLogic item.Amount = (item.Amount / 1000) + listItemDTO.Amount; item.Unit = "kg"; } - else if (item.Unit == "ml" && item.Unit == "l") + else if (item.Unit == "ml" && listItemDTO.Unit == "l") { item.Amount = (item.Amount / 1000) + listItemDTO.Amount; item.Unit = "l"; } - else if (item.Unit == "dl" && item.Unit == "l") + else if (item.Unit == "dl" && listItemDTO.Unit == "l") { item.Amount = (item.Amount / 10) + listItemDTO.Amount; item.Unit = "l"; @@ -85,6 +87,7 @@ namespace API.BusinessLogic return await _dbAccess.UpdateShoppingList(user); } + // Gets the shoppinglist and tries to find the item and when it does it checks/unchecks that item public async Task CheckItemInShoppingList(int userId, int itemId) { var user = await _dbAccess.ReadShoppingList(userId); @@ -96,6 +99,7 @@ namespace API.BusinessLogic return await _dbAccess.UpdateShoppingList(user); } + // Updates an item on the shopping list to what the user specified public async Task UpdateItemInShoppingList(int userId, int itemId, ShoppingListItemDTO listItemDTO) { var user = await _dbAccess.ReadShoppingList(userId); @@ -125,6 +129,7 @@ namespace API.BusinessLogic return await _dbAccess.UpdateShoppingList(user); } + // Deletes an item from the shopping list if it is on the users shoppinglist public async Task DeleteItemInShoppingList(int userId, int itemId) { var user = await _dbAccess.ReadShoppingList(userId); @@ -136,6 +141,7 @@ namespace API.BusinessLogic return await _dbAccess.UpdateShoppingList(user); } + // Adds an entire recipes ingredients to the shoppinglist public async Task AddRecipeToShoppingList(int userId, int recipeId) { var user = await _dbAccess.ReadShoppingList(userId); diff --git a/backend/API/BusinessLogic/UserLogic.cs b/backend/API/BusinessLogic/UserLogic.cs index 2a93d55..f32bd2e 100644 --- a/backend/API/BusinessLogic/UserLogic.cs +++ b/backend/API/BusinessLogic/UserLogic.cs @@ -23,6 +23,7 @@ namespace API.BusinessLogic _configuration = configuration; } + // Gets an user from their id public async Task GetUser(int userId) { User user = await _dbAccess.ReadUser(userId); @@ -31,6 +32,7 @@ namespace API.BusinessLogic return new OkObjectResult(new { user.Id, user.UserName, user.Email }); } + // Checks if the userdata is ok before the user is created and creats the othere list's that the user have public async Task RegisterUser(CreateUserDTO userDTO) { if (!EmailCheck(userDTO.Email)) @@ -71,6 +73,7 @@ namespace API.BusinessLogic return await _dbAccess.CreateUser(user); } + // Checks if the username/email matches the password and generates a jwttoken if it is correct public async Task Login(LoginDTO loginDTO) { var user = await _dbAccess.ReadUserForLogin(loginDTO.EmailUsr); @@ -91,6 +94,7 @@ namespace API.BusinessLogic return new ConflictObjectResult(new { message = "Invalid password" }); } + // Checks if the username or email is already in use and changes them if they are diffrent from before public async Task EditProfile(UpdateUserDTO userDTO, int userId) { var profile = await _dbAccess.ReadUser(userId); @@ -142,6 +146,7 @@ namespace API.BusinessLogic return await _dbAccess.UpdateUser(profile); } + // Checks if the old password is correct and then it checks if the password is secure enough public async Task ChangePassword(ChangePasswordDTO passwordDTO, int userId) { var user = await _dbAccess.ReadUser(userId); @@ -167,6 +172,7 @@ namespace API.BusinessLogic return await _dbAccess.UpdatePassword(user); } + // Checks if the user exist and it deletes that user public async Task DeleteUser(int userId) { var user = await _dbAccess.ReadUserForDelete(userId); @@ -176,6 +182,7 @@ namespace API.BusinessLogic return new ConflictObjectResult(new { message = "Invalid user" }); } + // Checks if the refreshtoken is correct and if it is it generates a new jwttoken and refreshtoken public async Task RefreshToken(string refreshToken) { User user = await _dbAccess.ReadUserByRefreshToken(refreshToken); @@ -185,6 +192,7 @@ namespace API.BusinessLogic return new OkObjectResult(new { token = jwtToken, refreshToken = user.RefreshToken }); } + // Checks if the password is up to our security standard private bool PasswordSecurity(string password) { var hasMinimum8Chars = new Regex(@".{8,}"); @@ -192,11 +200,19 @@ namespace API.BusinessLogic return hasMinimum8Chars.IsMatch(password); } + // Checks if the email has all the things an email should have private bool EmailCheck(string email) { return new Regex(@".+@.+\..+").IsMatch(email); } + /// + /// Generates a hash from a salt and input using the algorithm that is provided + /// + /// This is the input that is supposed to be hashed + /// This is the alogorithm that is used to encrypt the input + /// This is something extra added to make the hashed input more unpredictable + /// The hashed input private static string ComputeHash(string input, HashAlgorithm algorithm, string salt) { Byte[] inputBytes = Encoding.UTF8.GetBytes(input); @@ -212,6 +228,7 @@ namespace API.BusinessLogic return BitConverter.ToString(hashedBytes); } + // Generates a jwttoken that contains the users id and username and a unique identifier that is valid for 1 hour private string GenerateJwtToken(User user) { var claims = new[] @@ -235,6 +252,7 @@ namespace API.BusinessLogic return new JwtSecurityTokenHandler().WriteToken(token); } + // Generate a new refreshtoken that expire after 30 days private async Task UpdateRefreshToken(User user) { user.RefreshToken = Guid.NewGuid().ToString(); diff --git a/backend/API/DBAccess/RecipeDBaccess.cs b/backend/API/DBAccess/RecipeDBaccess.cs index ef1ee81..b36b9be 100644 --- a/backend/API/DBAccess/RecipeDBaccess.cs +++ b/backend/API/DBAccess/RecipeDBaccess.cs @@ -23,7 +23,7 @@ namespace API.DBAccess public async Task ReadRecipe(int recipeId) { - return await _context.Recipes.Include(r => r.Ingredients).FirstOrDefaultAsync(r => r.Id == recipeId); + return await _context.Recipes.Include(r => r.Ingredients).Include(r => r.Directions).FirstOrDefaultAsync(r => r.Id == recipeId); } public async Task CreateRecipe(Recipe recipe, int userId) @@ -50,7 +50,7 @@ namespace API.DBAccess return new ConflictObjectResult(new { message = "Could not save to database" }); } - public async Task DeleteUser(Recipe recipe) + public async Task DeleteRecipe(Recipe recipe) { _context.Recipes.Remove(recipe); bool saved = await _context.SaveChangesAsync() >= 0;