public async Task <IActionResult> GetRentals([FromQuery] RentalPaginationParams rentalPaginationParams) { bool isAdmin = false; int currentUserId = 0; if (HttpContext.User.FindFirst(ClaimTypes.NameIdentifier) != null) { currentUserId = int.Parse(HttpContext.User.FindFirst(ClaimTypes.NameIdentifier).Value); if (currentUserId != rentalPaginationParams.UserId && rentalPaginationParams.UserId != 0) { return(BadRequest("Niepoprawne dane wejściowe")); } isAdmin = await rolesService.IsPermitted(RolesPermitted, currentUserId); } else { rentalPaginationParams.UserId = 0; } var rentals = await database.RentalRepository.GetPagedRentals(rentalPaginationParams, isAdmin); var rentalsToReturn = mapper.Map <ICollection <RentalForBrowserDto> >(rentals); Response.AddPagination(rentals.CurrentPage, rentals.PageSize, rentals.TotalCount, rentals.TotalPages); return(Ok(rentalsToReturn)); }
public async Task <PagedList <Rental> > GetPagedRentals(RentalPaginationParams rentalPaginationParams, bool isAdmin = false) { var rentals = await GetWhere <Rental>(r => r.Rating >= rentalPaginationParams.Rating && !r.Owner.IsBlocked); if (rentalPaginationParams.UserId != 0) { rentals = rentals.Where(r => r.RentalFollows.Any(f => f.UserId == rentalPaginationParams.UserId)); } if (rentals.Count() == 0) { return(PagedList <Rental> .Create(rentals.AsQueryable(), rentalPaginationParams.PageNumber, rentalPaginationParams.PageSize)); } float totalRatingAverage = rentals.Average(r => r.Rating); int totalOpinionsCount = 0; rentals.ToList().ForEach(r => totalOpinionsCount += r.RentalOpinions.Count); rentals = rentals.OrderByDescending(r => r.Rating >= 3 ? (r.Rating * totalOpinionsCount + totalRatingAverage * r.RentalOpinions.Count) * 100 / totalOpinionsCount : r.Rating * 100); if (!isAdmin) { rentals = rentals.Where(r => r.Accepted); } else { switch (rentalPaginationParams.AcceptedStatus) { case "Accepted": rentals = rentals.Where(r => r.Accepted); break; case "UnAccepted": rentals = rentals.Where(r => !r.Accepted); break; default: break; } } if (!string.IsNullOrEmpty(rentalPaginationParams.Name)) { rentals = rentals.Where(r => r.Name.Contains(rentalPaginationParams.Name)); } if (!string.IsNullOrEmpty(rentalPaginationParams.State)) { rentals = rentals.Where(r => r.RentalPlaces.Where(p => p.State == rentalPaginationParams.State).Count() != 0); } if (!string.IsNullOrEmpty(rentalPaginationParams.City)) { rentals = rentals.Where(r => r.RentalPlaces.Where(p => p.City == rentalPaginationParams.City).Count() != 0); } switch (rentalPaginationParams.OrderBy) { case "SkiSetPricePerDay": rentals = rentals.Where(r => r.SkiSetPricePerDay != null).OrderBy(r => r.SkiSetPricePerDay); break; case "SnowboardSetPricePerDay": rentals = rentals.Where(r => r.SnowboardSetPricePerDay != null).OrderBy(r => r.SnowboardSetPricePerDay); break; case "IdDescending": rentals = rentals.OrderByDescending(r => r.Id); break; case "IdAscending": rentals = rentals.OrderBy(r => r.Id); break; default: break; } return(PagedList <Rental> .Create(rentals.AsQueryable(), rentalPaginationParams.PageNumber, rentalPaginationParams.PageSize)); }