Пример #1
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);
            }
        }
Пример #2
0
        public async Task <PagedResults <IEnumerable <ProductServiceModel> > > GetByIdsAsync(GetProductsByIdsServiceModel model)
        {
            var searchResults = await this.productSearchRepository.GetAsync(model.Language, model.OrganisationId, model.Ids, model.OrderBy);

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