-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathSendMessageCommandHandler.cs
More file actions
136 lines (112 loc) · 4.81 KB
/
SendMessageCommandHandler.cs
File metadata and controls
136 lines (112 loc) · 4.81 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
using FluentValidation;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using MangoAPI.Application.Interfaces;
using MangoAPI.Application.Services;
using MangoAPI.BusinessLogic.HubConfig;
using MangoAPI.BusinessLogic.Notifications;
using MangoAPI.BusinessLogic.Responses;
using MangoAPI.Domain.Constants;
using MangoAPI.Domain.Entities;
using MangoAPI.Infrastructure.Database;
using MediatR;
using Microsoft.AspNetCore.SignalR;
using Microsoft.EntityFrameworkCore;
namespace MangoAPI.BusinessLogic.ApiCommands.Messages;
public class SendMessageCommandHandler
: IRequestHandler<SendMessageCommand, Result<SendMessageResponse>>
{
private readonly MangoDbContext dbContext;
private readonly ResponseFactory<SendMessageResponse> responseFactory;
private readonly IBlobServiceSettings blobServiceSettings;
private readonly IBlobService blobService;
private readonly IHubContext<ChatHub, IHubClient> hubContext;
public SendMessageCommandHandler(
MangoDbContext dbContext,
ResponseFactory<SendMessageResponse> responseFactory,
IBlobServiceSettings blobServiceSettings,
IBlobService blobService, IHubContext<ChatHub, IHubClient> hubContext)
{
this.dbContext = dbContext;
this.responseFactory = responseFactory;
this.blobServiceSettings = blobServiceSettings;
this.blobService = blobService;
this.hubContext = hubContext;
}
public async Task<Result<SendMessageResponse>> Handle(
SendMessageCommand request,
CancellationToken cancellationToken)
{
var user = await dbContext.Users.AsNoTracking()
.Select(x => new
{
x.DisplayName, x.DisplayNameColour, Image = x.ImageFileName, x.Id,
}).FirstOrDefaultAsync(
x => x.Id == request.UserId, cancellationToken);
if (user == null)
{
const string errorMessage = ResponseMessageCodes.UserNotFound;
var errorDescription = ResponseMessageCodes.ErrorDictionary[ResponseMessageCodes.UserNotFound];
return responseFactory.ConflictResponse(errorMessage, errorDescription);
}
var userChat = await dbContext.UserChats
.Select(x => new { x.ChatId, x.RoleId, x.Chat, }).FirstOrDefaultAsync(
x => x.ChatId == request.ChatId,
cancellationToken);
if (userChat == null)
{
const string errorMessage = ResponseMessageCodes.ChatNotFound;
var errorDescription = ResponseMessageCodes.ErrorDictionary[errorMessage];
return responseFactory.ConflictResponse(errorMessage, errorDescription);
}
var attachmentUniqueFileName = await UploadAttachmentIfExistsAsync(request);
var messageEntity = MessageEntity.Create(
request.UserId,
request.ChatId,
request.Text,
request.InReplyToUser,
request.InReplyToText,
attachmentUniqueFileName);
userChat.Chat.UpdateLastMessage(
lastMessageAuthor: user.DisplayName,
lastMessageText: messageEntity.Text,
messageEntity.CreatedAt,
messageEntity.Id);
dbContext.Chats.Update(userChat.Chat);
dbContext.Messages.Add(messageEntity);
await dbContext.SaveChangesAsync(cancellationToken);
var authorImageUrl = $"{blobServiceSettings.MangoBlobAccess}/{user.Image}";
var attachmentUrl = attachmentUniqueFileName == null
? null
: $"{blobServiceSettings.MangoBlobAccess}/{attachmentUniqueFileName}";
var messageNotification = new SendMessageNotification(
messageEntity.Id,
messageEntity.UserId,
messageEntity.ChatId,
user.DisplayName,
user.DisplayNameColour,
messageEntity.Text,
messageEntity.CreatedAt,
messageEntity.UpdatedAt,
messageEntity.InReplyToUser,
messageEntity.InReplyToText,
authorImageUrl,
attachmentUrl);
await hubContext.Clients.Group(request.ChatId.ToString()).MessageSentAsync(messageNotification);
return responseFactory.SuccessResponse(SendMessageResponse.FromSuccess(messageEntity.Id, attachmentUrl,
messageEntity.CreatedAt));
}
private async Task<string> UploadAttachmentIfExistsAsync(SendMessageCommand request)
{
if (request.Attachment == null)
{
return null;
}
await new CommonFileValidator().ValidateAndThrowAsync(request.Attachment);
var file = request.Attachment;
var uniqueFileName = FileNameHelper.CreateUniqueFileName(file.FileName);
await blobService.UploadFileBlobAsync(file.OpenReadStream(), file.ContentType, uniqueFileName);
return uniqueFileName;
}
}