10.4: Getting Message Threads/Conversations
Implementing GetMessageThread() Service Method
public async Task<IEnumerable<MessageToReturn>> GetMessageThread(int userId, int recipientId)
{
var messages = await _context.Messages
.Include(u => u.Sender).ThenInclude(p => p.Photos)
.Include(u => u.Recipient).ThenInclude(p => p.Photos)
.Where(m => (m.RecipientId == userId &&
m.SenderId == recipientId &&
m.RecipientDeleted == false)
||
(m.RecipientId == recipientId &&
m.SenderId == userId &&
m.SenderDeleted == false))
.Select(u => new MessageToReturn
{
Id = u.Id,
SenderId = u.SenderId,
SenderKnownAs = u.Sender.KnownAs,
SenderPhotoUrl = u.Sender.Photos.FirstOrDefault(p => p.IsMain).Url,
RecipientId = u.RecipientId,
RecipientKnownAs = u.Recipient.KnownAs,
RecipientPhotoUrl = u.Recipient.Photos.FirstOrDefault(p => p.IsMain).Url,
Content = u.Content,
IsRead = u.IsRead,
DateRead = u.DateRead,
DateSent = u.DateSent
})
.OrderByDescending(m => m.DateSent)
.ToListAsync();
return messages;
}Add GetMessageThread() Controller Method
Testing in Postman

Previous10.3: Add Inbox and Outbox Service MethodsNext10.5: Building the Message Component in Angular
Last updated