Skip to content

Commit

Permalink
Merge pull request #463 from gordon-cs/360DM
Browse files Browse the repository at this point in the history
Add endpoint to store a users connection id
  • Loading branch information
jahnueldorelus authored Feb 20, 2021
2 parents 0186d36 + 8914a8e commit 5687cf5
Show file tree
Hide file tree
Showing 7 changed files with 225 additions and 34 deletions.
88 changes: 72 additions & 16 deletions Gordon360/ApiControllers/DirectMessageController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -39,21 +39,7 @@ public DirectMessageController(IDirectMessageService DirectMessageService)
{
_DirectMessageService = DirectMessageService;
}

/// <summary>
/// returns hello world example
/// </summary>
/// <returns>string and date</returns>
[HttpGet]
[Route("")]
public IHttpActionResult Get()
{
DateTime currentTime = DateTime.Now;
var result = "hello world I'm coming from the back end at: " + currentTime;

return Ok(result);
}


/// <summary>
/// returns messages from a specified group
/// </summary>
Expand Down Expand Up @@ -199,8 +185,78 @@ public IHttpActionResult StoreUserRooms([FromBody] String roomId)

}



/// <summary>
/// stores connection associated with a user id
/// </summary>
/// <returns>true if successful</returns>
[HttpPost]
[Route("userConnectionIds")]
public IHttpActionResult StoreUserConnectionIds([FromBody] String connectionId)
{
var authenticatedUser = this.ActionContext.RequestContext.Principal as ClaimsPrincipal;
var username = authenticatedUser.Claims.FirstOrDefault(x => x.Type == "user_name").Value;

var userId = _accountService.GetAccountByUsername(username).GordonID;

var result = _DirectMessageService.StoreUserConnectionIds(userId, connectionId);

}
if (result == false)
{
return NotFound();
}


return Ok(result);

}

/// <summary>
/// Gets connection ids associated with a user id
/// </summary>
/// <returns>true if successful</returns>
[HttpPut]
[Route("userConnectionIds")]
public IHttpActionResult GetConnectionIds([FromBody] String userId)
{


var result = _DirectMessageService.GetUserConnectionIds(userId);

if (result == null)
{
return NotFound();
}


return Ok(result);

}

/// <summary>
/// Deletes connection ids associated with a user id
/// </summary>
/// <returns>true if successful</returns>
[HttpPut]
[Route("deleteUserConnectionIds")]
public IHttpActionResult DeleteConnectionIds([FromBody] String connectionId)
{


var result = _DirectMessageService.DeleteUserConnectionIds(connectionId);

if (result == null)
{
return NotFound();
}


return Ok(result);

}


}

}
24 changes: 18 additions & 6 deletions Gordon360/Documentation/Gordon360.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Gordon360/Gordon360.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -583,6 +583,7 @@
</Compile>
<Compile Include="Models\ViewModels\CliftonStrengthsViewModel.cs" />
<Compile Include="Models\ViewModels\ClockInViewModel.cs" />
<Compile Include="Models\ViewModels\ConnectionIdViewModel.cs" />
<Compile Include="Models\ViewModels\CreateGroupViewModel.cs" />
<Compile Include="Models\ViewModels\GroupViewModel.cs" />
<Compile Include="Models\ViewModels\HourTypesViewModel.cs" />
Expand Down
47 changes: 37 additions & 10 deletions Gordon360/Hubs/SignalRHub.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,31 +7,58 @@
using Microsoft.AspNet.SignalR;
using Microsoft.AspNet.SignalR.Hubs;
using Gordon360;
using Gordon360.Services;
using Gordon360.ApiControllers;
using System.Web.Http;
using Gordon360.Models.ViewModels;
using Gordon360.Repositories;
using System.Security.Claims;

namespace Gordon360.Hubs
{
[RoutePrefix("api/signalr")]
[HubName("ChatHub")]
public class ChatHub : Hub
{

public async Task refreshMessages(string user)
{
await Clients.All.SendAsync("ReceivedMessagerefresh", user);
//public DirectMessageService _DirectMessageService = new DirectMessageService();

public async Task refreshMessages(List<string> connectionIds, IEnumerable<MessageViewModel> message)
{
foreach(string connections in connectionIds)
{
await Groups.Add(connections, "list");
}
await Clients.Group("list").SendAsync("Received message refresh, Message: ", message);
}

public async Task test()

public string savedUserId;
public string savedConnectionId;

public void saveConnection(string id)
{
await Clients.All.SendAsync("It's working boss");
DirectMessageService _DirectMessageService = new DirectMessageService();
savedUserId = id;
savedConnectionId = Context.ConnectionId;
_DirectMessageService.StoreUserConnectionIds(id, Context.ConnectionId);
Clients.All.BroadcastMessage("It's working boss");

}

public string test2()

public override Task OnDisconnected(bool stopCalled)
{
DirectMessageService _DirectMessageService = new DirectMessageService();

_DirectMessageService.DeleteUserConnectionIds(Context.ConnectionId);
return base.OnDisconnected(stopCalled);
}

public override Task OnReconnected()
{
return "It worked";
DirectMessageService _DirectMessageService = new DirectMessageService();

_DirectMessageService.DeleteUserConnectionIds(savedConnectionId);
_DirectMessageService.StoreUserConnectionIds(savedUserId, Context.ConnectionId);
return base.OnReconnected();
}
}

Expand Down
14 changes: 14 additions & 0 deletions Gordon360/Models/ViewModels/ConnectionIdViewModel.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace Gordon360.Models.ViewModels
{
public class ConnectionIdViewModel
{
//user websocket connection Id
public string connection_id { get; set; }

}
}
82 changes: 80 additions & 2 deletions Gordon360/Services/DirectMessageService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -218,18 +218,19 @@ public bool SendMessage(SendTextViewModel textInfo, String user_id)
idParam, roomIdParam, textParam, createdAtParam, userIdParam, imageParam, videoParam, audioParam, systemParam, sentParam, receivedParam, pendingParam); //run stored procedure

bool returnAnswer = true;

if (result == null)
{
returnAnswer = false;
throw new ResourceNotFoundException() { ExceptionMessage = "The data was not found." };
}



return returnAnswer;

}



public bool StoreUserRooms(String userId, String roomId)
{
var _unitOfWork = new UnitOfWork();
Expand All @@ -255,6 +256,83 @@ public bool StoreUserRooms(String userId, String roomId)



return returnAnswer;

}


public bool StoreUserConnectionIds(String userId, String connectionId)
{
var _unitOfWork = new UnitOfWork();
var query = _unitOfWork.AccountRepository.FirstOrDefault(x => x.gordon_id == userId);
if (query == null)
{
throw new ResourceNotFoundException() { ExceptionMessage = "The account was not found." };
}

var userIdParam = new SqlParameter("@user_id", userId);
var connectionIdParam = new SqlParameter("@connection_id", connectionId);


var result = RawSqlQuery<MessageViewModel>.query("INSERT_USER_CONNECTION_ID @user_id, @connection_id", userIdParam, connectionIdParam); //run stored procedure

bool returnAnswer = true;

if (result == null)
{
returnAnswer = false;
throw new ResourceNotFoundException() { ExceptionMessage = "The data was not found." };
}



return returnAnswer;

}

public IEnumerable<ConnectionIdViewModel> GetUserConnectionIds(String userId)
{
var _unitOfWork = new UnitOfWork();
var query = _unitOfWork.AccountRepository.FirstOrDefault(x => x.gordon_id == userId);
if (query == null)
{
throw new ResourceNotFoundException() { ExceptionMessage = "The account was not found." };
}

var userIdParam = new SqlParameter("@user_id", userId);

var result = RawSqlQuery<ConnectionIdViewModel>.query("GET_ALL_CONNECTION_IDS_BY_ID @user_id", userIdParam); //run stored procedure

var model = result.Select(x =>
{
ConnectionIdViewModel y = new ConnectionIdViewModel();
y.connection_id = x.connection_id;


return y;
});


return model;

}

public bool DeleteUserConnectionIds(String connectionId)
{
var connectionIdParam = new SqlParameter("@connection_id", connectionId);

var result = RawSqlQuery<MessageViewModel>.query("DELETE_USER_CONNECTION_ID @connection_id", connectionIdParam); //run stored procedure

bool returnAnswer = true;

if (result == null)
{
returnAnswer = false;
throw new ResourceNotFoundException() { ExceptionMessage = "The data was not found." };
}



return returnAnswer;

}
Expand Down
3 changes: 3 additions & 0 deletions Gordon360/Services/ServiceInterfaces.cs
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,9 @@ public interface IDirectMessageService
bool CreateGroup(String id, String name, bool group, DateTime lastUpdated,string image);
bool SendMessage(SendTextViewModel textInfo, string user_id);
bool StoreUserRooms(String userId, String roomId);
bool StoreUserConnectionIds(String userId, String connectionId);
bool DeleteUserConnectionIds(String connectionId);
IEnumerable<ConnectionIdViewModel> GetUserConnectionIds(String userId);
IEnumerable<MessageViewModel> GetMessages(string roomId);
IEnumerable<GroupViewModel> GetRooms(string userId);
List<Object> GetRoomById(string userId);
Expand Down

0 comments on commit 5687cf5

Please sign in to comment.