Пример #1
0
        public async Task <ActionResult <ProductGetViewModel> > GetProduct(int id)
        {
            var product = await repository.GetByIdAsync(id);

            if (product == null)
            {
                return(NotFound());
            }

            return(ProductGetViewModel.FromProductEntity(product));
        }
Пример #2
0
        public async Task <ActionResult <IEnumerable <ProductGetViewModel> > > GetProduct()
        {
            var productList = await repository.ListAsync();

            return(Ok(productList.Select(p => ProductGetViewModel.FromProductEntity(p))));
        }
        public async Task <OkObjectResult> Get(ProductGetViewModel viewModel)
        {
            var entityIds = new List <long>();

            if (viewModel.EntityId != default)
            {
                entityIds.Add(viewModel.EntityId);
            }

            var filterServiceModel = AutoMapper.Mapper.Map <ProductFilterServiceModel>(viewModel);

            filterServiceModel.Ids = entityIds;

            var products = _productService.GetProducts(filterServiceModel);

            if (string.IsNullOrEmpty(viewModel.SortField) == false)
            {
                if (viewModel.SortField == "code")
                {
                    products = (viewModel.SortOrder == "ascend") ? products.OrderBy(o => o.Code) : products.OrderByDescending(o => o.Code);
                }
                if (viewModel.SortField == "title")
                {
                    products = (viewModel.SortOrder == "ascend") ? products.OrderBy(o => o.Details.FirstOrDefault().Title) : products.OrderByDescending(o => o.Details.FirstOrDefault().Title);
                }
            }

            object actionOkResult = null;

            if (viewModel.GetMode == (int)GetMode.Paginated)
            {
                var pageList = await PaginatedList <ProductEntity> .CreateAsync(products, viewModel.Page, viewModel.PageSize);

                var result = new PageEntityViewModel <ProductEntity, ProductViewModel>(pageList, entity => ProductViewModel.FromEntity(entity));

                actionOkResult = result;
            }
            else if (viewModel.GetMode == (int)GetMode.List)
            {
                var result = products.Select(o => ProductEditViewModel.FromEntity(o));
                actionOkResult = result;
            }
            else
            {
                var product = products.FirstOrDefault(o => o.Id == viewModel.EntityId || o.Name == viewModel.Name);

                var result = new ProductEditViewModel();

                if (product != null)
                {
                    result = ProductEditViewModel.FromEntity(product);
                }

                if (viewModel.IsEditModel)
                {
                    var allBrand = _productTaxonomiesService.GetAllBrand();
                    var allType  = _productTaxonomiesService.GetAllProductType();

                    result.AvaliableBrands       = allBrand.Select(o => TaxomonyViewModel.FromEntity(o));
                    result.AvaliableProductTypes = allType.Select(o => TaxomonyViewModel.FromEntity(o));
                }

                actionOkResult = result;
            }

            return(Ok(actionOkResult));
        }