コード例 #1
0
        public async Task <CustomizeViewModel> GetCustomizeItems(int?categoryid, int?catalogItemId)
        {
            var categorySpec = new CategorySpecification();
            var cats         = await _categoryRepository.ListAsync(categorySpec);

            List <CatalogType> productTypes = new List <CatalogType>();

            if (categoryid.HasValue)
            {
                var catalogSpec = new CatalogTypeSpecification(categoryid.Value);
                productTypes = await _catalogTypeRepository.ListAsync(catalogSpec);
            }

            return(new CustomizeViewModel
            {
                CategoryId = categoryid,
                CatalogItemId = catalogItemId,
                Categories = cats.Select(x => (x.Id, x.Name)).ToList(),
                ProductTypes = productTypes.Select(x => new CatalogTypeViewModel
                {
                    Id = x.Id,
                    Code = x.Code,
                    Name = x.Name,
                    PictureUri = _uriComposer.ComposePicUri(x.PictureUri)
                }).ToList()
            });
コード例 #2
0
        public async Task <IActionResult> UpdatePicturesAsync()
        {
            var spec     = new CatalogFilterSpecification(false);
            var products = await _catalogRepository.ListAsync(spec);

            //Add main Picture
            foreach (var item in products)
            {
                if (!string.IsNullOrEmpty(item.PictureUri) && !item.Pictures.Any(x => x.IsMain))
                {
                    item.AddPicture(new CatalogPicture(isActive: true, isMain: true, pictureUri: item.PictureUri, order: 0));
                    await _catalogRepository.UpdateAsync(item);
                }
            }
            //Set current Picture to PictureHighUri
            products = await _catalogRepository.ListAsync(spec);

            foreach (var product in products)
            {
                foreach (var picture in product.Pictures)
                {
                    if (!string.IsNullOrEmpty(picture.PictureUri) && string.IsNullOrEmpty(picture.PictureHighUri))
                    {
                        //Set pictureHighUri
                        picture.UpdatePictureUri(picture.PictureUri);
                        Uri uri      = new Uri(picture.PictureUri);
                        var name     = Utils.URLFriendly(Path.GetFileNameWithoutExtension(uri.LocalPath));
                        var fileName = name + Path.GetExtension(uri.LocalPath);
                        var fullPath = Path.Combine(_backofficeSettings.WebProductsPictureV2FullPath, fileName);
                        picture.UpdatePictureUri(_backofficeSettings.WebProductsPictureV2Uri + fileName);

                        //Get Image
                        var originalImage = Path.Combine(_backofficeSettings.WebProductsPictureFullPath, Path.GetFileName(uri.LocalPath));
                        using (Image image = Image.Load(originalImage))
                        {
                            image.Mutate(x => x
                                         .Resize(469, 469));

                            image.Save(fullPath); // Automatic encoder selected based on extension.
                        }
                    }
                }
                await _catalogRepository.UpdateAsync(product);
            }

            //Product Types
            var typesSpec = new CatalogTypeSpecification(true);
            var types     = await _catalogTypeRepository.ListAsync(typesSpec);

            foreach (var item in types)
            {
                //Main Picture
                if (!string.IsNullOrEmpty(item.PictureUri) && !item.PictureUri.Contains("v2"))
                {
                    Uri uri               = new Uri(item.PictureUri);
                    var fileName          = Path.GetFileName(uri.LocalPath);
                    var originalImagePath = Path.Combine(_backofficeSettings.WebProductTypesPictureFullPath, fileName);
                    var newImagePath      = Path.Combine(_backofficeSettings.WebProductTypesPictureV2FullPath, fileName);
                    using (Image image = Image.Load(originalImagePath))
                    {
                        image.Mutate(x => x
                                     .Resize(255, 116));

                        image.Save(newImagePath); // Automatic encoder selected based on extension.
                    }
                    item.UpdatePicture(_backofficeSettings.WebProductTypesPictureV2Uri + fileName);

                    if (item.PictureTextHelpers?.Count > 0)
                    {
                        foreach (var helper in item.PictureTextHelpers)
                        {
                            Uri uriHelper          = new Uri(helper.PictureUri);
                            var fileNameHelper     = Path.GetFileName(uriHelper.LocalPath);
                            var originalHelperPath = Path.Combine(_backofficeSettings.WebProductTypesPictureFullPath, fileNameHelper);
                            var newHelperPath      = Path.Combine(_backofficeSettings.WebProductTypesPictureV2FullPath, fileNameHelper);
                            using (Image image = Image.Load(originalHelperPath))
                            {
                                image.Mutate(x => x
                                             .Resize(112, 96));

                                image.Save(newHelperPath); // Automatic encoder selected based on extension.
                            }
                            helper.PictureUri = _backofficeSettings.WebProductTypesPictureV2Uri + fileNameHelper;
                            helper.Location   = newHelperPath;
                        }
                    }
                    await _catalogTypeRepository.UpdateAsync(item);
                }
            }

            return(Ok());
        }