public bool EditDraft(EditArticleViewModel model) { Article draftToEdit = dbContext.Articles.Find(model?.Id); if (draftToEdit == null) { return(false); } if (model.ArticleImage != null) { string webRootPath = hostingEnvironment.WebRootPath; string imageUrl = @$ "{webRootPath}/Images/{model?.ImageName}.jpg"; using (var fileStream = new FileStream(imageUrl, FileMode.Create)) { model.ArticleImage.CopyTo(fileStream); } model.ImageUrl = $@"\Images\{model.ImageName}.jpg"; byte[] myBinaryImage = File.ReadAllBytes(imageUrl); var resizzedImage = SkiaSharpImageManipulationProvider.ResizeStaticProportions(myBinaryImage, 460); File.WriteAllBytes(imageUrl, resizzedImage.FileContents); } else { draftToEdit.ImageUrl = model.ImageUrl; } draftToEdit.Body = model.Body; draftToEdit.CategoryId = model.CategoryId; draftToEdit.ImageUrl = model.ImageUrl; draftToEdit.EnableComments = model.EnableComments; draftToEdit.H1Tag = model.H1Tag; draftToEdit.LastModDate = DateTime.UtcNow; draftToEdit.SourceName = model.SourceName; draftToEdit.SourceURL = model.SourceURL; draftToEdit.Title = model.Title; draftToEdit.LastModDate = DateTime.UtcNow; dbContext.SaveChanges(); ArticleSeoData articleSeoDataToEdit = dbContext.ArticlesSeoData.Where(x => x.ArticleId == draftToEdit.Id).FirstOrDefault(); articleSeoDataToEdit.MetaDescription = model.MetaDescription; articleSeoDataToEdit.MetaKeyword = model.MetaKeyword; articleSeoDataToEdit.MetaTitle = model.MetaTitle; articleSeoDataToEdit.SeoUrl = model.SeoUrl; dbContext.SaveChanges(); return(true); }
public bool AddNewDraft(AddArticleViewModel model) { if (model == null) { return(false); } string webRootPath = hostingEnvironment.WebRootPath; string imageUrl = @$ "{webRootPath}/Images/{model?.ImageName}.jpg"; Article article = mapper.Map <Article>(model); if (model.ArticleImage != null) { using (var fileStream = new FileStream(imageUrl, FileMode.Create)) { model.ArticleImage.CopyTo(fileStream); } byte[] myBinaryImage = File.ReadAllBytes(imageUrl); var resizzedImage = SkiaSharpImageManipulationProvider.ResizeStaticProportions(myBinaryImage, 460); File.WriteAllBytes(imageUrl, resizzedImage.FileContents); article.ImageUrl = $@"\Images\{model.ImageName}.jpg"; } article.CreationDate = DateTime.UtcNow; article.LastModDate = DateTime.UtcNow; article.State = ArticleState.Draft; article.CategoryId = model.CategoryId; dbContext.Articles.Add(article); dbContext.SaveChanges(); ArticleSeoData seoData = mapper.Map <ArticleSeoData>(model); seoData.ArticleId = dbContext.Articles.Where(x => x == article).FirstOrDefault().Id; seoData.SeoUrl = article.Title.Replace(" ", "-"); dbContext.ArticlesSeoData.Add(seoData); dbContext.SaveChanges(); return(true); }