public async Task <ActionResult <IEnumerable <ChatViewModel> > > GetChatMessages([FromRoute] long accountId, long?afterId = null, long?beforeId = null, int count = 30, CancellationToken cancellationToken = default) { var messages = _context.Chats.AsNoTracking().Where(q => (q.SenderAccountId == AccountId || q.SenderAccountId == accountId) && (q.ReceiverAccountId == AccountId || q.ReceiverAccountId == accountId)); if (afterId != null) { messages = messages.Where(q => q.Id > afterId.Value).OrderBy(q => q.Id); } if (beforeId != null) { messages = messages.Where(q => q.Id < beforeId.Value).OrderByDescending(q => q.Id); } messages = messages.Take(count); var result = await messages.ToListAsync(cancellationToken); return(OkData(ChatViewModel.MapAll(result))); }