public async Task <IActionResult> CreateMessage(int userId, MessageForCreationDto messageForCreationDto) { User sender = await _repo.GetUser(userId, false); if (sender.Id != int.Parse(User.FindFirst(ClaimTypes.NameIdentifier).Value)) { return(Unauthorized()); } messageForCreationDto.SenderId = userId; User recipient = await _repo.GetUser(messageForCreationDto.RecipientId, true); if (recipient == null) { return(BadRequest("Could not find user")); } Message message = _mapper.Map <Message>(messageForCreationDto); _repo.Add(message); if (await _repo.SaveAll()) { MessageToReturnDto messageToReturn = _mapper.Map <MessageToReturnDto>(message); return(CreatedAtRoute("GetMessage", new { id = message.Id }, messageToReturn)); } throw new Exception("Creating the message failed on save"); }
public async Task SendMessageToClient(int clientId, MessageToReturnDto message) { await Clients.User(clientId.ToString()).SendAsync("RecieveMessage", message); }