コード例 #1
0
        public virtual async Task SendPublishArticleNotificationAsync(BaseNotificationPayload payload, CmsArticleDTO article)
        {
            var data = new
            {
                click_action = "FLUTTER_NOTIFICATION_CLICK",
                sound        = "default",
                status       = "done",
                screen       = "READ_ARTICLE",
                article,
                title   = payload.Title,
                titleKu = payload.TitleKu,
                titleAr = payload.TitleAr,
                body    = payload.Body,
                bodyKu  = payload.BodyKu,
                bodyAr  = payload.BodyAr
            };

            await SendFcmToTopicAsync("Public", payload.Title, payload.Body, data);
        }
コード例 #2
0
        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 }));
        }