mirror of
https://github.com/Govor-team/Govor.git
synced 2026-07-21 11:44:56 +00:00
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:
@@ -29,7 +29,7 @@ public class FriendshipsController : Controller
|
||||
{
|
||||
_logger.LogInformation("Get all friendships by administrator");
|
||||
var result = await _friendshipsRepository.GetAllAsync();
|
||||
return Ok(BuildFriendshipDtos(result));
|
||||
return Ok(result);
|
||||
}
|
||||
catch (NotFoundException ex)
|
||||
{
|
||||
@@ -53,7 +53,7 @@ public class FriendshipsController : Controller
|
||||
{
|
||||
_logger.LogInformation($"Get user's {userId} all friendships by administrator");
|
||||
var result = await _friendshipsRepository.FindByUserIdAsync(userId);
|
||||
return Ok(BuildFriendshipDtos(result));
|
||||
return Ok(result);
|
||||
}
|
||||
catch (NotFoundByKeyException<Guid> ex)
|
||||
{
|
||||
@@ -89,21 +89,4 @@ public class FriendshipsController : Controller
|
||||
return StatusCode(500, new { error = "Internal server error." });
|
||||
}
|
||||
}
|
||||
|
||||
private List<UserDto> BuildUserDtos(IEnumerable<User> users) => users.Select(user => new UserDto
|
||||
{
|
||||
Id = user.Id,
|
||||
Username = user.Username,
|
||||
Description = user.Description,
|
||||
WasOnline = user.WasOnline,
|
||||
IconId = user.IconId
|
||||
}).ToList();
|
||||
|
||||
private List<FriendshipDto> BuildFriendshipDtos(IEnumerable<Friendship> friendships) => friendships.Select(f => new FriendshipDto
|
||||
{
|
||||
Id = f.Id,
|
||||
Status = f.Status,
|
||||
AddresseeId = f.AddresseeId,
|
||||
RequesterId = f.RequesterId,
|
||||
}).ToList();
|
||||
}
|
||||
@@ -56,7 +56,11 @@ public class FriendsRequestQueryController : Controller
|
||||
{
|
||||
try
|
||||
{
|
||||
var result = await _friendsService.GetResponsesAsync(_currentUserService.GetCurrentUserId());
|
||||
var userId = _currentUserService.GetCurrentUserId();
|
||||
|
||||
_logger.LogInformation("Getting responses by user {userId}", userId);
|
||||
|
||||
var result = await _friendsService.GetResponsesAsync(userId);
|
||||
var response = _mapper.Map<List<FriendshipDto>>(result);
|
||||
|
||||
return Ok(response);
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
using Govor.Application.Interfaces;
|
||||
using Govor.Application.Interfaces.Infrastructure.Extensions;
|
||||
using Govor.Application.Interfaces.Medias;
|
||||
using Govor.Contracts.Requests;
|
||||
using Govor.Core.Models;
|
||||
@@ -15,27 +16,67 @@ public class MediaController : Controller
|
||||
{
|
||||
private readonly ILogger<MediaController> _logger;
|
||||
private readonly IMediaService _mediaService;
|
||||
|
||||
public MediaController(ILogger<MediaController> logger, IMediaService mediaService)
|
||||
private readonly ICurrentUserService _currentUserService;
|
||||
|
||||
public MediaController(
|
||||
ILogger<MediaController> logger,
|
||||
IMediaService mediaService,
|
||||
ICurrentUserService currentUserService)
|
||||
{
|
||||
_logger = logger;
|
||||
_mediaService = mediaService;
|
||||
_currentUserService = currentUserService;
|
||||
}
|
||||
|
||||
[HttpPost("upload")]
|
||||
[RequestSizeLimit(100_000_000)]// ~100MB
|
||||
[RequestSizeLimit(100_000_000)] // ~100MB
|
||||
public async Task<IActionResult> Upload([FromForm] MediaUploadRequest request)
|
||||
{
|
||||
try
|
||||
{
|
||||
var result = await _mediaService.UploadMediaAsync(new Media(request.Data, request.FileName, request.Type,
|
||||
request.MimeType, request.EncryptedKey));
|
||||
|
||||
return Ok(result);
|
||||
if (!ModelState.IsValid)
|
||||
return BadRequest(ModelState);
|
||||
|
||||
// Чтение байт из IFormFile
|
||||
using var memoryStream = new MemoryStream();
|
||||
await request.Data.CopyToAsync(memoryStream);
|
||||
byte[] fileBytes = memoryStream.ToArray();
|
||||
|
||||
var media = new Media(
|
||||
_currentUserService.GetCurrentUserId(),
|
||||
DateTime.UtcNow,
|
||||
fileBytes,
|
||||
request.FileName,
|
||||
request.Type,
|
||||
request.MimeType,
|
||||
request.EncryptedKey
|
||||
);
|
||||
|
||||
var result = await _mediaService.UploadMediaAsync(media);
|
||||
return Ok(result);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
return StatusCode(500, ex);
|
||||
_logger.LogError(ex, "Error uploading media");
|
||||
return StatusCode(500, "Internal server error");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
[HttpGet("download/{id}")]
|
||||
public async Task<IActionResult> Download(Guid id)
|
||||
{
|
||||
try
|
||||
{
|
||||
if (!ModelState.IsValid)
|
||||
return BadRequest(ModelState);
|
||||
|
||||
var media = await _mediaService.GetMediaByIdAsync(id);
|
||||
return File(media.Data, media.MineType, media.FileName);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
_logger.LogError(ex, "Error downloading media");
|
||||
return StatusCode(500, "Internal server error");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user