async Task<string> GenerateJwtTokenAsync(User user)

This commit is contained in:
Artemy
2025-07-02 15:26:27 +07:00
parent 0967fe1b4e
commit 0669614a5e
13 changed files with 131 additions and 22 deletions
@@ -24,16 +24,35 @@ public class UsersController : Controller
[HttpGet]
public async Task<IActionResult> AllUsers()
{
_logger.LogInformation("Getting all users by administrator");
var read = await _users.GetAllUsersAsync();
try
{
_logger.LogInformation("Getting all users by administrator");
var read = await _users.GetAllUsersAsync();
return Ok(BuildUserDtos(read));
return Ok(BuildUserDtos(read));
}
catch (Exception e)
{
_logger.LogError(e, e.Message);
return StatusCode(500, e.Message);
}
}
[HttpGet("{id:guid}")]
public async Task<IActionResult> GetUserById(Guid id)
{
return Ok(id);
try
{
_logger.LogInformation($"Getting user {id} by administrator");
var read = await _users.GetUserById(id);
return Ok(BuildUserDtos([read]).First());
}
catch (Exception e)
{
_logger.LogError(e, e.Message);
return StatusCode(500, e.Message);
}
}
private List<UserResponse> BuildUserDtos(IEnumerable<User> users) => users.Select(user => new UserResponse
+50
View File
@@ -0,0 +1,50 @@
using Govor.Application.Interfaces;
using Govor.Contracts.Requests;
using Govor.Core.Models;
using Govor.Core.Repositories.MediasAttachments;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
namespace Govor.API.Controllers;
[ApiController]
[Route("api/media")]
[Authorize(Roles = "User,Admin")]
public class MediaController : Controller
{
private readonly ILogger<MediaController> _logger;
private readonly IStorageService _storageService;
private readonly IMediaAttachmentsRepository _repository;
public MediaController(ILogger<MediaController> logger, IStorageService storageService)
{
_logger = logger;
_storageService = storageService;
}
[HttpPost("upload")]
[RequestSizeLimit(100_000_000)]// ~100MB
public async Task<IActionResult> Upload([FromForm] MediaUploadRequest request)
{
try
{
var url = await _storageService.SaveAsync(request.Data,request.FileName);
var mediaId = Guid.NewGuid();
_repository.AddAsync(new MediaAttachments()
{
Id = mediaId,
FilePath = url,
EncryptedKey = request.EncryptedKey,
MimeType = request.MimeType,
Type = request.Type,
});
return Ok(mediaId);
}
catch (Exception ex)
{
return StatusCode(500, ex);
}
}
}
+10 -9
View File
@@ -1,4 +1,5 @@
using System.Security.Claims;
using Govor.Contracts.Requests.SignalR;
using Govor.Core.Models;
using Govor.Core.Repositories.Users;
using Govor.Data.Repositories.Exceptions;
@@ -48,19 +49,19 @@ public class ChatsHub : Hub
}
public async Task Send(string message, Guid toUserId)
public async Task Send(MessageRequest request)
{
// Валидация входных данных
if (string.IsNullOrWhiteSpace(message))
if (string.IsNullOrWhiteSpace(request.EncryptedContent))
{
_logger.LogWarning("Empty message received from user {UserId}", GetUserId());
throw new ArgumentException("Message cannot be empty", nameof(message));
throw new ArgumentException("Message cannot be empty", nameof(request.EncryptedContent));
}
var senderId = GetUserId();
// Проверка существования получателя
try
/*try
{
await _usersRepository.FindByIdAsync(toUserId);
}
@@ -73,19 +74,19 @@ public class ChatsHub : Hub
{
_logger.LogWarning("Invalid recipient userId received from user {UserId}", GetUserId());
throw;
}
}*/
try
{
_logger.LogInformation("Message sent from {SenderId} to {RecipientId}: {Message}", senderId, toUserId, message);
_logger.LogInformation("Message sent from {SenderId} to {RecipientId} at {UtcNow}", senderId, request.RecipientId, DateTime.UtcNow);
// Отправка сообщения отправителю и получателю
await Clients.Group(toUserId.ToString()).SendAsync("Receive", message, senderId);
await Clients.Group(senderId.ToString()).SendAsync("Receive", message, senderId);
//await Clients.Group(request.RecipientId.ToString()).SendAsync("Receive", message, senderId);
// Clients.Group(senderId.ToString()).SendAsync("Receive", message, senderId);
}
catch (Exception ex)
{
_logger.LogError(ex, "Error sending message from {SenderId} to {RecipientId}", senderId, toUserId);
//_logger.LogError(ex, "Error sending message from {SenderId} to {RecipientId}", senderId, toUserId);
throw;
}
}