public void SendChatMessage(MessageToReturnDTO message) { string name = Context.User.Identity.Name; object[] args = { message }; Clients.Group(message.RecipientId.ToString()).SendCoreAsync("recieveMessage", args); }
public async Task <IActionResult> CreateMessage(int userId, MessageForCreationDTO messageForCreationDTO) { // Check is user is making request from appropriate userId if (userId != int.Parse(User.FindFirst(ClaimTypes.NameIdentifier).Value)) { return(Unauthorized()); } messageForCreationDTO.SenderId = userId; // Check if recipient user exists User recipient = await _Repo.GetUser(messageForCreationDTO.RecipientId); if (recipient == null) { return(BadRequest("User not found")); } // Map the new message to the Message object and queue into entity Message message = _Mapper.Map <Message>(messageForCreationDTO); message.Recipient = recipient; message.Sender = await _Repo.GetUser(userId); _Repo.Add(message); // Execute save on the database and return a new route on the message if (await _Repo.SaveAll()) { // Map the message back to the DTO MessageToReturnDTO messageToReturn = _Mapper.Map <MessageToReturnDTO>(message); return(CreatedAtRoute("GetMessage", new { id = message.Id }, messageToReturn)); } throw new System.Exception("Message failed to save"); }