public async Task <PagedListDto <ContestDto> > GetContests(GetContestsDto dto) { if (!dto.IsDtoValid()) { throw new ValidationException(); } var query = _db.Contests .Where(x => x.EndDate >= DateTime.UtcNow && x.IsPublished == dto.IsPublished); if (!string.IsNullOrWhiteSpace(dto.Search)) { query = query.Where(e => e.Title.Contains(dto.Search)); } if (!string.IsNullOrWhiteSpace(dto.City)) { query = query.Where(x => x.City.Contains(dto.City) || x.AcrossCountry); } if (dto.Sort == "Popular") { query = query.OrderByDescending(e => e.Views); } else if (dto.Sort == "Old") { query = query.OrderBy(e => e.PublishDate); } else if (dto.Sort == "AlmostClosed") { query = query.OrderBy(e => e.EndDate); } else { query = query.OrderByDescending(e => e.PublishDate); } var totalCount = query.Count(); var entities = await query.Skip(dto.PageSize *(dto.PageNumber - 1)) .Take(dto.PageSize) .ToListAsync(); var contests = _mapper.Map <List <ContestEntity>, List <ContestDto> >(entities); foreach (var contest in contests) { contest.EndDateString = contest.EndDate.ParseToDateAndMonth(); contest.PublishDateString = contest.PublishDate?.ParseToTimeDifference(); } return(new PagedListDto <ContestDto>(dto.PageNumber, dto.PageSize, totalCount, contests, dto.Sort, dto.Search)); }
public async Task <IActionResult> GetContests([FromQuery] GetContestsDto dto) { try { var contests = await _contestService.GetContests(dto); return(Ok(contests)); } catch (ValidationException) { return(BadRequest("Неправильный формат одного или нескольких параметров")); } }