Skip to content

Conversation

@staszkiet
Copy link
Member

No description provided.

Comment on lines 34 to 39
public async Task<bool> CheckIfCategoriesExistAsync(List<Category> categories)
{
var dbCategories = await _tickApiDbContext.Categories.ToListAsync();
return categories.All(c => dbCategories.Any(cdb => cdb.Name == c.Name));
}

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we apply filter here before calling ToListAsync()? I think what happens now is all categories are fetched into memory and then they are filtered. If we do something like _tickApiDbContext.Categories.Where(c => categories.Include(c.Name)).Count() == categories.Count (the actual implementation may differ, but you get the idea - filter before calling ToListAsync() or any other function that actually fetches the records into memory) we don't fetch all categories but rather filter them on the sql level. I know in our case it's not gonna change much as we don't have many categories but I think it's something we should do anyway.
I also think in functions like this we should make arguments as generic as possible for future use, so in this case there is nothing stopping us from using IEnumerable<Category> instead of List<Category>.
Another thing is that I believe this kind of functionality should be placed in CategoryService rather than CategoryRepository. You can use GetCategories function from CategoryRepository and use the result of it in CategoryService for further data processing, I just feel like this is too high-level logic to put it in repository, which should be just responsible for data transfer between our app and db.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I agree with the points you made in the review but I believe the last point is contradictory to the first one. In the first one you say that we should change the data fetching so we fetch to the memory already filtered data and in the third point you say that I should use GetCategories which is a function that fetches all the data without applying filters. I mean the third point has the problem that you described in the first point. Which logic should I use then? I am more in favour of the one with changing it into service because, as you observed, there are not many categories and organizers are not allowed to add custom ones so the category table won't grow much. I would like to here your ideas @kubapoke @kTrzcinskii

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The IQueryable<T> represents the SQL query, it actually doesn't fetch data until you call ToListAsync on it or some other similar method. So if you call the GetCategories you can apply filters on them without having to fetch all the data

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy that

Copy link
Member

@kTrzcinskii kTrzcinskii left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM 🚀

@kTrzcinskii kTrzcinskii merged commit 84a38b0 into develop Apr 19, 2025
1 check passed
@kTrzcinskii kTrzcinskii deleted the feat/CreateEventWithCategoriesAndTicketTyped branch April 19, 2025 09:22
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants