public async Task <GetPaged.Response> GetPaged(GetPaged.Request request, CancellationToken cancellationToken) { var total = await _repository.Count( cancellationToken ); if (total == 0) { return(new GetPaged.Response { Items = Array.Empty <GetPaged.Response.AdResponse>(), Total = total, Offset = request.Offset, Limit = request.Limit }); } var ads = await _repository.GetPaged( request.Offset, request.Limit, cancellationToken ); return(new GetPaged.Response { Items = ads.Select(ad => new GetPaged.Response.AdResponse { Id = ad.Id, Name = ad.Name, Price = ad.Price, Status = ad.Status.ToString() }), Total = total, Offset = request.Offset, Limit = request.Limit }); }
public int Count() { return(_repository.Count()); }
public async Task <GetPagedAdResponse> GetPagedAd(GetPagedAdRequest request, CancellationToken cancellationToken) { var total = await _repository.Count(cancellationToken); if (total == 0) { return(_mapper.Map <GetPagedAdResponse>(request)); } var authUserId = await _identityService.GetCurrentIdentityUserId(cancellationToken); var categoriesId = new List <Guid>(); if (request.SearchCategoryId != Guid.Empty) { var categoryResponse = await _categoryService.GetByIdCategory(new GetByIdCategoryRequest { Id = request.SearchCategoryId }, cancellationToken); var categoryTree = new Queue <Domain.Category>(); categoryTree.Enqueue(new Domain.Category() { Id = request.SearchCategoryId, Name = categoryResponse.Name, ParentCategoryId = categoryResponse.ParentCategoryId, ChildCategories = categoryResponse.ChildCategories }); while (categoryTree.Count > 0) { var current = categoryTree.Dequeue(); categoriesId.Add(current.Id); foreach (var currentSubCategory in current.ChildCategories) { categoryTree.Enqueue(currentSubCategory); } } } GetLocationKladrIdStartAndEnd(request.SearchLocationKladrId, out var searchLocationKladrIdStart, out var searchLocationKladrIdEnd); var ads = await _repository.FindWhere( find => find.Name.Contains(request.SearchName) && ((find.CategoryId != null && categoriesId.Contains((Guid)find.CategoryId)) || request.SearchCategoryId == Guid.Empty) && ((find.LocationKladrId >= searchLocationKladrIdStart && find.LocationKladrId < searchLocationKladrIdEnd) || request.SearchLocationKladrId == null) && (find.OwnerId == request.SearchOwnerId || request.SearchOwnerId == Guid.Empty) && (request.SearchOwnerId == Guid.Empty && find.Status == Statuses.OnSale || request.SearchOwnerId == find.OwnerId && request.SearchOwnerId.ToString() == authUserId.Id || request.SearchOwnerId == find.OwnerId && request.SearchOwnerId.ToString() != authUserId.Id && find.Status == Statuses.OnSale) && (find.OwnerId == request.SearchOwnerId || find.Status != Statuses.Rejected) , cancellationToken); return(new GetPagedAdResponse { Items = SortAd(request.SortParam, request.SortDirection, ads) .Skip(request.Offset) .Take(request.Limit) .Select(a => _mapper.Map <GetPagedAdResponseItem>(a)), Total = ads.Count(), Offset = request.Offset, Limit = request.Limit }); }