public async Task <IActionResult> GetSchools([FromQuery] SchoolPaginationParams schoolPaginationParams) { var schools = await database.SchoolRepository.GetPagedSchools(schoolPaginationParams, isAdmin : true); var schoolsToReturn = mapper.Map <ICollection <SchoolAdminListDto> >(schools); Response.AddPagination(schools.CurrentPage, schools.PageSize, schools.TotalCount, schools.TotalPages); return(Ok(schoolsToReturn)); }
public async Task <IActionResult> GetSchools([FromQuery] SchoolPaginationParams schoolPaginationParams) { bool isAdmin = false; int currentUserId = 0; if (HttpContext.User.FindFirst(ClaimTypes.NameIdentifier) != null) { currentUserId = int.Parse(HttpContext.User.FindFirst(ClaimTypes.NameIdentifier).Value); if (currentUserId != schoolPaginationParams.UserId && schoolPaginationParams.UserId != 0) { return(BadRequest("Niepoprawne dane wejściowe")); } isAdmin = await rolesService.IsPermitted(RolesPermitted, currentUserId); } else { schoolPaginationParams.UserId = 0; } var schools = await database.SchoolRepository.GetPagedSchools(schoolPaginationParams, isAdmin : isAdmin); var schoolsToReturn = mapper.Map <ICollection <SchoolUserListDto> >(schools).ToList(); if (currentUserId != 0) { for (int i = 0; i < schoolsToReturn.Count; i++) { if (schools[i].Follows.Any(f => f.UserId == currentUserId)) { schoolsToReturn[i].IsFollowed = true; } } } Response.AddPagination(schools.CurrentPage, schools.PageSize, schools.TotalCount, schools.TotalPages); return(Ok(schoolsToReturn)); }
public async Task <PagedList <School> > GetPagedSchools(SchoolPaginationParams schoolPaginationParams, bool isAdmin = false) { var schools = await GetWhere <School>(s => s.Rating >= schoolPaginationParams.Rating && !s.Owner.IsBlocked); if (schoolPaginationParams.UserId != 0) { schools = schools.Where(s => s.Follows.Any(f => f.UserId == schoolPaginationParams.UserId)); } if (schools.Count() == 0) { return(PagedList <School> .Create(schools.AsQueryable(), schoolPaginationParams.PageNumber, schoolPaginationParams.PageSize)); } float totalRatingAverage = schools.Average(s => s.Rating); int totalOpinionsCount = 0; schools.ToList().ForEach(s => totalOpinionsCount += s.Opinions.Count); schools = schools.OrderByDescending(s => s.Rating >= 3 ? (s.Rating * totalOpinionsCount + totalRatingAverage * s.Opinions.Count) * 100 / totalOpinionsCount : s.Rating * 100); if (!isAdmin) { schools = schools.Where(s => s.Accepted); } else { if (!string.IsNullOrEmpty(schoolPaginationParams.AcceptedStatus)) { switch (schoolPaginationParams.AcceptedStatus) { case "All": break; case "Accepted": schools = schools.Where(s => s.Accepted); break; case "Unaccepted": schools = schools.Where(s => !s.Accepted); break; default: break; } } } if (!string.IsNullOrEmpty(schoolPaginationParams.OrderBy)) { switch (schoolPaginationParams.OrderBy) { case "Rating": break; case "Follows": schools = schools.OrderByDescending(s => s.Follows.Count); break; default: break; } } if (!string.IsNullOrEmpty(schoolPaginationParams.Name)) { schools = schools.Where(s => s.Name.ToLower().Contains(schoolPaginationParams.Name.ToLower())); } if (!string.IsNullOrEmpty(schoolPaginationParams.State)) { schools = schools.Where(s => s.State.ToLower().Contains(schoolPaginationParams.State.ToLower())); } if (!string.IsNullOrEmpty(schoolPaginationParams.City)) { schools = schools.Where(s => s.City.ToLower().Contains(schoolPaginationParams.City.ToLower())); } if (schoolPaginationParams.IsRental) { schools = schools.Where(s => s.IsRental); } return(PagedList <School> .Create(schools.AsQueryable(), schoolPaginationParams.PageNumber, schoolPaginationParams.PageSize)); }