public bool Update(NewsVM model) { var news = new News(); try { model.Image = UploadHelper.GetImageName(model.Image, "news", "news"); model.CoverImage = UploadHelper.GetImageName(model.CoverImage, "news-cover", "news"); model.ShareImage = UploadHelper.GetImageName(model.ShareImage, "news-share", "news"); model.Slug = LanguageHelper.GenerateSlug(model.Slug); using (var scope = new TransactionScope()) { if (model.Id == 0) { news.Update(model); _db.News.Add(news); } else { news = _db.News.Include("Translations").Include(e => e.NewsToTags).Include(e => e.AttachedFiles).Where(x => x.Id == model.Id).FirstOrDefault(); news.Update(model); } news.UpdateTranslations(model.Translations); news.UpdateTags(model.TagsId); // attached files if (model.AttachedFiles != null && model.AttachedFiles.Count() > 0) { news.UpdateFiles(model.AttachedFiles); } //remove files if (model.Id != 0 && news.AttachedFiles != null) { var removedFiles = news.AttachedFiles.Where(x => !model.AttachedFiles.Any(q => q.Id == x.Id)).ToList(); _db.AttachedFiles.RemoveRange(removedFiles); } //remove category if (news.Id != 0 && model.TagsId != null) { var removedTags = _db.NewsToTags.Where(x => x.NewsId == news.Id && !model.TagsId.Contains(x.TagsId)).ToList(); _db.NewsToTags.RemoveRange(removedTags); } _db.SaveChanges(); scope.Complete(); } return(true); } catch (Exception ex) { logger.Error(ex); return(false); } }
public JsonResult GenerateSlug(string slug) { var index = 0; var generatedSlug = LanguageHelper.GenerateSlug(slug); var newSug = generatedSlug; while (CheckSlug(newSug)) { index++; newSug = generatedSlug + "-" + index.ToString(); } return(Json(new { slug = newSug })); }