52 lines
No EOL
1.6 KiB
C#
52 lines
No EOL
1.6 KiB
C#
using Microsoft.EntityFrameworkCore;
|
|
|
|
namespace ImageBoardServerApp.Data.Repository;
|
|
|
|
public static class UsersRepository
|
|
{
|
|
public static async Task<List<UserData>> getUsersAsync()
|
|
{
|
|
await using var db = new AppDBContext();
|
|
return await db.Users.ToListAsync();
|
|
}
|
|
|
|
public static async Task<UserData> getUserByIdAsync(int userId)
|
|
{
|
|
await using var db = new AppDBContext();
|
|
return await db.Users.FirstOrDefaultAsync(user => user.UserID == userId);
|
|
}
|
|
|
|
public static async Task<UserData> getUserByEmailAsync(string email)
|
|
{
|
|
await using var db = new AppDBContext();
|
|
return await db.Users.FirstOrDefaultAsync(user => user.Email == email);
|
|
}
|
|
|
|
public static async Task<int> createUserAsync(UserData userToCreate)
|
|
{
|
|
await using var db = new AppDBContext();
|
|
await db.Users.AddAsync(userToCreate);
|
|
if (await db.SaveChangesAsync() >= 1)
|
|
{
|
|
Console.WriteLine($"Created user with ID: {userToCreate.UserID}");
|
|
return userToCreate.UserID;
|
|
}
|
|
|
|
return -1;
|
|
}
|
|
|
|
public static async Task<bool> updateUserAsync(UserData userToUpdate)
|
|
{
|
|
await using var db = new AppDBContext();
|
|
db.Users.Update(userToUpdate);
|
|
return await db.SaveChangesAsync() >= 1;
|
|
}
|
|
|
|
public static async Task<bool> deleteUserAsync(int userId)
|
|
{
|
|
await using var db = new AppDBContext();
|
|
UserData userToDelete = await getUserByIdAsync(userId);
|
|
db.Remove(userToDelete);
|
|
return await db.SaveChangesAsync() >= 1;
|
|
}
|
|
} |