Skip to content

Commit 8906c03

Browse files
committed
fixes 424
1 parent ce5e08b commit 8906c03

3 files changed

Lines changed: 31 additions & 13 deletions

File tree

MangoAPI.BusinessLogic/ApiCommands/Communities/LeaveGroupCommandHandler.cs

Lines changed: 19 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
1-
using System.Linq;
1+
using MangoAPI.BusinessLogic.HubConfig;
2+
using System.Linq;
23
using System.Threading;
34
using System.Threading.Tasks;
45
using MangoAPI.BusinessLogic.Responses;
56
using MangoAPI.Domain.Constants;
67
using MangoAPI.Domain.Enums;
78
using MangoAPI.Infrastructure.Database;
89
using MediatR;
10+
using Microsoft.AspNetCore.SignalR;
911
using Microsoft.EntityFrameworkCore;
1012

1113
namespace MangoAPI.BusinessLogic.ApiCommands.Communities;
@@ -15,38 +17,39 @@ public class LeaveGroupCommandHandler
1517
{
1618
private readonly MangoDbContext dbContext;
1719
private readonly ResponseFactory<LeaveGroupResponse> responseFactory;
20+
private readonly IHubContext<ChatHub, IHubClient> hubContext;
1821

1922
public LeaveGroupCommandHandler(
2023
MangoDbContext dbContext,
21-
ResponseFactory<LeaveGroupResponse> responseFactory)
24+
ResponseFactory<LeaveGroupResponse> responseFactory,
25+
IHubContext<ChatHub, IHubClient> hubContext)
2226
{
2327
this.dbContext = dbContext;
2428
this.responseFactory = responseFactory;
29+
this.hubContext = hubContext;
2530
}
2631

2732
public async Task<Result<LeaveGroupResponse>> Handle(
2833
LeaveGroupCommand request,
2934
CancellationToken cancellationToken)
3035
{
31-
var userChat = await dbContext.UserChats
32-
.Include(x => x.Chat)
33-
.Where(chatEntity => chatEntity.UserId == request.UserId)
34-
.Where(chatEntity => chatEntity.ChatId == request.ChatId)
35-
.FirstOrDefaultAsync(cancellationToken);
36+
var chat = await dbContext.Chats
37+
.Include(x => x.ChatUsers)
38+
.FirstOrDefaultAsync(x => x.Id == request.ChatId, cancellationToken);
3639

37-
if (userChat == null)
40+
if (chat == null)
3841
{
3942
const string errorMessage = ResponseMessageCodes.ChatNotFound;
4043
var details = ResponseMessageCodes.ErrorDictionary[errorMessage];
4144

4245
return responseFactory.ConflictResponse(errorMessage, details);
4346
}
4447

45-
var chat = userChat.Chat;
48+
var userBelongsToChat = chat.ChatUsers.Any(x => x.UserId == request.UserId);
4649

47-
if (chat == null)
50+
if (!userBelongsToChat)
4851
{
49-
const string errorMessage = ResponseMessageCodes.ChatNotFound;
52+
const string errorMessage = ResponseMessageCodes.Unauthorized;
5053
var details = ResponseMessageCodes.ErrorDictionary[errorMessage];
5154

5255
return responseFactory.ConflictResponse(errorMessage, details);
@@ -58,16 +61,20 @@ public async Task<Result<LeaveGroupResponse>> Handle(
5861
.Where(messageEntity => messageEntity.ChatId == request.ChatId)
5962
.ToListAsync(cancellationToken);
6063

64+
var partnerId = chat.ChatUsers.First(x => x.UserId != request.UserId).UserId;
65+
6166
dbContext.Messages.RemoveRange(messages);
6267
dbContext.UserChats.RemoveRange(chat.ChatUsers);
6368
dbContext.Chats.Remove(chat);
6469

6570
await dbContext.SaveChangesAsync(cancellationToken);
6671

72+
await hubContext.Clients.Group(partnerId.ToString()).PrivateChatDeletedAsync(request.ChatId);
73+
6774
return responseFactory.SuccessResponse(LeaveGroupResponse.FromSuccess(chat.Id));
6875
}
6976

70-
dbContext.UserChats.Remove(userChat);
77+
dbContext.Chats.Remove(chat);
7178
chat.IncrementMembersCount(-1);
7279

7380
dbContext.Update(chat);

MangoAPI.BusinessLogic/HubConfig/IHubClient.cs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
using System.Threading.Tasks;
22
using MangoAPI.BusinessLogic.Models;
3+
using System;
34

45
namespace MangoAPI.BusinessLogic.HubConfig;
56

@@ -11,9 +12,14 @@ public interface IHubClient
1112
Task BroadcastMessageAsync(Message message);
1213

1314
/// <summary>
14-
/// Updates client list of chats via SignalR.
15+
/// Pushes new created private chat to the receiver's chats list via SignalR.
1516
/// </summary>
1617
Task PrivateChatCreatedAsync(Chat chat);
18+
19+
/// <summary>
20+
/// Removes deleted private chat from the receiver's chats list via SignalR.
21+
/// </summary>
22+
Task PrivateChatDeletedAsync(Guid chatId);
1723

1824
/// <summary>
1925
/// Notifies chat subscribers on the message delete via SignalR.

MangoAPI.Client/src/app/components/chats/chats.component.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -164,6 +164,11 @@ export class ChatsComponent implements OnInit {
164164
});
165165
});
166166

167+
this.connection.on('PrivateChatDeletedAsync', (chatId: string) => {
168+
console.log(`Private chat deleted: ${chatId}`);
169+
this.chats = this.chats.filter((x) => x.chatId !== chatId);
170+
});
171+
167172
this.connection.on('NotifyOnMessageDeleteAsync', (notification: DeleteMessageNotification) => {
168173
this.onMessageDeleteHandler(notification);
169174
});

0 commit comments

Comments
 (0)