public async Task <ActionResult> Post([FromBody] CircleTopicCommentReply model)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }
            var circleTopicComment = await _repo.GetCircleTopicComment(model.CommentId);

            if (circleTopicComment == null)
            {
                return(NotFound());
            }
            if (!await this.MatchAppUserWithToken(model.AppUserId) || !await _repo.IsMember((int)model.AppUserId, circleTopicComment.CircleId))
            {
                return(Unauthorized());
            }

            model.CircleId = circleTopicComment.CircleId;
            // var newTopic = this._mapper.Map<CircleTopic>(model);
            _repo.Add(model);
            await _repo.SaveAll();

            if (circleTopicComment.AppUserId == model.AppUserId) //Comment owner
            {
                await _notificationRepo.AddNotification(NotificationEnum.NewCircleCommentReplyByOwner, (int)model.AppUserId, model);
            }
            else
            {
                await _notificationRepo.AddNotification(NotificationEnum.NewCircleCommentReplyByMember, (int)model.AppUserId, model);
            }
            await _repo.SaveAll();

            return(CreatedAtRoute("GetCircleTopicCommentReply", new { id = model.Id }, _mapper.Map <CommentForReturnDto>(model)));
        }
        private async Task AddNewCircleCommentReplyByMemberNotification(int appUserId, CircleTopicCommentReply circleTopicCommentReply)
        {
            // var circleTopicComment = record as CircleTopicComment;
            if (circleTopicCommentReply == null)
            {
                return;
            }
            var circleTopicComment = await _context.CircleTopicComments.Include(ctc => ctc.CircleTopic).FirstOrDefaultAsync(ctc => ctc.Id == circleTopicCommentReply.CommentId);

            if (circleTopicComment == null)
            {
                return;
            }
            Dictionary <string, int> recordIds = new Dictionary <string, int>()
            {
                { "Circle", circleTopicComment.CircleId },
                { "CircleTopic", circleTopicComment.CircleTopicId }
            };

            var user = await _context.AppUsers.FirstOrDefaultAsync(au => au.Id == appUserId);

            await AddNotificationIfNotExist(new Notification()
            {
                AppUserId         = (int)circleTopicComment.AppUserId,
                NotificationType  = NotificationEnum.NewCircleCommentReplyByMember,
                RecordType        = "CircleTopicComment",
                RecordId          = circleTopicComment.Id,
                RelatingRecordIds = JObject.FromObject(recordIds),
                FromUserName      = user.DisplayName,
                TargetRecordTitle = circleTopicComment.CircleTopic.Title,
                // AdditionalRecordId = circleTopicComment.CircleTopicId,
                // Message = "コミュニティトピック『" + circleTopicComment.CircleTopic.Title +"』であなたのコメントに返信がありました"
            });
        }
        private async Task AddNewCircleCommentReplyByOwnerNotification(int appUserId, CircleTopicCommentReply circleTopicCommentReply)
        {
            if (circleTopicCommentReply == null)
            {
                return;
            }
            var circleTopicComment = await _context.CircleTopicComments.Include(ctc => ctc.CircleTopic).Include(ctc => ctc.AppUser).FirstOrDefaultAsync(ctc => ctc.Id == circleTopicCommentReply.CommentId);

            if (circleTopicComment == null)
            {
                return;
            }
            // var circleTopicCommentReply = record as CircleTopicCommentReply;
            var previousOwnerReply = await _context.CircleTopicCommentReplies.Where(
                ctc => ctc.CommentId == circleTopicCommentReply.CommentId &&
                ctc.AppUserId == appUserId &&
                ctc.Id != circleTopicCommentReply.Id
                )
                                     .OrderByDescending(tr => tr.DateCreated).FirstOrDefaultAsync();

            List <CircleTopicCommentReply> notifyingReplies = null;

            if (previousOwnerReply == null)
            {
                notifyingReplies = await _context.CircleTopicCommentReplies.Where(ctcr =>
                                                                                  ctcr.CommentId == circleTopicCommentReply.CommentId &&
                                                                                  ctcr.AppUserId != appUserId
                                                                                  ).GroupBy(fc => fc.AppUserId)
                                   .Select(g => g.First())
                                   .ToListAsync();
            }
            else
            {
                notifyingReplies = await _context.CircleTopicCommentReplies.Where(ctcr =>
                                                                                  ctcr.CommentId == circleTopicCommentReply.CommentId &&
                                                                                  ctcr.DateCreated > previousOwnerReply.DateCreated &&
                                                                                  ctcr.AppUserId != appUserId
                                                                                  ).GroupBy(fc => fc.AppUserId)
                                   .Select(g => g.First())
                                   .ToListAsync();
            }

            foreach (var reply in notifyingReplies)
            {
                Dictionary <string, int> recordIds = new Dictionary <string, int>()
                {
                    { "Circle", reply.CircleId },
                    { "CircleTopic", circleTopicComment.CircleTopicId }
                };
                if (reply.AppUserId != null)
                {
                    await AddNotificationIfNotExist(new Notification()
                    {
                        AppUserId         = (int)reply.AppUserId,
                        NotificationType  = NotificationEnum.NewCircleCommentReplyByOwner,
                        RecordType        = "CircleTopicComment",
                        RecordId          = (int)reply.CommentId,
                        RelatingRecordIds = JObject.FromObject(recordIds),
                        FromUserName      = circleTopicComment.AppUser.DisplayName,
                        TargetRecordTitle = circleTopicComment.CircleTopic.Title,
                        // Photo = blogFeed.Photo,
                        // Message = "コミュニティトピック『" + circleTopicComment.CircleTopic.Title +"』であなたが返信した" + circleTopicComment.AppUser.DisplayName + "さんのコメントに、新しい返信がありました"
                    });
                }
            }
        }