public async Task SynchronizeAdlerImagesAsync(int itemCardId, ImportAdlerDto itemCardImport, CancellationToken cancellationToken) { var itemCardImageRepository = _unitOfWork.GetRepository <ItemCardImage>(); var images = await _itemCardImageQueries.GetEntitiesAsync(_ajkaShopDbContext, itemCardId, cancellationToken).ConfigureAwait(false); foreach (var imageRecord in images) { var checkImage = itemCardImport.ImagePaths.FirstOrDefault(s => s.ImagePath == imageRecord.ImagePath); if (checkImage != null) { imageRecord.ColorName = checkImage.ColorName; imageRecord.AvailableSizesList = checkImage.AvailableSizesList; itemCardImageRepository.Update(imageRecord); itemCardImport.ImagePaths.Remove(checkImage); } } if (itemCardImport.ImagePaths.Any()) { foreach (var image in itemCardImport.ImagePaths) { var imageForInsert = new ItemCardImage { ItemCardId = itemCardId, ColorName = image.ColorName, ImagePath = image.ImagePath }; await itemCardImageRepository.InsertAsync(imageForInsert).ConfigureAwait(false); } } await _unitOfWork.SaveChangesAsync().ConfigureAwait(false); }
public async Task UploadImageAsync(int itemCardId, MemoryStream stream, string fileExtension, CancellationToken cancellationToken) { var itemCardRepository = _unitOfWork.GetRepository <DAL.Model.ItemCard>(); var checkItemCard = await itemCardRepository.FindAsync(itemCardId).ConfigureAwait(false); if (checkItemCard == null) { return; } var uniqueFileName = Convert.ToString(Guid.NewGuid()); var directoryPath = Path.Combine(_environment.WebRootPath, ItemCardConstants.imagesMainDirectory, $@"{ItemCardConstants.itemCardSubdirectoryPrefix}{itemCardId}"); _fileProcessingService.CreateDirectory(directoryPath); var image = Image.FromStream(stream); var pathForImage = Path.Combine(directoryPath, $@"{uniqueFileName + fileExtension}"); _fileProcessingService.CreateScaledImage(image, pathForImage, ItemCardConstants.imageWidthImageSize); var itemCardImageRepository = _unitOfWork.GetRepository <ItemCardImage>(); var recordForAdd = new ItemCardImage { ItemCardId = itemCardId, ImagePath = Path.Combine(ItemCardConstants.imagesMainDirectory, $@"{ItemCardConstants.itemCardSubdirectoryPrefix}{itemCardId}", $@"{uniqueFileName + fileExtension}") }; recordForAdd.ImagePath = recordForAdd.ImagePath.Replace('\\', '/'); await itemCardImageRepository.InsertAsync(recordForAdd).ConfigureAwait(false); await _unitOfWork.SaveChangesAsync().ConfigureAwait(false); }