/// <summary> /// Gets list of paginated reports. Optionally the list can be filtered by report status /// </summary> /// <param name="reportListParams">Pagination parameters of report list that will be returned (page number, page size, status filter).</param> /// <param name="userId">Id of user which made request</param> /// <returns>List of reports generated using parameters</returns> public async Task <IServiceResult <ReportListToReturnDto> > GetReportList(ReportListParams reportListParams, int userId) { try { UserEntity user = await _userManager.Users.Where(x => x.Id == userId).Include(ur => ur.UserRoles).ThenInclude(r => r.Role).FirstOrDefaultAsync(); if (user != null) { string userRole = user.UserRoles.Select(x => x.Role).FirstOrDefault().Name; IQueryable <ReportEntity> reports = _repository.GetReports(userRole, userId); reports = PaginateCollection(reports, reportListParams); int totalPages = (int)Math.Ceiling(reports.Count() / (double)reportListParams.PageSize); List <ReportListItemToReturnDto> reportItems = _reportMapper.Map(await reports.ToListAsync()).ToList(); ReportListToReturnDto reportList = new ReportListToReturnDto { TotalPages = totalPages, ReportListItems = reportItems }; return(new ServiceResult <ReportListToReturnDto>(ResultType.Correct, reportList)); } return(new ServiceResult <ReportListToReturnDto>(ResultType.Unauthorized)); } catch (Exception) { return(new ServiceResult <ReportListToReturnDto> (ResultType.Error, new List <string> { "Błąd podczas pobierania listy zgłoszeń" })); } }
/// <summary> /// Creates paginated (filtered out) collection based on pagination parameters /// </summary> /// <param name="source">Source data collection</param> /// <param name="paginationParameters">Parameters based on which the collection will be generated</param> /// <returns>Paginated source collection by parameters</returns> private IQueryable <ReportEntity> PaginateCollection(IQueryable <ReportEntity> source, ReportListParams paginationParameters) { if (paginationParameters.StatusFilter != null) { source = source.Where(s => (int)s.Status == paginationParameters.StatusFilter); } return(source .OrderByDescending(d => d.Date.Date) .ThenByDescending(d => d.Date.TimeOfDay) .Skip((paginationParameters.PageNumber - 1) * paginationParameters.PageSize) .Take(paginationParameters.PageSize)); }