Compare commits

...

3 Commits

Author SHA1 Message Date
Jeas0001
050ff896e1 UserDbAccess comments 2025-05-07 13:33:36 +02:00
Jeas0001
dee0ea54a2 shoppinglistdbaccess comments 2025-05-07 13:14:41 +02:00
Jeas0001
fe90c243e2 RecipeDbAccess comments 2025-05-07 13:05:59 +02:00
3 changed files with 20 additions and 1 deletions

View File

@ -14,6 +14,7 @@ namespace API.DBAccess
_context = context;
}
// Reads the users recipes
public async Task<List<Recipe>> ReadRecipes(int userId)
{
var recipes = await _context.Users.Include(p => p.Recipes).FirstOrDefaultAsync(u => u.Id == userId);
@ -21,11 +22,13 @@ namespace API.DBAccess
return recipes.Recipes;
}
// Returns a specifik recipe
public async Task<Recipe> ReadRecipe(int recipeId)
{
return await _context.Recipes.Include(r => r.Ingredients).Include(r => r.Directions).FirstOrDefaultAsync(r => r.Id == recipeId);
}
// Adds a new recipe to the database
public async Task<IActionResult> CreateRecipe(Recipe recipe, int userId)
{
var recipes = await _context.Users.Include(p => p.Recipes).FirstOrDefaultAsync(u => u.Id == userId);
@ -39,6 +42,7 @@ namespace API.DBAccess
return new ConflictObjectResult(new { message = "Could not save to database" });
}
// Updates the recipe in the database
public async Task<IActionResult> UpdateRecipe(Recipe recipe)
{
_context.Entry(recipe).State = EntityState.Modified;
@ -50,6 +54,7 @@ namespace API.DBAccess
return new ConflictObjectResult(new { message = "Could not save to database" });
}
// Deletes the recipe from the database
public async Task<IActionResult> DeleteRecipe(Recipe recipe)
{
_context.Recipes.Remove(recipe);

View File

@ -14,6 +14,8 @@ namespace API.DBAccess
{
_context = context;
}
// Read the shoppinglist connected to the user
public async Task<User> ReadShoppingList(int userId)
{
var user = await _context.Users.Include(u => u.ShoppingList).FirstOrDefaultAsync(u => u.Id == userId);
@ -21,6 +23,7 @@ namespace API.DBAccess
return user;
}
// Updates the shoppinglist
public async Task<IActionResult> UpdateShoppingList(User user)
{
_context.Entry(user).State = EntityState.Modified;

View File

@ -13,16 +13,19 @@ namespace API.DBAccess
_context = context;
}
// Reads the user from the database
public async Task<User> ReadUser(int userId)
{
return await _context.Users.FirstOrDefaultAsync(u => u.Id == userId);
}
// Reads all the users from the database
public async Task<List<User>> ReadAllUsers()
{
return await _context.Users.ToListAsync();
}
// Searches for a user with the username
public async Task<bool> UserNameInUse(string username)
{
var user = await _context.Users.FirstOrDefaultAsync(u => u.UserName == username);
@ -30,6 +33,7 @@ namespace API.DBAccess
return true;
}
// Searches for a user with the email
public async Task<bool> EmailInUse(string email)
{
var user = await _context.Users.FirstOrDefaultAsync(u => u.UserName == email);
@ -37,16 +41,19 @@ namespace API.DBAccess
return true;
}
// Searches for a user with refreshtoken and returns that user
public async Task<User> ReadUserByRefreshToken(string refreshToken)
{
return await _context.Users.FirstOrDefaultAsync(u => u.RefreshToken == refreshToken);
}
// Gets all the data for a user
public async Task<User> ReadUserForDelete(int userId)
{
return await _context.Users.FirstOrDefaultAsync(u => u.Id == userId);
return await _context.Users.Include(u => u.Recipes).ThenInclude(r => r.Ingredients).Include(u => u.Recipes).ThenInclude(r => r.Directions).Include(u => u.ShoppingList).FirstOrDefaultAsync(u => u.Id == userId);
}
// Gets a user according to either the email or username
public async Task<User> ReadUserForLogin(string emailOrUsername)
{
if (emailOrUsername.Contains("@"))
@ -59,6 +66,7 @@ namespace API.DBAccess
}
}
// Adds a new user to the database
public async Task<IActionResult> CreateUser(User user)
{
_context.Users.Add(user);
@ -71,6 +79,7 @@ namespace API.DBAccess
}
// Updates the user in the database
public async Task<IActionResult> UpdateUser(User user)
{
_context.Entry(user).State = EntityState.Modified;
@ -82,6 +91,7 @@ namespace API.DBAccess
return new ConflictObjectResult(new { message = "Could not save to database" });
}
// Updates the password in the database
public async Task<IActionResult> UpdatePassword(User user)
{
_context.Entry(user).State = EntityState.Modified;
@ -93,6 +103,7 @@ namespace API.DBAccess
return new ConflictObjectResult(new { message = "Could not save to database" });
}
// Deletes the user from the database
public async Task<IActionResult> DeleteUser(User user)
{
_context.Users.Remove(user);