Пример #1
0
        public void DeleteMemberFromChatRoom(int chatRoomId, string userId)
        {
            var chatRoom = _chatRoomRepository.FindById(chatRoomId);

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

            var user = _userManager.FindById(userId);

            if (user == null)
            {
                throw new ObjectNotFoundException($"User with id={userId} not found");
            }

            if (chatRoom.Users.Count > 1)
            {
                chatRoom.Users.Remove(user);
            }
            else
            {
                _chatRoomRepository.Delete(chatRoom);
            }
        }
Пример #2
0
        public void CreateMessage(string senderId, int chatRoomId, string text)
        {
            var chatRoom = _chatRoomRepository.FindById(chatRoomId);

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

            var sender = _userManager.FindById(senderId);

            if (sender == null)
            {
                throw new ObjectNotFoundException($"User with id={senderId} not found");
            }

            var message = new Message()
            {
                Text      = text,
                DateAdded = DateTime.UtcNow,
                Sender    = sender,
                ChatRoom  = chatRoom
            };

            _messageRepository.Create(message);
        }