11using System ;
22using System . Collections . Generic ;
3+ using System . IdentityModel . Tokens . Jwt ;
34using System . Linq ;
45using System . Linq . Expressions ;
56using System . Threading ;
67using System . Threading . Tasks ;
78using MediatR ;
9+ using Microsoft . AspNetCore . Http ;
10+ using Microsoft . AspNetCore . Identity ;
811using Microsoft . EntityFrameworkCore ;
912using WebApi . Entities ;
1013using WebApi . Extensions ;
@@ -32,10 +35,17 @@ public class QueryHandler : IRequestHandler<Query, Object>
3235 private readonly int DEFAULT_PAGE = 1 ;
3336 private readonly int DEFAULT_ROWS_PER_PAGE = 10 ;
3437 private readonly ApplicationDbContext _context ;
38+ private readonly IHttpContextAccessor _httpContext ;
39+ private readonly UserManager < User > _manager ;
3540
36- public QueryHandler ( ApplicationDbContext context )
41+ public QueryHandler (
42+ ApplicationDbContext context ,
43+ IHttpContextAccessor httpContext ,
44+ UserManager < User > manager )
3745 {
3846 _context = context ;
47+ _httpContext = httpContext ;
48+ _manager = manager ;
3949 }
4050
4151 public async Task < Object > Handle ( Query request , CancellationToken cancellationToken )
@@ -45,16 +55,38 @@ public async Task<Object> Handle(Query request, CancellationToken cancellationTo
4555 IQueryable < LogViewModel > queryableModel ;
4656 var startDate = request . Parameters . StartDate ;
4757 var endDate = request . Parameters . EndDate ;
48-
49- // Apply Search filter if not null
5058 var searchQuery = request . Parameters . Search ;
51- if ( ! string . IsNullOrEmpty ( searchQuery ) )
59+
60+ // Check if the current user is Employee
61+ var isEmployee = _httpContext . HttpContext . User . IsInRole ( "Employee" ) ;
62+ if ( isEmployee )
63+ {
64+ // var username = _httpContext.HttpContext.User.FindFirst("sub");
65+
66+ // Get the username in sub type claim
67+ var username = _httpContext . HttpContext . User . Claims
68+ . First ( m => m . Type == "http://schemas.xmlsoap.org/ws/2005/05/identity/claims/nameidentifier" )
69+ . Value ;
70+
71+ // Get account details
72+ var account = _context . Users
73+ . Include ( m => m . Employee )
74+ . First ( m => m . UserName == username ) ;
75+
76+ queryableModel = _context . Logs . MapToViewModel ( )
77+ . Where ( m =>
78+ m . EmployeeId == account . Employee . Id &&
79+ m . Deleted == null ) ;
80+ }
81+ // Apply Search filter
82+ else if ( ! string . IsNullOrEmpty ( searchQuery ) )
5283 {
5384 queryableModel = _context . Logs . MapToViewModel ( )
5485 . Where ( m =>
5586 m . FullName . Contains ( searchQuery ) &&
5687 m . Deleted == null ) ;
5788 }
89+ // Get all List
5890 else
5991 {
6092 queryableModel = _context . Logs . MapToViewModel ( )
0 commit comments