Selaa lähdekoodia

GetAll_FullName

zinab_elgendy 3 kuukautta sitten
vanhempi
commit
f23dc0b1ee
1 muutettua tiedostoa jossa 46 lisäystä ja 66 poistoa
  1. 46 66
      MTWorkHR.Application/Services/User/UserService.cs

+ 46 - 66
MTWorkHR.Application/Services/User/UserService.cs

@@ -165,87 +165,67 @@ namespace MTWorkHR.Application.Services
         public virtual async Task<PagingResultDto<UserAllDto>> GetAll(UserPagingInputDto PagingInputDto)
         {
             var query = _userManager.Users
-                .Include(u => u.Qualification).Include(u => u.JobTitle).Include(u => u.University).Include(u => u.Industry).Include(u => u.Country)
-                .Where(e => _globalInfo.CompanyId == null || e.CompanyId != _globalInfo.CompanyId)
-                .AsQueryable();
-            var filter = PagingInputDto.Filter;
-            query = query.Where(u =>
-         u.UserName.Contains(filter) ||
-         u.Email.Contains(filter) ||
-         u.FavoriteName.Contains(filter) ||
-         u.Position.Contains(filter) ||
-         u.PhoneNumber.Contains(filter));
-
-            // Check if the filter contains a space (indicating a full name)
-            if (filter.Contains(" "))
-            {
-                var fullName = filter.Split(" ");
-                var firstNameFilter = fullName[0];
-                var lastNameFilter = fullName.Length > 1 ? fullName[1] : "";
+                .Include(u => u.Qualification)
+                .Include(u => u.JobTitle)
+                .Include(u => u.University)
+                .Include(u => u.Industry)
+                .Include(u => u.Country)
+                .Where(u => _globalInfo.CompanyId == null || u.CompanyId != _globalInfo.CompanyId);
 
-                query = query.Where(u =>
-                    (u.FirstName.Contains(firstNameFilter) &&
-                     (string.IsNullOrEmpty(lastNameFilter) || u.LastName.Contains(lastNameFilter))) ||
-                    (u.LastName.Contains(lastNameFilter) &&
-                     (string.IsNullOrEmpty(firstNameFilter) || u.FirstName.Contains(firstNameFilter))));
-            }
-            else
+            var filter = PagingInputDto.Filter?.Trim();
+
+            // Filtering by text fields and handling full name search
+            if (!string.IsNullOrEmpty(filter))
             {
-                // If there's no space, apply the filter for individual names
+                var filters = filter.Split(" ");
+                var firstNameFilter = filters[0];
+                var lastNameFilter = filters.Length > 1 ? filters[1] : "";
+
                 query = query.Where(u =>
-                    u.FirstName.Contains(filter) ||
-                    u.LastName.Contains(filter));
-            }
-            if (PagingInputDto.IndustryId != null && PagingInputDto.IndustryId.Count > 0)
-            {
-                query = query.Where(u => u.IndustryId.HasValue && PagingInputDto.IndustryId.Contains( u.IndustryId.Value ));
+                    u.UserName.Contains(filter) || u.Email.Contains(filter) || u.FavoriteName.Contains(filter) ||
+                    u.Position.Contains(filter) || u.PhoneNumber.Contains(filter) ||
+                    u.FirstName.Contains(firstNameFilter) && (string.IsNullOrEmpty(lastNameFilter) || u.LastName.Contains(lastNameFilter)) ||
+                    u.LastName.Contains(lastNameFilter) && (string.IsNullOrEmpty(firstNameFilter) || u.FirstName.Contains(firstNameFilter))
+                );
             }
-            if (PagingInputDto.QualificationId != null)
-            {
+
+            // Applying additional filters
+            if (PagingInputDto.IndustryId?.Count > 0)
+                query = query.Where(u => u.IndustryId.HasValue && PagingInputDto.IndustryId.Contains(u.IndustryId.Value));
+
+            if (PagingInputDto.QualificationId.HasValue)
                 query = query.Where(u => u.QualificationId == PagingInputDto.QualificationId);
-            }
-            if (PagingInputDto.JobTitleId != null)
-            {
+
+            if (PagingInputDto.JobTitleId.HasValue)
                 query = query.Where(u => u.JobTitleId == PagingInputDto.JobTitleId);
-            }
-            if (PagingInputDto.UniversityId != null)
-            {
+
+            if (PagingInputDto.UniversityId.HasValue)
                 query = query.Where(u => u.UniversityId == PagingInputDto.UniversityId);
-            }
-            if (PagingInputDto.CountryId != null && PagingInputDto.CountryId.Count > 0)
-            {
-                //List<long> CountryList = PagingInputDto.CountryId.Split(",").Select(long.Parse).ToList();
+
+            if (PagingInputDto.CountryId?.Count > 0)
                 query = query.Where(u => u.CountryId.HasValue && PagingInputDto.CountryId.Contains(u.CountryId.Value));
-            }
-            if (PagingInputDto.UserTypeId != null && PagingInputDto.UserTypeId.Count > 0)
-            {
+
+            if (PagingInputDto.UserTypeId?.Count > 0)
                 query = query.Where(u => PagingInputDto.UserTypeId.Contains(u.UserType));
-            }
-            if (PagingInputDto.Employed != null)
-            {
-                if(PagingInputDto.Employed == true)
-                    query = query.Where(u => u.CompanyId != null);
-                else 
-                    query = query.Where(u => u.CompanyId == null);
-            }
-           
 
-            var order = query.OrderBy(PagingInputDto.OrderByField + " " + PagingInputDto.OrderType);
+            if (PagingInputDto.Employed.HasValue)
+                query = query.Where(u => PagingInputDto.Employed.Value ? u.CompanyId != null : u.CompanyId == null);
 
-            var page = order.Skip((PagingInputDto.PageNumber * PagingInputDto.PageSize) - PagingInputDto.PageSize).Take(PagingInputDto.PageSize);
+            // Ordering by specified field and direction
+            var orderByField = !string.IsNullOrEmpty(PagingInputDto.OrderByField) ? PagingInputDto.OrderByField : "UserName";
+            var orderByDirection = PagingInputDto.OrderType?.ToLower() == "desc" ? "desc" : "asc";
+            query = query.OrderBy($"{orderByField} {orderByDirection}");
 
+            // Pagination
             var total = await query.CountAsync();
+            var result = await query
+                .Skip((PagingInputDto.PageNumber - 1) * PagingInputDto.PageSize)
+                .Take(PagingInputDto.PageSize)
+                .ToListAsync();
 
-            var list = MapperObject.Mapper
-                .Map<IList<UserAllDto>>(await page.ToListAsync());
+            var list = MapperObject.Mapper.Map<IList<UserAllDto>>(result);
 
-            var response = new PagingResultDto<UserAllDto>
-            {
-                Result = list,
-                Total = total
-            };
-
-            return response;
+            return new PagingResultDto<UserAllDto> { Result = list, Total = total };
         }
         public async Task<List<UserDto>> GetAllEmployees()
         {