diff --git a/Tsi1.Api/Tsi1.Api/SignalR/ChatHub.cs b/Tsi1.Api/Tsi1.Api/SignalR/ChatHub.cs index 0c54af61a8bd5ac751eec8dd173a9f1140b4f5c8..575b05897d81008f78475837aee031ca50e3ae94 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 f6875a941d07522ec6b2f79f14cf0b1aaa06c443..7fcd3c57c06569ecf9d7ea553173c65be8d2f200 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 cf332b0a11d11b5b77f9058d9d1a95488293b9ea..a79f4b1782a5111636d70efa79a199682db1816a 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 4e4ac8e76aaca0017a0d35dca73cd1e57503da3e..93573625ea418e32ad924cf5124c8e93028e6689 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(); }