Refactor media attachments to use MediaFile entity

This commit introduces the MediaFile entity and updates the media attachments model, repositories, and related logic to reference MediaFile instead of storing file metadata directly in MediaAttachments. Controller, service, and SignalR request/response contracts are updated to support the new structure. Database configurations and validators are also adjusted accordingly. This refactor improves media management and normalization in the data model.
This commit is contained in:
Artemy
2025-07-06 21:09:12 +07:00
parent 42348c863f
commit 66819a015a
23 changed files with 166 additions and 93 deletions
@@ -77,9 +77,7 @@ public class MediaAttachmentsRepository : IMediaAttachmentsRepository
.ExecuteUpdateAsync(u => u
.SetProperty(m => m.MessageId, attachments.MessageId)
.SetProperty(m => m.Message, attachments.Message)
.SetProperty(m => m.FilePath, attachments.FilePath)
.SetProperty(m => m.MimeType, attachments.MimeType)
.SetProperty(m => m.EncryptedKey, attachments.EncryptedKey)
.SetProperty(m => m.MediaFileId, attachments.MediaFileId)
);
if (rowsAffected == 0)
@@ -121,11 +119,8 @@ public class MediaAttachmentsRepository : IMediaAttachmentsRepository
return _context.MediaAttachments.Any(
e => e.Id == attachments.Id &&
e.EncryptedKey == attachments.EncryptedKey &&
e.MimeType == attachments.MimeType &&
e.FilePath == attachments.FilePath &&
e.MessageId == attachments.MessageId &&
e.Type == attachments.Type
e.MediaFileId == attachments.MediaFileId
);
}
}
+11 -7
View File
@@ -21,7 +21,8 @@ public class MessagesRepository : IMessagesRepository
{
return await _context.Messages
.AsNoTracking()
.Include(m => m.ReplyToMessage)
.Include(m => m.MediaAttachments)
.ThenInclude(m => m.MediaFile)
.AsSplitQuery()
.ToListOrThrowIfEmpty(new NotFoundException("No messages found in the database"));
}
@@ -30,7 +31,6 @@ public class MessagesRepository : IMessagesRepository
{
return await _context.Messages
.AsNoTracking()
.Include(m => m.ReplyToMessage)
.AsSplitQuery()
.FirstOrDefaultAsync(m => m.Id == messageId)
?? throw new NotFoundByKeyException<Guid>(messageId, "Message with given id does not exist");
@@ -40,7 +40,8 @@ public class MessagesRepository : IMessagesRepository
{
return await _context.Messages
.AsNoTracking()
.Include(m => m.ReplyToMessage)
.Include(m => m.MediaAttachments)
.ThenInclude(m => m.MediaFile)
.AsSplitQuery()
.Where(m => m.SenderId == senderId)
.ToListOrThrowIfEmpty(new NotFoundByKeyException<Guid>(senderId, "Messages with given sender id do not exist"));
@@ -50,7 +51,8 @@ public class MessagesRepository : IMessagesRepository
{
return await _context.Messages
.AsNoTracking()
.Include(m => m.ReplyToMessage)
.Include(m => m.MediaAttachments)
.ThenInclude(m => m.MediaFile)
.AsSplitQuery()
.Where(m => m.RecipientId == receiverId)
.ToListOrThrowIfEmpty(new NotFoundByKeyException<Guid>(receiverId, "Messages with given recipient id do not exist"));
@@ -60,7 +62,8 @@ public class MessagesRepository : IMessagesRepository
{
return await _context.Messages
.AsNoTracking()
.Include(m => m.ReplyToMessage)
.Include(m => m.MediaAttachments)
.ThenInclude(m => m.MediaFile)
.AsSplitQuery()
.Where(m => m.SenderId == senderId
&& m.RecipientId == receiverId
@@ -72,7 +75,8 @@ public class MessagesRepository : IMessagesRepository
{
return await _context.Messages
.AsNoTracking()
.Include(m => m.ReplyToMessage)
.Include(m => m.MediaAttachments)
.ThenInclude(m => m.MediaFile)
.AsSplitQuery()
.Where(m => m.SentAt == date)
.ToListOrThrowIfEmpty(new NotFoundByKeyException<DateTime>(date, "Messages sent at date do not exist"));
@@ -161,7 +165,7 @@ public class MessagesRepository : IMessagesRepository
m.EditedAt == message.EditedAt &&
m.IsEdited == message.IsEdited &&
m.SentAt == message.SentAt &&
m.ReplyToMessageId == message.ReplyToMessageId
m.ReplyToMessageId == message.ReplyToMessageId
);
}