In my application there is a Enum which name is RecordType and all of tables contains a field named 'TypeId'.
When a user Add a new record, I set TypeId based on user's TypeId. in this way I want to load data realate to each user's Type.
RecordType is :
public enum RecordType
{
None=0,
Programmers = 1,
Marketer = 3,
Financial = 5,
}
When a user log-in to the system with type of Programmer I must just load all the data that have added by users with type 'Programmer'.
I wanted to use HasQueryFilter() but As I know it just work with static fields and can not use currentUserId beacuse it is possible after running application.
I added an extension method like this:
public static class QueryFilterExtensions
{
public static IQueryable<TEntity> FilterByUser<TEntity>(this IQueryable<TEntity> query, ICurrentUserService currentUser) where TEntity : BaseEntity
{
if (currentUser.TypeId != Domain.Enums.RecordType.None)
query = query.Where(e => e.TypeId == currentUser.TypeId);
return query;
}
}
in this way I have to reapt calling this Extension method like below in all part of my reading.
return await _dbContext.Groups.OrderBy(x => x.Id)
.FilterByUser(_currentUser)
.ProjectTo<GroupDto>(_mapper.ConfigurationProvider)
.PaginatedListAsync(request.PageIndex, request.PageSize);