Get me and cors

This commit is contained in:
Veljko Tosic
2026-02-15 23:01:12 +01:00
parent 6d8ae9bef6
commit 3463ba1e6d
5 changed files with 70 additions and 3 deletions

View File

@@ -0,0 +1,5 @@
using AipsCore.Application.Abstract.Query;
namespace AipsCore.Application.Models.User.Query.GetMe;
public record GetMeQuery : IQuery<GetMeQueryDto>;

View File

@@ -0,0 +1,3 @@
namespace AipsCore.Application.Models.User.Query.GetMe;
public record GetMeQueryDto(string UserName);

View File

@@ -0,0 +1,37 @@
using AipsCore.Application.Abstract.Query;
using AipsCore.Application.Abstract.UserContext;
using AipsCore.Domain.Common.Validation;
using AipsCore.Domain.Models.User.Validation;
using AipsCore.Domain.Models.User.ValueObjects;
using AipsCore.Infrastructure.Persistence.Db;
using Microsoft.EntityFrameworkCore;
namespace AipsCore.Application.Models.User.Query.GetMe;
public class GetMeQueryHandler : IQueryHandler<GetMeQuery, GetMeQueryDto>
{
private readonly AipsDbContext _context;
private readonly IUserContext _userContext;
public GetMeQueryHandler(AipsDbContext context, IUserContext userContext)
{
_context = context;
_userContext = userContext;
}
public async Task<GetMeQueryDto> Handle(GetMeQuery query, CancellationToken cancellationToken = default)
{
var userId = _userContext.GetCurrentUserId();
var result = await _context.Users
.Where(u => u.Id.ToString() == userId.IdValue)
.FirstOrDefaultAsync(cancellationToken);
if (result is null)
{
throw new ValidationException(UserErrors.NotFound(new UserId(userId.IdValue)));
}
return new GetMeQueryDto(result.UserName!);
}
}