public async Task <IActionResult> CreateUpdateArticle(int?id) { ViewBag.ReaderTypes = MyEnumExtensions.EnumToSelectList <ArticleReaderType>().ToList(); ViewBag.ContentCategories = await _dbContext.ContentCategories.Select(x => new SelectListItem { Value = x.Id.ToString(), Text = Lng == Lang.AR ? x.Title_Ar : Lng == Lang.KU ? x.Title_Ku : x.Title }).ToListAsync(); var model = new CmsArticleCreateUpdateViewModel { Id = id }; if (id != null) { var article = await _dbContext.ContentArticles.FindAsync(id); if (article == null) { throw new AwroNoreException("Article not found"); } if (!string.IsNullOrEmpty(article.ThumbnailUrl)) { ViewBag.AvatarPreview = "<img src=" + article.ThumbnailUrl + " alt=\"Image\">"; } if (!string.IsNullOrEmpty(article.ThumbnailUrl_Ku)) { ViewBag.AvatarPreviewKu = "<img src=" + article.ThumbnailUrl_Ku + " alt=\"Image\">"; } if (!string.IsNullOrEmpty(article.ThumbnailUrl_Ar)) { ViewBag.AvatarPreviewAr = "<img src=" + article.ThumbnailUrl_Ar + " alt=\"Image\">"; } model = _mapper.Map <CmsArticleCreateUpdateViewModel>(article); } return(PartialView(model)); }
public async Task <IActionResult> CreateUpdateArticle(CmsArticleCreateUpdateViewModel model) { var success = false; var message = Core.Resources.Global.Global.Err_ErrorOccured; var strategy = _dbContext.Database.CreateExecutionStrategy(); await strategy.ExecuteAsync(async() => { using (var transaction = _dbContext.Database.BeginTransaction()) { ContentArticle article; if (model.Id != null) { article = await _dbContext.ContentArticles.FindAsync(model.Id); if (article == null) { throw new AwroNoreException("Article not found"); } article.Title = model.Title; article.Title_Ar = model.Title_Ar; article.Title_Ku = model.Title_Ku; article.Summary = model.Summary; article.Summary_Ar = model.Summary_Ar; article.Summary_Ku = model.Summary_Ku; article.Body = model.Body; article.Body_Ar = model.Body_Ar; article.Body_Ku = model.Body_Ku; article.ContentCategoryId = model.ContentCategoryId; article.IsPublished = model.IsPublished; article.ReaderType = model.ReaderType; article.UpdatedAt = DateTime.Now; _dbContext.Entry(article).State = EntityState.Modified; await _dbContext.SaveChangesAsync(); message = Messages.ItemUpdatedSuccessFully; } else { article = _mapper.Map <ContentArticle>(model); await _dbContext.ContentArticles.AddAsync(article); await _dbContext.SaveChangesAsync(); message = Messages.ItemAddedSuccessFully; } if (model.Image != null) { var(newName, thumbName, dirPath, baseUrl) = _uploadService.GenerateCmsArticleImageName(article.Id, Lang.EN.ToString(), model.Image); article.ImageUrl = $"{baseUrl}/{newName}"; article.ThumbnailUrl = $"{baseUrl}/{thumbName}"; _dbContext.ContentArticles.Attach(article); _dbContext.Entry(article).State = EntityState.Modified; await _dbContext.SaveChangesAsync(); await _uploadService.UploadCmsArticleImageAsync(model.Image, dirPath, newName, thumbName); } if (model.Image_Ku != null) { var(newName, thumbName, dirPath, baseUrl) = _uploadService.GenerateCmsArticleImageName(article.Id, Lang.KU.ToString(), model.Image_Ku); article.ImageUrl_Ku = $"{baseUrl}/{newName}"; article.ThumbnailUrl_Ku = $"{baseUrl}/{thumbName}"; _dbContext.ContentArticles.Attach(article); _dbContext.Entry(article).State = EntityState.Modified; await _dbContext.SaveChangesAsync(); await _uploadService.UploadCmsArticleImageAsync(model.Image_Ku, dirPath, newName, thumbName); } if (model.Image_Ar != null) { var(newName, thumbName, dirPath, baseUrl) = _uploadService.GenerateCmsArticleImageName(article.Id, Lang.AR.ToString(), model.Image_Ar); article.ImageUrl_Ar = $"{baseUrl}/{newName}"; article.ThumbnailUrl_Ar = $"{baseUrl}/{thumbName}"; _dbContext.ContentArticles.Attach(article); _dbContext.Entry(article).State = EntityState.Modified; await _dbContext.SaveChangesAsync(); await _uploadService.UploadCmsArticleImageAsync(model.Image_Ar, dirPath, newName, thumbName); } if (model.SendNotification) { if (!model.IsPublished) { throw new AwroNoreException("For send notification, you must publish article"); } var title = article.Title ?? article.Title_Ku ?? article.Title_Ar; var titleKu = article.Title_Ku ?? article.Title ?? article.Title_Ar; var titleAr = article.Title_Ar ?? article.Title_Ku ?? article.Title; var body = article.Summary ?? article.Summary_Ku ?? article.Summary_Ar; var bodyKu = article.Summary_Ku ?? article.Summary ?? article.Summary_Ar; var bodyAr = article.Summary_Ar ?? article.Summary_Ku ?? article.Summary; var articleDTO = new CmsArticleDTO { Id = article.Id, CategoryId = article.ContentCategoryId, ReaderType = article.ReaderType, Title = title, Summary = body }; await _notificationService.SendPublishArticleNotificationAsync(new BaseNotificationPayload { Title = title, TitleKu = titleKu, TitleAr = titleAr, Body = body?.TruncateLongString(50), BodyKu = bodyKu?.TruncateLongString(50), BodyAr = bodyAr?.TruncateLongString(50), }, articleDTO); } transaction.Commit(); success = true; } }); return(Json(new { success, message })); }