コード例 #1
0
        public async Task <ChatMessageModel> EditMessage(Guid id, PostChatMessageRequestModel model)
        {
            var message = await DbContext.ChatMessages.FindAsync(id);

            if (message == null)
            {
                throw new EntityNotFoundException("Message with specified id is not found");
            }

            message.Message = model.Text;

            await DbContext.SaveChangesAsync();

            return(await GetMessage(id));
        }
コード例 #2
0
        public async Task <ChatMessageModel> PostMessage(PostChatMessageRequestModel model)
        {
            var currentUser = await _authService.GetAuthorizedUser();

            var channel = await DbContext.ChatChannels.FindAsync(model.ChannelId);

            if (channel == null)
            {
                throw new EntityNotFoundException("Channel with specified id is not found");
            }

            var dbAttachmentsCount = await DbContext.FileUploads
                                     .CountAsync(_ => model.Attachments.Contains(_.Id));

            if (dbAttachmentsCount < model.Attachments.Count)
            {
                throw new EntityNotFoundException("One of the specified attachment id is not found");
            }

            var message = new ChatMessage
            {
                ChannelId   = channel.Id,
                SenderId    = currentUser.Id,
                Message     = model.Text,
                Attachments = model.Attachments.Select(id => new ChatMessageAttachment
                {
                    UploadId = id
                }).ToList(),
            };

            await DbContext.AddAsync(message);

            await DbContext.SaveChangesAsync();

            await _chatProvider.BroadcastMessage(currentUser, message);

            return(await GetMessage(message.Id));
        }
コード例 #3
0
 public async Task <BaseResponse <ChatMessageModel> > Put(Guid id, PostChatMessageRequestModel model) =>
 await PrepareResponse(_chatService.EditMessage, id, model);
コード例 #4
0
 public async Task <BaseResponse <ChatMessageModel> > Post(PostChatMessageRequestModel model) =>
 await PrepareResponse(_chatService.PostMessage, model);