public async Task <PagedResult <ProductServiceModel> > GetAsync(GetProductsServiceModel model)
        {
            var result = await _repository.GetProductsAsync(new GetProductsRepositoryModel
            {
                StartIndex = model.StartIndex,
                MaxRecords = model.MaxRecords
            });

            return(new PagedResult <ProductServiceModel>(result.Select(o => new ProductServiceModel(o)), result.TotalElements));
        }
Beispiel #2
0
        public async Task <IActionResult> Get(
            string ids,
            Guid?categoryId,
            Guid?sellerId,
            bool?hasPrimaryProduct,
            bool?isNew,
            string searchTerm,
            int pageIndex,
            int itemsPerPage,
            string orderBy)
        {
            var productIds = ids.ToEnumerableGuidIds();

            if (productIds != null)
            {
                var serviceModel = new GetProductsByIdsServiceModel
                {
                    Ids          = productIds,
                    PageIndex    = pageIndex,
                    ItemsPerPage = itemsPerPage,
                    OrderBy      = orderBy,
                    Language     = CultureInfo.CurrentCulture.Name
                };

                var validator = new GetProductsByIdsModelValidator();

                var validationResult = await validator.ValidateAsync(serviceModel);

                if (validationResult.IsValid)
                {
                    var products = await this.productService.GetByIdsAsync(serviceModel);

                    if (products != null)
                    {
                        var response = new PagedResults <IEnumerable <ProductResponseModel> >(products.Total, products.PageSize)
                        {
                            Data = products.Data.OrEmptyIfNull().Select(x => MapProductServiceModelToProductResponseModel(x))
                        };

                        return(this.StatusCode((int)HttpStatusCode.OK, response));
                    }
                }

                throw new CustomException(string.Join(ErrorConstants.ErrorMessagesSeparator, validationResult.Errors.Select(x => x.ErrorMessage)), (int)HttpStatusCode.UnprocessableEntity);
            }
            else
            {
                var serviceModel = new GetProductsServiceModel
                {
                    PageIndex         = pageIndex,
                    ItemsPerPage      = itemsPerPage,
                    SearchTerm        = searchTerm,
                    CategoryId        = categoryId,
                    OrganisationId    = sellerId,
                    OrderBy           = orderBy,
                    Language          = CultureInfo.CurrentCulture.Name,
                    HasPrimaryProduct = hasPrimaryProduct,
                    IsNew             = isNew
                };

                var validator = new GetProductsModelValidator();

                var validationResult = await validator.ValidateAsync(serviceModel);

                if (validationResult.IsValid)
                {
                    var products = await this.productService.GetAsync(serviceModel);

                    if (products != null)
                    {
                        var response = new PagedResults <IEnumerable <ProductResponseModel> >(products.Total, products.PageSize)
                        {
                            Data = products.Data.OrEmptyIfNull().Select(x => MapProductServiceModelToProductResponseModel(x))
                        };

                        return(this.StatusCode((int)HttpStatusCode.OK, response));
                    }
                }

                throw new CustomException(string.Join(ErrorConstants.ErrorMessagesSeparator, validationResult.Errors.Select(x => x.ErrorMessage)), (int)HttpStatusCode.UnprocessableEntity);
            }
        }
Beispiel #3
0
        public async Task <PagedResults <IEnumerable <ProductServiceModel> > > GetAsync(GetProductsServiceModel model)
        {
            var searchResults = await this.productSearchRepository.GetAsync(
                model.Language,
                model.CategoryId,
                model.OrganisationId,
                model.HasPrimaryProduct,
                model.IsNew,
                model.SearchTerm,
                model.PageIndex,
                model.ItemsPerPage,
                model.OrderBy);

            return(await this.MapToPageResultsAsync(searchResults, model.Language, model.OrganisationId));
        }