A lot of changes
This commit is contained in:
@@ -0,0 +1,4 @@
|
||||
# Copilot Instructions
|
||||
|
||||
## Project Guidelines
|
||||
- No crear pruebas/tests si el usuario no lo solicita explícitamente; evitar cambios no pedidos.
|
||||
@@ -0,0 +1,41 @@
|
||||
# Contexto del proyecto para agentes de IA
|
||||
|
||||
## Resumen rápido
|
||||
- Proyecto backend en `.NET 10`.
|
||||
- Estructura principal en capas: `Domain`, `Application`, `Infrastructure`.
|
||||
- Patrón arquitectónico: `Clean Architecture` con enfoque `CQRS`.
|
||||
|
||||
## Arquitectura
|
||||
- **Domain (`Domain`)**: entidades de negocio puras.
|
||||
- **Application (`Application`)**:
|
||||
- Casos de uso (comandos/queries y handlers).
|
||||
- Contratos (`Application.Contracts`).
|
||||
- Configuración de aplicación (`Application.Configuration`).
|
||||
- Mensajería orientada a eventos a través de `IEventBus`.
|
||||
- **Infrastructure (`Infrastructure`)**:
|
||||
- Persistencia y acceso a datos.
|
||||
- Integraciones externas (Keycloak, almacenamiento, procesamiento de imágenes).
|
||||
- Implementación de mensajería y consumidores.
|
||||
|
||||
## CQRS / Mediación
|
||||
- Se usa **DispatchR** (paquete `DispatchR.Mediator`) como variante de MediatR para orquestar comandos y consultas.
|
||||
- Los casos de uso están organizados por feature con handlers dedicados (`Commands`, `Queries`).
|
||||
|
||||
## Mensajería y workers
|
||||
- Se usa **MassTransit** para publicación/consumo de eventos.
|
||||
- Broker actual: **RabbitMQ** (`MassTransit.RabbitMQ`).
|
||||
- Hay modo de registro para workers con consumidores en `Infrastructure.Messaging.Consumers`.
|
||||
- Actualmente existen consumidores como:
|
||||
- `ResizePictureConsumer`
|
||||
- `RemoveFileConsumer`
|
||||
|
||||
## Persistencia e integraciones
|
||||
- `Entity Framework Core` + proveedor `Npgsql` (PostgreSQL).
|
||||
- Integración de identidad con `Keycloak`.
|
||||
- Almacenamiento con `AWS S3`.
|
||||
|
||||
## Notas para agentes
|
||||
- Mantener separación de responsabilidades por capa.
|
||||
- Evitar lógica de infraestructura dentro de `Application` y `Domain`.
|
||||
- Agregar nuevos casos de uso siguiendo el patrón CQRS existente (comando/query + handler).
|
||||
- Para nuevas integraciones asíncronas, reutilizar el pipeline de `MassTransit` y la abstracción `IEventBus`.
|
||||
@@ -9,7 +9,7 @@ namespace Musify.Application.Configuration
|
||||
public static IServiceCollection AddApplicationConfigurations(this IServiceCollection serviceDescriptors, IConfiguration configuration)
|
||||
{
|
||||
serviceDescriptors
|
||||
.AddValidatedConfiguration<StorageConfiguration>(configuration, StorageConfiguration.SectionName)
|
||||
.AddValidatedConfiguration<StorageSettings>(configuration, StorageSettings.SectionName)
|
||||
.AddValidatedConfiguration<PlayListConfiguration>(configuration, PlayListConfiguration.SectionName);
|
||||
|
||||
return serviceDescriptors;
|
||||
|
||||
@@ -1,28 +0,0 @@
|
||||
using System.ComponentModel.DataAnnotations;
|
||||
|
||||
namespace Musify.Application.Configuration
|
||||
{
|
||||
#pragma warning disable CS8618
|
||||
public class StorageConfiguration
|
||||
{
|
||||
public const string SectionName = "Storage";
|
||||
|
||||
[Required]
|
||||
public string BucketName { get; set; }
|
||||
|
||||
[Required]
|
||||
public string ServiceUrl { get; set; }
|
||||
|
||||
[Required]
|
||||
public string AccessKey { get; set; }
|
||||
|
||||
[Required]
|
||||
public string SecretAccessKey { get; set; }
|
||||
|
||||
[Required]
|
||||
public bool ForcePathStyle { get; set; }
|
||||
|
||||
[Required]
|
||||
public bool UseHttp { get; set; }
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,12 @@
|
||||
using System.ComponentModel.DataAnnotations;
|
||||
|
||||
namespace Musify.Application.Configuration
|
||||
{
|
||||
public class StorageSettings
|
||||
{
|
||||
public const string SectionName = "Storage";
|
||||
|
||||
[Required]
|
||||
public required string BucketName { get; set; }
|
||||
}
|
||||
}
|
||||
@@ -12,7 +12,7 @@ using Musify.Domain.Entities;
|
||||
namespace Musify.Application.PlayLists.Commands.CreatePlayList
|
||||
{
|
||||
public class CreatePlayListCommandHandler(IPublishEndpoint publishEndpoint, IDatabase database, IStorageHandler storageHandler,
|
||||
ILogger<CreatePlayListCommandHandler> logger, StorageConfiguration storageConfiguration, PlayListConfiguration playListConfiguration)
|
||||
ILogger<CreatePlayListCommandHandler> logger, StorageSettings storageConfiguration, PlayListConfiguration playListConfiguration)
|
||||
: IRequestHandler<CreatePlayListCommand, Task<Result<PlayListResponse>> >
|
||||
{
|
||||
public async Task<Result<PlayListResponse>> Handle(CreatePlayListCommand request, CancellationToken cancellationToken)
|
||||
@@ -57,7 +57,7 @@ namespace Musify.Application.PlayLists.Commands.CreatePlayList
|
||||
return Result.Error($"Failed to upload picture for PlayList with Id {playList.Id} to storage.");
|
||||
}
|
||||
|
||||
logger.LogInformation("PlayList with Id={PlayListId} created successfully for UserId={UserId}.", playList.Id, request.UserId);
|
||||
logger.LogInformation("Playlist image uploaded successfully for PlayList with Id={PlayListId}.", playList.Id);
|
||||
|
||||
await publishEndpoint.Publish(new ResizePictureEvent(
|
||||
storageConfiguration.BucketName,
|
||||
@@ -85,7 +85,7 @@ namespace Musify.Application.PlayLists.Commands.CreatePlayList
|
||||
await database.PlayLists.AddAsync(playList, cancellationToken);
|
||||
await database.SaveChangesAsync(cancellationToken);
|
||||
|
||||
logger.LogInformation("Picture for PlayList with Id={PlayListId} ", playList.Id);
|
||||
logger.LogInformation("PlayList with id={PlayListId} created successfully for user with id={UserId}.", playList.Id, request.UserId);
|
||||
return Result.Created(PlayListResponse.FromEntity(playList));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -9,7 +9,7 @@ using Musify.Application.Events;
|
||||
namespace Musify.Application.PlayLists.Commands.DeletePlayList
|
||||
{
|
||||
public class DeletePlayListCommandHandler(IDatabase database, IPublishEndpoint publishEndpoint,
|
||||
StorageConfiguration storageConfiguration, PlayListConfiguration playListConfiguration, ILogger<DeletePlayListCommandHandler> logger)
|
||||
StorageSettings storageConfiguration, PlayListConfiguration playListConfiguration, ILogger<DeletePlayListCommandHandler> logger)
|
||||
: IRequestHandler<DeletePlayListCommand, Task<Result>>
|
||||
{
|
||||
public async Task<Result> Handle(DeletePlayListCommand request, CancellationToken cancellationToken)
|
||||
|
||||
@@ -11,7 +11,7 @@ using Musify.Application.PlayLists.Contracts;
|
||||
namespace Musify.Application.PlayLists.Commands.UpdatePlayList
|
||||
{
|
||||
public class UpdatePlayListCommandHandler(IPublishEndpoint publishEndpoint, IDatabase database, IStorageHandler storageHandler,
|
||||
ILogger<UpdatePlayListCommandHandler> logger, StorageConfiguration storageConfiguration, PlayListConfiguration playListConfiguration)
|
||||
ILogger<UpdatePlayListCommandHandler> logger, StorageSettings storageConfiguration, PlayListConfiguration playListConfiguration)
|
||||
: IRequestHandler<UpdatePlayListCommand, Task<Result<PlayListResponse>> >
|
||||
{
|
||||
public async Task<Result<PlayListResponse>> Handle(UpdatePlayListCommand request, CancellationToken cancellationToken)
|
||||
@@ -40,7 +40,7 @@ namespace Musify.Application.PlayLists.Commands.UpdatePlayList
|
||||
{
|
||||
database.PlayLists.Update(playList);
|
||||
await database.SaveChangesAsync(cancellationToken);
|
||||
logger.LogInformation("Updated PlayList with id={PlayListId}.", playList.Id);
|
||||
logger.LogInformation("Updated PlayList with id={PlayListId} without picture changes.", playList.Id);
|
||||
return Result.Success(PlayListResponse.FromEntity(playList));
|
||||
}
|
||||
|
||||
@@ -85,7 +85,7 @@ namespace Musify.Application.PlayLists.Commands.UpdatePlayList
|
||||
return Result.Error($"Failed to upload picture for PlayList with Id {playList.Id} to storage.");
|
||||
}
|
||||
|
||||
playList.OriginalPictureKeyName = newImageId.ToString();
|
||||
playList.OriginalPictureKeyName = imageKeyName;
|
||||
await publishEndpoint.Publish(new ResizePictureEvent(
|
||||
storageConfiguration.BucketName,
|
||||
imageKeyName,
|
||||
@@ -108,7 +108,7 @@ namespace Musify.Application.PlayLists.Commands.UpdatePlayList
|
||||
database.PlayLists.Update(playList);
|
||||
await database.SaveChangesAsync(cancellationToken);
|
||||
|
||||
logger.LogInformation("Updated PlayList with id={PlayListId}.", playList.Id);
|
||||
logger.LogInformation("Updated PlayList with id={PlayListId} and picture key {PictureKeyName}.", playList.Id, imageKeyName);
|
||||
return Result.Success(PlayListResponse.FromEntity(playList));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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,
|
||||
|
||||
+2
-2
@@ -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> >>;
|
||||
}
|
||||
}
|
||||
+2
-2
@@ -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>> >;
|
||||
}
|
||||
+4
-4
@@ -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);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -10,7 +10,8 @@ namespace Musify.Infrastructure.Configuration
|
||||
{
|
||||
serviceDescriptors
|
||||
.AddValidatedConfiguration<DatabaseConfiguration>(configuration, DatabaseConfiguration.SectionName)
|
||||
.AddValidatedConfiguration<KeycloakConfiguration>(configuration, KeycloakConfiguration.SectionName);
|
||||
.AddValidatedConfiguration<KeycloakConfiguration>(configuration, KeycloakConfiguration.SectionName)
|
||||
.AddValidatedConfiguration<StorageClientConfiguration>(configuration, StorageClientConfiguration.SectionName);
|
||||
|
||||
return serviceDescriptors;
|
||||
}
|
||||
|
||||
@@ -0,0 +1,24 @@
|
||||
using System.ComponentModel.DataAnnotations;
|
||||
|
||||
namespace Musify.Infrastructure.Configuration
|
||||
{
|
||||
public class StorageClientConfiguration
|
||||
{
|
||||
public const string SectionName = "Storage";
|
||||
|
||||
[Required]
|
||||
public required string ServiceUrl { get; set; }
|
||||
|
||||
[Required]
|
||||
public required string AccessKey { get; set; }
|
||||
|
||||
[Required]
|
||||
public required string SecretAccessKey { get; set; }
|
||||
|
||||
[Required]
|
||||
public bool ForcePathStyle { get; set; }
|
||||
|
||||
[Required]
|
||||
public bool UseHttp { get; set; }
|
||||
}
|
||||
}
|
||||
@@ -1,7 +1,7 @@
|
||||
using Amazon.S3;
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
using Musify.Application.Configuration;
|
||||
using Musify.Application.Contracts.Infrastructure;
|
||||
using Musify.Infrastructure.Configuration;
|
||||
|
||||
namespace Musify.Infrastructure.Storage
|
||||
{
|
||||
@@ -11,7 +11,7 @@ namespace Musify.Infrastructure.Storage
|
||||
{
|
||||
serviceDescriptors.AddScoped<IAmazonS3>(serviceProvider =>
|
||||
{
|
||||
var configuration = serviceProvider.GetRequiredService<StorageConfiguration>();
|
||||
var configuration = serviceProvider.GetRequiredService<StorageClientConfiguration>();
|
||||
var s3Configuration = new AmazonS3Config
|
||||
{
|
||||
ServiceURL = configuration.ServiceUrl,
|
||||
|
||||
Reference in New Issue
Block a user