From 03fb7bd5d9738f5d05d9a9271802efe4bb63c973 Mon Sep 17 00:00:00 2001 From: esantangelo <enzo020895@gmail.com> Date: Sun, 15 Nov 2020 20:47:09 -0300 Subject: [PATCH] fix signalR ChatHub --- Tsi1.Api/Tsi1.Api/SignalR/ChatHub.cs | 33 ++++--------------- Tsi1.Api/Tsi1.Api/Startup.cs | 1 + .../Interfaces/IChatService.cs | 2 +- .../Services/ChatService.cs | 32 ++++++++++++++++-- 4 files changed, 37 insertions(+), 31 deletions(-) diff --git a/Tsi1.Api/Tsi1.Api/SignalR/ChatHub.cs b/Tsi1.Api/Tsi1.Api/SignalR/ChatHub.cs index 0c54af6..575b058 100644 --- a/Tsi1.Api/Tsi1.Api/SignalR/ChatHub.cs +++ b/Tsi1.Api/Tsi1.Api/SignalR/ChatHub.cs @@ -66,6 +66,8 @@ namespace Tsi1.Api.SignalR var userId = int.Parse(Context.User.Claims.FirstOrDefault(x => x.Type == "Id").Value); var tenantId = int.Parse(Context.User.Claims.FirstOrDefault(x => x.Type == "TenantId").Value); + newMessage.TenantId = tenantId; + if (userId == newMessage.ReceiverId) throw new HubException("You cannot send messages to yourself"); @@ -93,41 +95,18 @@ namespace Tsi1.Api.SignalR await Clients.Group(groupName).SendAsync("NewMessage", messageDto); } - private async Task<Group> AddToGroup(string groupName) + private async Task<bool> AddToGroup(string groupName) { - var group = await _chatService.GetGroupByName(groupName); - - if (group == null) - { - throw new HubException($"No existe el grupo {groupName}"); - } - var userId = int.Parse(Context.User.Claims.FirstOrDefault(x => x.Type == "Id").Value); - var connection = new Connection - { - ConnectionId = Context.ConnectionId, - UserId = userId, - }; - - if (group == null) - { - group = new Group - { - Name = groupName, - }; - } - - group.Connections.Add(connection); - - var result = await _chatService.CreateGroup(group); + var result = await _chatService.CreateGroup(groupName, Context.ConnectionId, userId); if (result.HasError) { - throw new HubException($"Error al crear el grupo {group}"); + throw new HubException($"Error al crear el grupo {groupName}"); } - return group; + return true; } private async Task<bool> RemoveFromGroup() diff --git a/Tsi1.Api/Tsi1.Api/Startup.cs b/Tsi1.Api/Tsi1.Api/Startup.cs index f6875a9..7fcd3c5 100644 --- a/Tsi1.Api/Tsi1.Api/Startup.cs +++ b/Tsi1.Api/Tsi1.Api/Startup.cs @@ -92,6 +92,7 @@ namespace Tsi1.Api services.AddScoped<ISectionItemTypeService, SectionItemTypeService>(); services.AddScoped<IDataLoad, DataLoad>(); services.AddScoped<ISurveyService, SurveyService>(); + services.AddScoped<IChatService, ChatService>(); services.AddSingleton<PresenceTracker>(); diff --git a/Tsi1.Api/Tsi1.BusinessLayer/Interfaces/IChatService.cs b/Tsi1.Api/Tsi1.BusinessLayer/Interfaces/IChatService.cs index cf332b0..a79f4b1 100644 --- a/Tsi1.Api/Tsi1.BusinessLayer/Interfaces/IChatService.cs +++ b/Tsi1.Api/Tsi1.BusinessLayer/Interfaces/IChatService.cs @@ -11,7 +11,7 @@ namespace Tsi1.BusinessLayer.Interfaces { Task<Group> GetGroupByName(string groupName); - Task<ServiceResult<Group>> CreateGroup(Group group); + Task<ServiceResult<bool>> CreateGroup(string groupName, string connectionId, int userId); Task<bool> RemoveConnectionFromGroup(string connectionId); } diff --git a/Tsi1.Api/Tsi1.BusinessLayer/Services/ChatService.cs b/Tsi1.Api/Tsi1.BusinessLayer/Services/ChatService.cs index 4e4ac8e..9357362 100644 --- a/Tsi1.Api/Tsi1.BusinessLayer/Services/ChatService.cs +++ b/Tsi1.Api/Tsi1.BusinessLayer/Services/ChatService.cs @@ -18,11 +18,35 @@ namespace Tsi1.BusinessLayer.Services { _tsi1Context = tsi1Context; } - public async Task<ServiceResult<Group>> CreateGroup(Group group) + + public async Task<ServiceResult<bool>> CreateGroup(string groupName, string connectionId, int userId) { - var result = new ServiceResult<Group>(); + var result = new ServiceResult<bool>(); + var existingGroup = true; + var group = await this.GetGroupByName(groupName); + + if (group == null) + { + existingGroup = false; + group = new Group + { + Name = groupName, + }; + } - _tsi1Context.Groups.Add(group); + var connection = new Connection + { + ConnectionId = connectionId, + UserId = userId, + }; + + group.Connections.Add(connection); + + if (!existingGroup) + { + _tsi1Context.Groups.Add(group); + } + try { await _tsi1Context.SaveChangesAsync(); @@ -51,6 +75,8 @@ namespace Tsi1.BusinessLayer.Services if (connection != null) { connection.Group = null; + _tsi1Context.Connections.Remove(connection); + await _tsi1Context.SaveChangesAsync(); } -- GitLab