public async Task <Article> SaveArticleImages(Article storedArticle, ArticleImagesDTO articleImages) { if (articleImages.FileThumbnail != null) { await _mediator.Send(new UploadPhotosCommand { AlbumId = storedArticle.AlbumId.Value, LocalFiles = new List <IFormFile>() { articleImages.FileThumbnail }, PhotoType = PhotoType.Thumbnail }); //storedArticle.PhotoThumbnailId = thumbnailList.Count > 0 ? (int?)thumbnailList[0].Id : null; } // Upload header if (articleImages.FileHeader != null) { await _mediator.Send(new UploadPhotosCommand { AlbumId = storedArticle.AlbumId.Value, LocalFiles = new List <IFormFile>() { articleImages.FileHeader }, PhotoType = PhotoType.Header }); //storedArticle.PhotoHeaderId = headerList.Count > 0 ? (int?)headerList[0].Id : null; } // Upload others to album if (articleImages.Files != null && articleImages.Files.Count > 0) { await _mediator.Send(new UploadPhotosCommand { AlbumId = storedArticle.AlbumId.Value, LocalFiles = articleImages.Files, PhotoType = PhotoType.Image }); } // Download thumbnail if (articleImages.RemoteFileThumbnail != null && articleImages.FileThumbnail == null) { await _mediator.Send(new UploadPhotosCommand { AlbumId = storedArticle.AlbumId.Value, Files = new List <string>() { articleImages.RemoteFileThumbnail }, PhotoType = PhotoType.Thumbnail }); //storedArticle.PhotoThumbnailId = thumbnailList.Count > 0 ? (int?)thumbnailList[0].Id : null; } // Download header if (articleImages.RemoteFileHeader != null && articleImages.FileHeader == null) { await _mediator.Send(new UploadPhotosCommand { AlbumId = storedArticle.AlbumId.Value, Files = new List <string>() { articleImages.RemoteFileHeader }, PhotoType = PhotoType.Header }); //storedArticle.PhotoHeaderId = headerList.Count > 0 ? (int?)headerList[0].Id : null; } // Upload others to album var remoteFiles = articleImages.RemoteFiles != null?articleImages.RemoteFiles.Split(';').ToList() : new List <string>(); if (remoteFiles.Count > 0) { await _mediator.Send(new UploadPhotosCommand { AlbumId = storedArticle.AlbumId.Value, Files = remoteFiles, PhotoType = PhotoType.Image }); } var albumPhotos = await _mediator.Send(new GetAlbumPhotosDetailQuery { AlbumId = storedArticle.AlbumId.Value }); var headerPhoto = albumPhotos.FirstOrDefault(x => x.Type == PhotoType.Header); var thumbnailPhoto = albumPhotos.FirstOrDefault(x => x.Type == PhotoType.Thumbnail); storedArticle.PhotoHeaderId = headerPhoto?.Id; storedArticle.PhotoThumbnailId = thumbnailPhoto?.Id; return(storedArticle); }
public async Task <int> Handle(CreateArticleCommand request, CancellationToken cancellationToken) { using (var transaction = await _context.BeginTransactionAsync()) { try { var articleEntity = new Article { Project = await _context.Projects.FindAsync(request.ProjectId), Title = request.Title, Description = request.Description, CategoryId = request.CategoryId, Content = request.Content, Status = ArticleStatus.Inactive, Slug = request.Title.GenerateSlug(), Seo = request.Seo.IsEmpty ? null : new Seo { Title = request.Seo.Title, Description = request.Seo.Description, Keywords = request.Seo.Keywords } }; _context.Articles.Add(articleEntity); await _context.SaveChangesAsync(cancellationToken); // Create article settings var articleSettings = _mapper.Map <ArticleSettings>(new ArticleSettingsDTO()); articleSettings.ArticleId = articleEntity.Id; _context.ArticleSettings.Add(articleSettings); await _context.SaveChangesAsync(cancellationToken); // Create album for article articleEntity.AlbumId = await _articleAlbumService.CreateAlbum(articleEntity); await _context.SaveChangesAsync(cancellationToken); // Upload images ArticleImagesDTO ArticleImagesDTO = _mapper.Map <ArticleImagesDTO>(request); articleEntity = await _articleAlbumService.SaveArticleImages(articleEntity, ArticleImagesDTO); // Process article content List <Photo> images = await _context.Photos .Where(x => x.AlbumId == articleEntity.AlbumId) .Where(x => x.Type == PhotoType.Image) .ToListAsync(); articleEntity.Content = _articleProcessorService.ProcessArticleImageContent( articleEntity.Content, articleEntity.Project.DomainName, _mapper.Map <List <PhotoDTO> >(images)); await _context.SaveChangesAsync(cancellationToken); transaction.Commit(); return(articleEntity.Id); } catch (Exception e) { // @TODO handle uploaded images rollback transaction.Rollback(); throw new EntityStoreException($"{nameof(Article)} store exception with message {e.Message}"); } } }
public async Task <Unit> Handle(UpdateArticleCommand request, CancellationToken cancellationToken) { using (var transaction = await _context.BeginTransactionAsync()) { try { var articleEntity = await _context.Articles .Include(x => x.Project) .Include(x => x.PhotoHeader) .Include(x => x.PhotoThumbnail) .Include(x => x.Seo) .Include(x => x.ArticleSettings) .Include(x => x.Category) .FirstOrDefaultAsync(x => x.Id == request.Id); if (articleEntity == null) { throw new EntityNotFoundException(nameof(Article), request.Id); } var seo = articleEntity.Seo; if (seo == null && !request.Seo.IsEmpty) { articleEntity.Seo = new Seo(); } if (seo != null) { articleEntity.Seo.Title = request.Seo.Title; articleEntity.Seo.Description = request.Seo.Description; articleEntity.Seo.Keywords = request.Seo.Keywords; } articleEntity.Title = request.Title; articleEntity.Description = request.Description; articleEntity.CategoryId = request.CategoryId; articleEntity.Content = request.Content; //articleEntity.ArticleSettings =_mapper.Map<ArticleSettings>(request.ArticleSettings); Mapper.Map <ArticleSettingsDTO, ArticleSettings>(request.ArticleSettings, articleEntity.ArticleSettings); await _context.SaveChangesAsync(cancellationToken); // Upload images ArticleImagesDTO ArticleImagesDTO = _mapper.Map <ArticleImagesDTO>(request); articleEntity = await _articleAlbumService.SaveArticleImages(articleEntity, ArticleImagesDTO); // Process article content List <Photo> images = await _context.Photos .Where(x => x.AlbumId == articleEntity.AlbumId) .Where(x => x.Type == PhotoType.Image) .ToListAsync(); articleEntity.Content = _articleProcessorService.ProcessArticleImageContent( articleEntity.Content, articleEntity.Project.DomainName, _mapper.Map <List <PhotoDTO> >(images)); await _context.SaveChangesAsync(cancellationToken); transaction.Commit(); return(Unit.Value); } catch (EntityNotFoundException e) { throw e; } catch (Exception e) { // @TODO handle uploaded images rollback transaction.Rollback(); throw new EntityStoreException($"{nameof(Article)} store exception with message {e.Message}"); } } }