2023-01-18 12:56:24 +00:00
|
|
|
using Microsoft.EntityFrameworkCore;
|
|
|
|
|
|
|
|
namespace ImageBoardServerApp.Data.Repository;
|
|
|
|
|
|
|
|
public static class UsersRepository
|
|
|
|
{
|
|
|
|
public static async Task<List<UserData>> getUsersAsync()
|
|
|
|
{
|
|
|
|
await using var db = new AppDBContext();
|
2023-02-12 18:06:33 +00:00
|
|
|
return await db.Users
|
|
|
|
.Include(user => user.SubmittedReports)
|
|
|
|
.Include(user => user.RecivedReports)
|
|
|
|
.Include(user => user.Comments)
|
|
|
|
.Include(user => user.Posts)
|
|
|
|
.ToListAsync();
|
2023-01-18 12:56:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public static async Task<UserData> getUserByIdAsync(int userId)
|
|
|
|
{
|
|
|
|
await using var db = new AppDBContext();
|
|
|
|
return await db.Users.FirstOrDefaultAsync(user => user.UserID == userId);
|
|
|
|
}
|
2023-02-14 11:46:52 +00:00
|
|
|
|
|
|
|
public static async Task<UserData> getUserByEmailRawAsync(string email)
|
|
|
|
{
|
|
|
|
await using var db = new AppDBContext();
|
|
|
|
return await db.Users
|
|
|
|
.Where(user => user.Email == email)
|
|
|
|
.FirstOrDefaultAsync();
|
|
|
|
}
|
2023-01-27 21:09:16 +00:00
|
|
|
|
2023-02-01 20:49:02 +00:00
|
|
|
public static async Task<UserData> getUserByEmailAsync(string email)
|
2023-01-27 21:09:16 +00:00
|
|
|
{
|
|
|
|
await using var db = new AppDBContext();
|
2023-02-12 18:06:33 +00:00
|
|
|
return await db.Users
|
|
|
|
.Where(user => user.Email == email)
|
|
|
|
.Include(user => user.SubmittedReports)
|
2023-02-13 17:45:14 +00:00
|
|
|
.Include(user => user.Posts)
|
|
|
|
.Include(user => user.Comments)
|
|
|
|
.Include(user => user.RecivedReports)
|
2023-02-12 18:06:33 +00:00
|
|
|
.FirstOrDefaultAsync();
|
2023-01-27 21:09:16 +00:00
|
|
|
}
|
2023-01-18 12:56:24 +00:00
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
}
|