Skip to content
Snippets Groups Projects
Commit 03fb7bd5 authored by esantangelo's avatar esantangelo
Browse files

fix signalR ChatHub

parent 240b5fcd
No related branches found
No related tags found
1 merge request!26Develop
Pipeline #10361 passed
......@@ -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()
......
......@@ -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>();
......
......@@ -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);
}
......
......@@ -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();
}
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment