public async Task <IEnumerable <ChatMessageModel> > GetMessagesByChannelAsync(string userId, int channelId) { var channel = await(from c in _chatChannelRepository.GetAll() join member in _chatChannelMemberRepository.GetAll() on c.ChannelId equals member.ChannelId where member.UserId == userId && c.ChannelId == channelId select c).FirstOrDefaultAsync(); if (channel == null) { throw new NotFoundException("Channel not found or user doesn't have access."); } var messages = await _chatMessageRepository.GetAll() .Where(_ => _.ChannelId == channelId) .ToListAsync(); return(_mapper.Map <IEnumerable <ChatMessageModel> >(messages)); }