示例#1
0
        public async Task <ActionResult <Message> > SendGroupMessage(string roomId, [FromBody] MessageSentForm sentMessage)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(new { message = "Message form is not valid" }));
            }

            var room = await roomRepository.GetRoom(roomId);

            if (room == null)
            {
                return(NotFound(new { message = "Couldn't find the room in the database" }));
            }

            await hubContext.Clients.Group(room.RoomName).SendAsync(KeyConstants.receiveNewRoomMessage, sentMessage, roomId);

            var updatedRoom = await roomRepository.AddMessageToRoom(roomId, sentMessage);

            var newMessage = updatedRoom.RoomMessages.Find(m => m.Content == sentMessage.Content && m.SenderName == sentMessage.SenderName);

            if (newMessage == null)
            {
                return(NotFound(new { message = "Couldn't find the new room message in the database" }));
            }

            return(CreatedAtRoute("GetRoomMessage", new { roomId = updatedRoom.Id.ToString(), roomMessageId = newMessage.Id.ToString() }, newMessage));
        }