public async Task <IActionResult> Update([FromForm] ProductImageUpdateViewModel viewModel)
        {
            var result = await _productImage.UpdateProducutImage(viewModel);


            if (result == 0)
            {
                return(BadRequest());
            }
            else
            {
                var productImage = _productImage.GetProductImageById(viewModel.Id);
                return(new OkObjectResult(productImage));
            }
        }
예제 #2
0
        public async Task <int> UpdateProducutImage(ProductImageUpdateViewModel viewModel)
        {
            var productImage = await(from pi in _context.productImages
                                     where pi.Id == viewModel.Id
                                     select new ProductImageSearchViewModel()
            {
                Id          = pi.Id,
                Caption     = pi.Caption,
                DateCreated = pi.CreateDate,
                FileSize    = pi.FileSize,
                ImagePath   = pi.ImagePath,
                IsDefault   = pi.IsDefault,
                ProductId   = pi.ProductId,
                SortOrder   = pi.SortOrder
            }).FirstOrDefaultAsync();

            if (productImage == null)
            {
                return(0);
            }
            else
            {
                productImage.SortOrder = viewModel.SortOrder;
                productImage.Caption   = viewModel.Caption;
                if (viewModel.ImageFile != null)
                {
                    productImage.ImagePath = await SaveFile(viewModel.ImageFile);

                    productImage.FileSize = viewModel.ImageFile.Length;
                }
            }
            await _context.SaveChangesAsync();

            await _storageService.SaveFileAsync(viewModel.ImageFile.OpenReadStream(), productImage.ImagePath);

            return(productImage.Id);
        }