Esempio n. 1
0
        public async Task <PaginatedResponse <List <Order> > > GetUserOrders(IndexOrderVMRequest paginationDTO)
        {
            var responseHTTP = await _httpService.Post2 <IndexOrderVMRequest, List <Order> >($"{url}/GetUserOrders", paginationDTO);

            var totalAmountPages  = int.Parse(responseHTTP.HttpResponseMessage.Headers.GetValues("totalAmountPages").FirstOrDefault());
            var totalAmount       = int.Parse(responseHTTP.HttpResponseMessage.Headers.GetValues("totalAmount").FirstOrDefault());
            var paginatedResponse = new PaginatedResponse <List <Order> >()
            {
                Response         = responseHTTP.Response,
                TotalAmountPages = totalAmountPages,
                TotalAmount      = totalAmount
            };

            return(paginatedResponse);
        }
        public async Task <ActionResult> UserOrders(IndexOrderVMRequest paginationDTO)
        {
            try
            {
                #region Start the watch
                var watch = new Stopwatch();
                watch.Start();
                #endregion

                List <Order> list = new();
                var          user = await _userCServices.GetCurrent(User.Identity.Name);

                long userId = user != null ? user.Id : 0;

                if (String.IsNullOrWhiteSpace(paginationDTO.SearchText))
                {
                    list = await _entityRepository.TableNoTracking
                           .Where(x => x.UserCId == userId)
                           .Include(x => x.Product).ThenInclude(x => x.Store)
                           .Include(x => x.StorePaymentMethod).ThenInclude(x => x.PaymentMethod)
                           .ToListAsync();
                }
                else
                {
                    list = await _entityRepository.TableNoTracking
                           .Include(x => x.Product)
                           .Include(x => x.StorePaymentMethod).ThenInclude(x => x.PaymentMethod)
                           .Where(x => x.Product.Name.ToLower().Contains(paginationDTO.SearchText.ToLower()) && x.UserCId == userId)
                           .ToListAsync();
                }

                if (paginationDTO.OrderStatus != null)
                {
                    list = list.Where(x => x.Status == paginationDTO.OrderStatus).ToList();
                }

                if (paginationDTO.From != null)
                {
                    list = list.Where(x => x.OrderDate >= paginationDTO.From).ToList();
                }

                if (paginationDTO.To != null)
                {
                    list = list.Where(x => x.OrderDate <= paginationDTO.To).ToList();
                }

                var listCount = list.Count;
                await HttpContext.InsertPaginationParametersInResponseList(list, paginationDTO.RecordsPerPage);

                list = list.PaginateSearch(paginationDTO.Pagination);

                var page       = paginationDTO.Page;
                var totalPages = TotalPagesCount.Value(listCount, paginationDTO.RecordsPerPage);

                var result = ApiPaginationResponse.Create(HttpStatusCode.OK, page, totalPages, list);

                #region End the watch
                watch.Stop();
                result.Meta.TotalProcessingTime = watch.ElapsedMilliseconds;
                #endregion

                return(Ok(result));
            }
            catch (Exception)
            {
                var result = ApiResponse.Create(HttpStatusCode.BadRequest, null, "InternalServerError_Error");
                return(Ok(result));
            }
        }