コード例 #1
0
        public async Task <IActionResult> CreateUpdate(NotificationCreateUpdateViewModel model)
        {
            var strategy = _dbContext.Database.CreateExecutionStrategy();
            await strategy.ExecuteAsync(async() =>
            {
                using (var transaction = _dbContext.Database.BeginTransaction())
                {
                    var notification = _mapper.Map <Notification>(model);

                    await _dbContext.Notifications.AddAsync(notification);

                    await _dbContext.SaveChangesAsync();

                    var(newName, thumbName, dirPath, baseUrl) = _uploadService.GenerateNotificationImageName(notification.Id, model.Image);

                    notification.Image = $"{baseUrl}/{newName}";

                    _dbContext.Notifications.Attach(notification);

                    _dbContext.Entry(notification).State = EntityState.Modified;

                    await _dbContext.SaveChangesAsync();

                    await _uploadService.UploadNotificationImageAsync(model.Image, dirPath, newName, thumbName);

                    var notificationDTO = new NotificationListItemDTO
                    {
                        Id            = notification.Id,
                        Text          = notification.Text ?? notification.Text_Ku ?? notification.Text_Ar,
                        TextKu        = notification.Text_Ku ?? notification.Text ?? notification.Text_Ar,
                        TextAr        = notification.Text_Ar ?? notification.Text_Ku ?? notification.Text,
                        Title         = notification.Title ?? notification.Title_Ku ?? notification.Title_Ar,
                        TitleKu       = notification.Title_Ku ?? notification.Title ?? notification.Title_Ar,
                        TitleAr       = notification.Title_Ar ?? notification.Title_Ku ?? notification.Title,
                        CreatedAt     = notification.CreatedAt,
                        Description   = notification.Description ?? notification.Description_Ku ?? notification.Description_Ar,
                        DescriptionKu = notification.Description_Ku ?? notification.Description ?? notification.Description_Ar,
                        DescriptionAr = notification.Description_Ar ?? notification.Description_Ku ?? notification.Description,
                        PayloadJson   = notification.PayloadJson,
                        Image         = notification.Image,
                        IsExpired     = DateTime.Now > notification.ValidUntil
                    };
                    await _notificationService.SendInboxNotificationAsync(new BaseNotificationPayload
                    {
                        Title   = notificationDTO.Title,
                        TitleKu = notificationDTO.TitleKu,
                        TitleAr = notificationDTO.TitleAr,
                        Body    = notificationDTO.Text?.TruncateLongString(50),
                        BodyKu  = notificationDTO.TextKu?.TruncateLongString(50),
                        BodyAr  = notificationDTO.TextAr?.TruncateLongString(50)
                    }, notificationDTO);

                    transaction.Commit();
                }
            });

            return(Json(new { success = true, message = Messages.ItemAddedSuccessFully }));
        }