Example #1
0
        public CreateChatRoomRespond CreateChatRoom(CreateChatRoomRequest r)
        {
            _chatRoomRepository.Insert(new ChatRoom {
                NumOfMember  = 2,
                ChatRoomName = r.User1Id + "and" + r.User2Id,
                CreatedAt    = DateTime.Now
            });
            ChatRoom i = _chatRoomRepository.GetChatRoomByName(r.User1Id + "and" + r.User2Id);

            _userChatRoomRepository.Insert(new UserChatRoom {
                ChatRoomId = i.Id,
                NickName   = r.User1NickName,
                User       = r.User1Id
            });
            _userChatRoomRepository.Insert(new UserChatRoom {
                ChatRoomId = i.Id,
                NickName   = r.User2NickName,
                User       = r.User2Id
            });
            i.ChatRoomName = r.User1NickName + " and " + r.User2NickName;
            _chatRoomRepository.Update(i);
            return(new CreateChatRoomRespond {
                RoomId = i.Id,
                RoomName = i.ChatRoomName,
            });
        }
Example #2
0
        public void EditChatRoom(int chatRoomId, string newTitle)
        {
            var chatRoom = _chatRoomRepository.FindById(chatRoomId);

            if (chatRoom == null)
            {
                throw new ObjectNotFoundException($"ChatRoom with id={chatRoomId} not found");
            }

            chatRoom.Title = newTitle;
            _chatRoomRepository.Update(chatRoom);
        }
Example #3
0
        public async Task <BasicOutput <string> > SendMessage(string chatRoomId, string authorId, string authorName, string message)
        {
            var result   = new BasicOutput <string>();
            var chatRoom = await _chatRoomRepository.GetChatRoomById(Guid.Parse(chatRoomId), true);

            if (chatRoom == null)
            {
                result.AddNotification(new Notification("Specified chat room could not be found", ENotificationType.Error, "chatRoom"));
            }

            if (result.Invalid)
            {
                return(result);
            }

            var author      = new ChatMessageAuthor(authorId, authorName);
            var chatMessage = new ChatMessage(message, author);

            if (chatMessage.Invalid)
            {
                result.AddNotifications(chatMessage.ValidationResult);
                return(result);
            }

            if (chatMessage.IsMessage)
            {
                chatRoom.AddChatMessage(chatMessage);
                _chatRoomRepository.Update(chatRoom);
                await _unitOfWork.CommitAsync();
            }

            if (chatMessage.IsCommand)
            {
                await _webSocketService.SendCommand(chatRoom.Id.ToString(), chatMessage.Content);
            }

            await _webSocketService
            .SendMessage(
                chatRoom.Id.ToString(),
                chatMessage);

            result.SetOutput("The message has been sent successfully");
            return(result);
        }