Currently, each endpoint gets its data by first getting the logged user, and then gets the needed collections. This is heavily hitting the performance of the app and leads to slow data fetching. Here is presumably the main culprit of this issue:
public async Task<User> GetUserByIdAsync(int userID)
{
DateTime startOfMonth = new DateTime(DateTime.Now.Year, DateTime.Now.Month, 1);
User? user = await _dataContext.Users
.Include(u => u.Accounts)
.Include(u => u.Categories)!
.ThenInclude(c => c.Subcategories)
.Include(u => u.Transactions.Where(t => t.DateCreated >= startOfMonth))
.Include(u => u.FinancialRecommendations)
.Include(u => u.Notifications)
.FirstOrDefaultAsync(u => u.ID == userID);
return user!;
}
This statement includes every data the user has (which in some cases may exceed over 100 Transactions). Refactor it so that the data is not included and it is fetched depending on the UserID:
public async Task<User?> GetUserByIdAsync(int userID)
{
return await _dataContext.Users.FirstOrDefaultAsync(u => u.ID == userID);
}
or use a UserResponseDTO:
public async Task<CurrentUserDto?> GetUserByIdAsync(int userID)
{
return await _dataContext.Users
.Where(u => u.ID == userID)
.Select(u => new CurrentUserDto
{
ID = u.ID,
Name = u.Name,
Email = u.Email,
MethodologyType = u.MethodologyType
})
.FirstOrDefaultAsync();
}
or use Unit of Work Pattern
Currently, each endpoint gets its data by first getting the logged user, and then gets the needed collections. This is heavily hitting the performance of the app and leads to slow data fetching. Here is presumably the main culprit of this issue:
This statement includes every data the user has (which in some cases may exceed over 100 Transactions). Refactor it so that the data is not included and it is fetched depending on the UserID:
or use a UserResponseDTO:
or use Unit of Work Pattern