public async Task <ServiceResponse <IEnumerable <GetBasketWithoutItemDto> > > GetAllBasketsAsync(PaginationParam pagination, QueryBasketDto query) { var response = new ServiceResponse <IEnumerable <GetBasketWithoutItemDto> >(); try { var dbBasketsQuery = _context.Baskets.AsQueryable(); if (query.From.HasValue) { // GTE dbBasketsQuery = dbBasketsQuery.Where(basket => basket.UpdatedAt.CompareTo(query.From.Value) > -1); } if (query.To.HasValue) { // LTE dbBasketsQuery = dbBasketsQuery.Where(basket => basket.UpdatedAt.CompareTo(query.To.Value) < 1); } if (query.Status.HasValue) { dbBasketsQuery = dbBasketsQuery.Where(basket => basket.Status == query.Status); } var dbBaskets = await dbBasketsQuery .Include(x => x.Customer) .Skip((pagination.Page - 1) * pagination.PerPage) .Take(pagination.PerPage) .ToListAsync(); var totalBasketQuantity = await dbBasketsQuery .Include(x => x.BasketItems) .CountAsync(); response.Data = _mapper.Map <IEnumerable <GetBasketWithoutItemDto> >(dbBaskets); response.Pagination = PaginationHelper.CreatePagination(pagination, totalBasketQuantity); return(response); } catch (BaseServiceException ex) { response.Success = false; response.Message = ex.ErrorMessage; response.Code = ex.Code; _logger.LogError(ex.Message, ex.StackTrace); return(response); } catch (Exception ex) { response.Success = false; response.Message = ex.Message; response.Code = ErrorCode.BASKET_UNEXPECTED_ERROR; _logger.LogError(ex.Message, ex.StackTrace); return(response); } }
public async Task <IActionResult> GetAllBaskets([FromQuery] PaginationParam pagination, [FromQuery] QueryBasketDto query) { var res = await _service.GetAllBasketsAsync(pagination, query); if (res.Success) { return(Ok(res)); } return(BadRequest(res)); }