Comments for the logic

This commit is contained in:
Jeas0001 2025-05-06 13:56:46 +02:00
parent f84cd6167b
commit 15a09e0d30
4 changed files with 48 additions and 9 deletions

View File

@ -16,8 +16,8 @@ namespace API.BusinessLogic
/// <summary>
/// Gets all the recipes from _dbaccess and checks if there are any
/// </summary>
/// <param name="userId"></param>
/// <returns></returns>
/// <param name="userId">the usér connected to the recipes</param>
/// <returns>a list of recipes in a ok objectresult</returns>
public async Task<IActionResult> 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<IActionResult> GetRecipe(int recipeId)
{
var recipe = await _dbAccess.ReadRecipe(recipeId);
@ -36,6 +36,13 @@ namespace API.BusinessLogic
return new OkObjectResult(recipe);
}
/// <summary>
/// Creates a recipe and checks if the recipe name is in use
/// Converts the recipeDTO to a normal recipe
/// </summary>
/// <param name="recipe">The recipeDTO that does not include all the id tags</param>
/// <param name="userId">The user that is going to get that recipe</param>
/// <returns>returns what recipedbaccess gives back</returns>
public async Task<IActionResult> CreateRecipe(RecipeDTO recipe, int userId)
{
var recipes = await _dbAccess.ReadRecipes(userId);
@ -71,6 +78,13 @@ namespace API.BusinessLogic
return await _dbAccess.CreateRecipe(dish, userId);
}
/// <summary>
/// Updates the recipe that is saved in the db
/// </summary>
/// <param name="recipe">The updated recipe</param>
/// <param name="recipeId">The recipeId on the recipe to be updated</param>
/// <param name="userId">The userÍd of the owner of the to be updated recipe</param>
/// <returns>returns what recipedbaccess gives back</returns>
public async Task<IActionResult> 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<IActionResult> 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" });
}
}
}

View File

@ -15,6 +15,7 @@ namespace API.BusinessLogic
_recipeDBAccess = recipeDBAccess;
}
// Reads the current shooping list of the user
public async Task<IActionResult> 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<IActionResult> 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<IActionResult> 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<IActionResult> 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<IActionResult> 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<IActionResult> AddRecipeToShoppingList(int userId, int recipeId)
{
var user = await _dbAccess.ReadShoppingList(userId);

View File

@ -23,6 +23,7 @@ namespace API.BusinessLogic
_configuration = configuration;
}
// Gets an user from their id
public async Task<IActionResult> 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<IActionResult> 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<IActionResult> 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<IActionResult> 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<IActionResult> 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<IActionResult> 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<IActionResult> 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);
}
/// <summary>
/// Generates a hash from a salt and input using the algorithm that is provided
/// </summary>
/// <param name="input">This is the input that is supposed to be hashed</param>
/// <param name="algorithm">This is the alogorithm that is used to encrypt the input</param>
/// <param name="salt">This is something extra added to make the hashed input more unpredictable</param>
/// <returns>The hashed input</returns>
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<User> UpdateRefreshToken(User user)
{
user.RefreshToken = Guid.NewGuid().ToString();

View File

@ -23,7 +23,7 @@ namespace API.DBAccess
public async Task<Recipe> 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<IActionResult> 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<IActionResult> DeleteUser(Recipe recipe)
public async Task<IActionResult> DeleteRecipe(Recipe recipe)
{
_context.Recipes.Remove(recipe);
bool saved = await _context.SaveChangesAsync() >= 0;