1- using System . Linq ;
1+ using MangoAPI . BusinessLogic . HubConfig ;
2+ using System . Linq ;
23using System . Threading ;
34using System . Threading . Tasks ;
45using MangoAPI . BusinessLogic . Responses ;
56using MangoAPI . Domain . Constants ;
67using MangoAPI . Domain . Enums ;
78using MangoAPI . Infrastructure . Database ;
89using MediatR ;
10+ using Microsoft . AspNetCore . SignalR ;
911using Microsoft . EntityFrameworkCore ;
1012
1113namespace 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 ) ;
0 commit comments