Add AutoMapper and message response models

Introduced AutoMapper to the API project and registered it in the DI container. Added mapping profiles for core models to DTOs and response objects. Refactored controllers to use AutoMapper for mapping entities to response DTOs. Implemented new response models for messages, media attachments, reactions, and views. Updated MessagesLoader to load messages with related data. Added a migration to support ChatGroupId in messages. Updated dependencies and fixed minor issues in related files.
This commit is contained in:
Artemy
2025-07-12 16:39:06 +07:00
parent c7de2318fc
commit 96587b491d
22 changed files with 972 additions and 98 deletions
@@ -1,24 +1,52 @@
using Govor.Application.Interfaces;
using Govor.Core.Infrastructure.Extensions;
using Govor.Core.Models.Messages;
using Govor.Core.Repositories.Groups;
using Govor.Core.Repositories.Messages;
using Govor.Core.Repositories.PrivateChats;
using Govor.Data;
using Govor.Data.Repositories.Exceptions;
using Microsoft.EntityFrameworkCore;
namespace Govor.Application.Services.Messages;
public class MessagesLoader : IMessagesLoader
{
private IVerifyFriendship _friendship;
private IGroupsRepository _groupsRepository;
private IMessagesRepository _messagesRepository;
private IPrivateChatsRepository _privateChatsRepository;
private GovorDbContext _dbContext;
public MessagesLoader(
IGroupsRepository groupsRepository,
IPrivateChatsRepository privateChatsRepository,
GovorDbContext dbContext)
{
_groupsRepository = groupsRepository;
_privateChatsRepository = privateChatsRepository;
_dbContext = dbContext;
}
public async Task<List<Message>> LoadLastMessagesInUserChat(Guid userId, Guid currentUser, Guid? startMessageId, int pageSize = 20)
{
await _friendship.VerifyAsync(userId, currentUser);
if(userId == Guid.Empty)
throw new ArgumentException("User id cannot be empty");
if(!_privateChatsRepository.Exist(userId, currentUser))
throw new InvalidOperationException("Private chat not found");
try
{
throw new NotImplementedException();
//return await _groups.GetMessages(userId, startMessageId, pageSize);
var chat = await _privateChatsRepository.GetByMembersAsync(userId, currentUser);
return await _dbContext.Messages
.AsNoTracking()
.Include(m => m.MediaAttachments)
.ThenInclude(m => m.MediaFile)
.AsSplitQuery()
.Where(m => m.RecipientType == RecipientType.User &&
m.RecipientId == chat.Id)
.Take(pageSize)
.ToListOrThrowIfEmpty(new NotFoundException("Messages not found"));
}
catch (NotFoundException ex)
{
@@ -28,12 +56,23 @@ public class MessagesLoader : IMessagesLoader
public async Task<List<Message>> LoadLastMessagesInChatGroup(Guid chatId, Guid currentUser, Guid? startMessageId, int pageSize = 20)
{
if(chatId == Guid.Empty)
throw new ArgumentException("Chat id cannot be empty");
if(!await _groupsRepository.IsUserMemberOfGroupAsync(currentUser, chatId))
throw new UnauthorizedAccessException("You are not in a group.");
try
{
throw new NotImplementedException();
//return await _groups.GetMessages(chatId, startMessageId, pageSize, RecipientType.Group);
return await _dbContext.Messages
.AsNoTracking()
.Include(m => m.MediaAttachments)
.ThenInclude(m => m.MediaFile)
.AsSplitQuery()
.Where(m => m.RecipientType == RecipientType.Group &&
m.RecipientId == chatId)
.Take(pageSize)
.ToListOrThrowIfEmpty(new NotFoundException("Messages not found"));
}
catch (NotFoundException ex)
{