A lot of changes

This commit is contained in:
2026-03-19 20:21:02 +01:00
parent 954101548a
commit d0a39b2824
17 changed files with 119 additions and 59 deletions
@@ -15,15 +15,21 @@ namespace Musify.Application.Users.Commands.CreateUser
{
logger.LogDebug("Handling {CommandName} with Id: {UserId}", nameof(CreateUserCommand), request.Id);
var existingUser = database.Users.SingleOrDefaultAsync(u => u.Id == request.Id, cancellationToken);
var existingUser = await database.Users.SingleOrDefaultAsync(u => u.Id == request.Id, cancellationToken);
if (existingUser is not null)
{
logger.LogInformation("User with Id={UserId} already exists.", request.Id);
return Result<UserResponse>.Conflict($"A user with Id {request.Id} already exists.");
}
var keycloakUser = await keycloakUserClient.GetUserByIdAsync(request.Id.ToString(), cancellationToken);
if (!keycloakUser.IsSuccess)
{
logger.LogWarning("Keycloak user with Id={UserId} was not found.", request.Id);
return Result.NotFound("Keycloak user not found.");
}
var newUser = new User
{
@@ -31,9 +37,9 @@ namespace Musify.Application.Users.Commands.CreateUser
};
await database.Users.AddAsync(newUser, cancellationToken);
await database.SaveChangesAsync(cancellationToken);
logger.LogInformation("User with Id={UserId} created successfully.", newUser.Id);
return Result.Created(new UserResponse(
newUser.Id.ToString(),
keycloakUser.Value.Name,
@@ -1,4 +1,4 @@
using Ardalis.Result;
using Ardalis.Result;
using DispatchR.Abstractions.Send;
using Musify.Application.Users.Contracts;
@@ -6,4 +6,4 @@ namespace Musify.Application.Users.Queries.GetUserByKeycloakId
{
public record GetUserByKeycloakQuery(Guid Id)
: IRequest<GetUserByKeycloakQuery, Task<Result<UserResponse> >>;
}
}
@@ -1,4 +1,4 @@
using Ardalis.Result;
using Ardalis.Result;
using DispatchR.Abstractions.Send;
using Microsoft.EntityFrameworkCore;
using Musify.Application.Contracts.Infrastructure;
@@ -32,4 +32,4 @@ namespace Musify.Application.Users.Queries.GetUserByKeycloakId
user.UpdatedAt));
}
}
}
}
@@ -1,9 +0,0 @@
using Ardalis.Result;
using DispatchR.Abstractions.Send;
using Musify.Application.Users.Contracts;
namespace Musify.Application.Users.Queries.GetUsers
{
public record GetUsersCommand(int PageNumber, int PageSize, string UsernameSearch)
: IRequest<GetUsersCommand, Task<Result<PaginatedUserResponse>> >;
}
@@ -0,0 +1,9 @@
using Ardalis.Result;
using DispatchR.Abstractions.Send;
using Musify.Application.Users.Contracts;
namespace Musify.Application.Users.Queries.GetUsers
{
public record GetUsersQuery(int PageNumber, int PageSize, string UsernameSearch)
: IRequest<GetUsersQuery, Task<Result<PaginatedUserResponse>> >;
}
@@ -1,4 +1,4 @@
using Ardalis.Result;
using Ardalis.Result;
using DispatchR.Abstractions.Send;
using Microsoft.EntityFrameworkCore;
using Musify.Application.Contracts.Infrastructure;
@@ -9,9 +9,9 @@ using X.PagedList.Extensions;
namespace Musify.Application.Users.Queries.GetUsers
{
public class GetUsersQueryHandler(IDatabase database, IKeycloakUserClient keycloakUserClient)
: IRequestHandler<GetUsersCommand, Task<Result<PaginatedUserResponse> >>
: IRequestHandler<GetUsersQuery, Task<Result<PaginatedUserResponse> >>
{
public async Task<Result<PaginatedUserResponse>> Handle(GetUsersCommand request, CancellationToken cancellationToken)
public async Task<Result<PaginatedUserResponse>> Handle(GetUsersQuery request, CancellationToken cancellationToken)
{
var pageNumber = request.PageNumber < 1 ? 1 : request.PageNumber;
var pageSize = request.PageSize < 1 ? 10 : request.PageSize;
@@ -56,4 +56,4 @@ namespace Musify.Application.Users.Queries.GetUsers
return Result.Success(response);
}
}
}
}