Refactor friend request and media services, remove console commands

Refactored the friend request command service and SignalR hub to return and broadcast FriendshipDto objects, improving real-time updates. Enhanced media upload and download logic to support file storage and retrieval, including database integration. Removed all command classes and related infrastructure from the Govor.Console project, streamlining the console client. Updated dependency injection and interfaces to reflect these changes.
This commit is contained in:
Artemy
2025-07-13 19:03:01 +07:00
parent 97b2ea14b2
commit 6063aafd9e
40 changed files with 1029 additions and 1381 deletions
@@ -15,7 +15,7 @@ public class FriendRequestCommandService : IFriendRequestCommandService
_friendshipsRepository = friendshipsRepository;
}
public async Task SendAsync(Guid fromUserId, Guid toUserId)
public async Task<Friendship> SendAsync(Guid fromUserId, Guid toUserId)
{
if (fromUserId == toUserId)
throw new InvalidOperationException("Cannot send a request to self user");
@@ -23,16 +23,20 @@ public class FriendRequestCommandService : IFriendRequestCommandService
if (_friendshipsRepository.Exist(fromUserId, toUserId))
throw new RequestAlreadySentException(fromUserId, toUserId);
await _friendshipsRepository.AddAsync(new Friendship
var friendship = new Friendship
{
Id = Guid.NewGuid(),
RequesterId = fromUserId,
AddresseeId = toUserId,
Status = FriendshipStatus.Pending
});
};
await _friendshipsRepository.AddAsync(friendship);
return friendship;
}
public async Task AcceptAsync(Guid requestId, Guid currentUserId)
public async Task<Friendship> AcceptAsync(Guid requestId, Guid currentUserId)
{
try
{
@@ -46,6 +50,8 @@ public class FriendRequestCommandService : IFriendRequestCommandService
friendship.Status = FriendshipStatus.Accepted;
await _friendshipsRepository.UpdateAsync(friendship);
return friendship;
}
catch (NotFoundByKeyException<Guid> ex)
{
@@ -53,7 +59,7 @@ public class FriendRequestCommandService : IFriendRequestCommandService
}
}
public async Task RejectAsync(Guid requestId, Guid currentUserId)
public async Task<Friendship> RejectAsync(Guid requestId, Guid currentUserId)
{
try
{
@@ -67,6 +73,7 @@ public class FriendRequestCommandService : IFriendRequestCommandService
friendship.Status = FriendshipStatus.Rejected;
await _friendshipsRepository.UpdateAsync(friendship);
return friendship;
}
catch (NotFoundByKeyException<Guid> ex)
{
@@ -1,20 +1,53 @@
using Govor.Application.Interfaces;
using Govor.Application.Interfaces.Medias;
using Govor.Core.Models;
using Govor.Data;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Logging;
namespace Govor.Application.Services.Messages;
public class MediaService : IMediaService
{
private ILogger<MediaService> _logger;
private IStorageService _storageService;
private GovorDbContext _dbContext;
public MediaService(IStorageService storageService)
public MediaService(IStorageService storageService, GovorDbContext dbContext, ILogger<MediaService> logger)
{
_storageService = storageService;
_dbContext = dbContext;
_logger = logger;
}
public Task<MediaUploadResult> UploadMediaAsync(Media file)
public async Task<MediaUploadResult> UploadMediaAsync(Media file)
{
throw new NotImplementedException();
try
{
var url = await _storageService.SaveAsync(file.Data, file.FileName);
var mediaId = Guid.NewGuid();
_dbContext.MediaFiles.Add(new MediaFile()
{
Id = mediaId,
UploaderId = file.UploaderId,
DateCreated = file.UploadedOn,
MediaType = file.Type,
MineType = file.MineType,
Url = url
});
await _dbContext.SaveChangesAsync();
_logger.LogInformation($"Media uploaded: {url} with id: {mediaId} by {file.UploaderId}");
return new MediaUploadResult(mediaId, url);
}
catch (ArgumentException ex)
{
throw new InvalidOperationException($"An error occured while uploading the media file: {ex.Message}");
}
}
public Task DeleteMediaAsync(Guid fileId)
@@ -22,8 +55,46 @@ public class MediaService : IMediaService
throw new NotImplementedException();
}
public Task<MediaUploadResult> GetMediaAsync(string url)
public Task<Media> GetMediaByUrlAsync(string url)
{
throw new NotImplementedException();
}
public async Task<Media> GetMediaByIdAsync(Guid mediaId)
{
try
{
var mediaFile = await _dbContext.MediaFiles
.AsNoTracking()
.FirstOrDefaultAsync(x => x.Id == mediaId)
?? throw new KeyNotFoundException("No media found");
// Загрузить бинарные данные из хранилища
Stream dataStream = await _storageService.LoadAsync(mediaFile.Url);
// Считать поток в byte[]
using var memoryStream = new MemoryStream();
await dataStream.CopyToAsync(memoryStream);
var contentBytes = memoryStream.ToArray();
_logger.LogInformation($"Media found: {mediaFile.MediaType} with id: {mediaFile.Id} and url: {mediaFile.Url}");
// Вернуть объект Media
return new Media(
mediaFile.UploaderId,
mediaFile.DateCreated,
contentBytes,
string.Empty,
mediaFile.MediaType,
mediaFile.MineType,
string.Empty
);
}
catch (FileNotFoundException ex)
{
_logger.LogWarning(ex, "Media file not found on storage.");
throw;
}
}
}