UserDbAccess comments

This commit is contained in:
Jeas0001 2025-05-07 13:33:36 +02:00
parent dee0ea54a2
commit 050ff896e1

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);