Changes on messages

This commit is contained in:
2026-03-28 21:26:45 +01:00
parent 4fa341d2c8
commit 2518101a52
25 changed files with 153 additions and 179 deletions
@@ -1,4 +1,4 @@
using Ardalis.Result;
using Ardalis.Result;
using DispatchR.Abstractions.Send;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Logging;
@@ -22,8 +22,8 @@ namespace Musify.Application.PlayLists.Handlers
var userExists = await database.Users.AnyAsync(u => u.Id == request.UserId, cancellationToken);
if (!userExists)
{
logger.LogWarning("User with Id={UserId} does not exist.", request.UserId);
return Result.NotFound($"User with Id {request.UserId} does not exist.");
logger.LogWarning("User {UserId} not found", request.UserId);
return Result.NotFound($"User {request.UserId} not found");
}
var playListEntity = CreatePlayListCommand.ToEntity(request,
@@ -54,11 +54,11 @@ namespace Musify.Application.PlayLists.Handlers
catch (Exception exception)
{
await RollbackPicture(playListEntity, cancellationToken);
logger.LogError(exception, "Failed to create PlayList for user with id={UserId}.", request.UserId);
return Result.Error($"Failed to create PlayList for user with id {request.UserId}.");
logger.LogError(exception, "Failed to create playlist for user {UserId}", request.UserId);
return Result.Error($"Failed to create playlist for user {request.UserId}");
}
logger.LogInformation("PlayList with id={PlayListId} created successfully for user with id={UserId}.", playListEntity.Id, request.UserId);
logger.LogInformation("Created playlist {PlayListId} for user {UserId}", playListEntity.Id, request.UserId);
return Result.Created(PlayListResponse.FromEntity(playListEntity));
}
@@ -71,7 +71,7 @@ namespace Musify.Application.PlayLists.Handlers
}
catch (Exception exception)
{
logger.LogError(exception, "Failed to rollback picture upload for PlayList with Id={PlayListId}.", playList.Id);
logger.LogError(exception, "Failed to rollback picture upload for playlist {PlayListId}", playList.Id);
}
}
@@ -83,7 +83,7 @@ namespace Musify.Application.PlayLists.Handlers
var uploadResult = await storageHandler.UploadFileAsync(picture.FileStream, picture.ContentType, storageConfiguration.BucketName, pictureKey, cancellationToken);
if (!uploadResult.IsSuccess)
{
logger.LogError("Failed to upload picture for PlayList with Id={PlayListId} to storage. Storage handler returned error={ErrorMessage}.",
logger.LogError("Failed to upload picture for playlist {PlayListId} to storage: {ErrorMessage}",
playList.Id, string.Join(", ", uploadResult.Errors));
return uploadResult.ToErrorResult();
}
@@ -118,8 +118,8 @@ namespace Musify.Application.PlayLists.Handlers
}
catch (Exception exception)
{
logger.LogError(exception, "Failed to publish UpdatePlayListPictureEvent for PlayList with Id={PlayListId}.", playList.Id);
return Result.Error($"Failed to publish UpdatePlayListPictureEvent for PlayList with Id {playList.Id}.");
logger.LogError(exception, "Failed to publish playlist picture update event for playlist {PlayListId}", playList.Id);
return Result.Error($"Failed to publish playlist {playList.Id} picture update event");
}
}
}
@@ -1,4 +1,4 @@
using Ardalis.Result;
using Ardalis.Result;
using DispatchR.Abstractions.Send;
using Microsoft.Extensions.Logging;
using Musify.Application.Configuration;
@@ -19,13 +19,13 @@ namespace Musify.Application.PlayLists.Handlers
cancellationToken);
if (playList is null)
{
logger.LogDebug("PlayList with id={PlayListId} not found", request.PlayListId);
logger.LogDebug("Playlist {PlayListId} not found", request.PlayListId);
return Result.NotFound();
}
if (playList.UserId != request.UserId)
{
logger.LogInformation("PlayList with id={PlayListId} does not belong to user with id={UserId}", request.PlayListId, request.UserId);
logger.LogWarning("Playlist {PlayListId} does not belong to user {UserId}", request.PlayListId, request.UserId);
return Result.Unauthorized();
}
@@ -56,8 +56,8 @@ namespace Musify.Application.PlayLists.Handlers
}
catch (Exception exception)
{
logger.LogError(exception, "Failed to publish remove file event for PlayList id={PlayListId} and key={Key}.", playListId, key);
return Result.Error($"Failed to publish remove file event for PlayList with id {playListId}.");
logger.LogError(exception, "Failed to publish remove file event for playlist {PlayListId} with key {Key}", playListId, key);
return Result.Error($"Failed to publish remove file event for playlist {playListId}");
}
}
}
@@ -1,4 +1,4 @@
using Ardalis.Result;
using Ardalis.Result;
using DispatchR.Abstractions.Send;
using Microsoft.EntityFrameworkCore;
using Musify.Application.Contracts.Infrastructure;
@@ -18,7 +18,7 @@ namespace Musify.Application.PlayLists.Handlers
return playList is not null
? Result.Success(PlayListResponse.FromEntity(playList))
: Result.NotFound("Playlist not found.");
: Result.NotFound("Playlist not found");
}
}
}
@@ -1,4 +1,4 @@
using Ardalis.Result;
using Ardalis.Result;
using DispatchR.Abstractions.Send;
using Microsoft.Extensions.Logging;
using Musify.Application.Configuration;
@@ -24,13 +24,13 @@ namespace Musify.Application.PlayLists.Handlers
cancellationToken);
if (playListEntity is null)
{
logger.LogInformation("PlayList with id={PlayListId} not found.", request.PlayListId);
logger.LogInformation("Playlist {PlayListId} not found", request.PlayListId);
return Result.NotFound();
}
if (playListEntity.UserId != request.UserId)
{
logger.LogWarning("User with id={UserId} is not the owner of the PlayList with id={PlayListId}.", request.UserId, request.PlayListId);
logger.LogWarning("User {UserId} is not the owner of playlist {PlayListId}", request.UserId, request.PlayListId);
return Result.Unauthorized();
}
@@ -50,11 +50,11 @@ namespace Musify.Application.PlayLists.Handlers
catch (Exception exception)
{
await Rollback(updatePictureResult.Value, cancellationToken);
logger.LogError(exception, "Failed to update PlayList with id={PlayListId}.", playListEntity.Id);
return Result.Error($"Failed to update PlayList with id {playListEntity.Id}.");
logger.LogError(exception, "Failed to update playlist {PlayListId}", playListEntity.Id);
return Result.Error($"Failed to update playlist {playListEntity.Id}");
}
logger.LogInformation("Successfully updated PlayList with id={PlayListId}.", playListEntity.Id);
logger.LogInformation("Updated playlist {PlayListId}", playListEntity.Id);
return Result.Success(PlayListResponse.FromEntity(playListEntity));
}
@@ -89,9 +89,9 @@ namespace Musify.Application.PlayLists.Handlers
if (!uploadResult.IsSuccess)
{
logger.LogError("Failed to upload picture for PlayList with Id={PlayListId} to storage. Storage handler returned error?={ErrorMessage}",
logger.LogError("Failed to upload picture for playlist {PlayListId} to storage: {ErrorMessage}",
playList.Id, string.Join("; ", uploadResult.Errors));
return Result.Error($"Failed to upload picture for PlayList with Id {playList.Id} to storage.");
return Result.Error($"Failed to upload playlist {playList.Id} picture");
}
playList.OriginalPictureName = pictureName;
return Result.Success(pictureKey);
@@ -122,8 +122,8 @@ namespace Musify.Application.PlayLists.Handlers
}
catch (Exception exception)
{
logger.LogError(exception, "Failed to publish UpdatePlayListPictureEvent for PlayList with id={PlayListId}.", playList.Id);
return Result.Error($"Failed to publish UpdatePlayListPictureEvent for PlayList with id {playList.Id}.");
logger.LogError(exception, "Failed to publish playlist picture update event for playlist {PlayListId}", playList.Id);
return Result.Error($"Failed to publish playlist {playList.Id} picture update event");
}
}
@@ -152,7 +152,7 @@ namespace Musify.Application.PlayLists.Handlers
storageConfiguration.BucketName,
pictureKey,
cancellationToken);
logger.LogInformation("Rolled back uploaded picture with key={PictureKey} from storage.", pictureKey);
logger.LogInformation("Rolled back uploaded picture {PictureKey} from storage", pictureKey);
}
async Task<Result> PublishRemoveFileEvent(string key, CancellationToken cancellationToken)
@@ -166,8 +166,8 @@ namespace Musify.Application.PlayLists.Handlers
}
catch (Exception exception)
{
logger.LogError(exception, "Failed to publish remove file event for key={Key}.", key);
return Result.Error($"Failed to publish remove file event for key {key}.");
logger.LogError(exception, "Failed to publish remove file event for key {Key}", key);
return Result.Error($"Failed to publish remove file event for {key}");
}
}
}
@@ -1,4 +1,4 @@
using Ardalis.Result;
using Ardalis.Result;
using DispatchR.Abstractions.Send;
using Microsoft.Extensions.Logging;
using Musify.Application.Configuration;
@@ -17,11 +17,10 @@ namespace Musify.Application.Tracks.Handler
public async Task<Result<TrackResponse>> Handle(CreateTrackCommand request, CancellationToken cancellationToken)
{
var userExists = await database.Users.FindAsync(request.UserId, cancellationToken);
if (userExists is null)
{
logger.LogWarning("User with id={UserId} not found.", request.UserId);
return Result.NotFound($"User with id {request.UserId} not found.");
logger.LogWarning("User {UserId} not found", request.UserId);
return Result.NotFound($"User {request.UserId} not found");
}
var trackEntity = CreateTrackCommand.ToEntity(
@@ -60,7 +59,7 @@ namespace Musify.Application.Tracks.Handler
if (!pictureUpload.IsSuccess)
{
var errors = string.Join(", ", pictureUpload.Errors);
return Result.Error($"Failed to upload picture for track {request.Title}. Errors: {errors}.");
return Result.Error($"Failed to upload picture for {request.Title}");
}
var audioUpload = await storageHandler.UploadFileAsync(
@@ -74,7 +73,7 @@ namespace Musify.Application.Tracks.Handler
{
await RollbackFilesAsync(pictureKey, audioKey, cancellationToken);
var errors = string.Join(", ", audioUpload.Errors);
return Result.Error($"Failed to upload audio for track {request.Title}. Errors: {errors}.");
return Result.Error($"Failed to upload audio for {request.Title}");
}
return Result.Success();
@@ -84,7 +83,7 @@ namespace Musify.Application.Tracks.Handler
{
await storageHandler.RemoveFileAsync(storageConfiguration.BucketName, pictureKey, cancellationToken);
await storageHandler.RemoveFileAsync(storageConfiguration.BucketName, audioKey, cancellationToken);
logger.LogInformation("Rolled back uploaded files={PictureKey}, {AudioKey}.", pictureKey, audioKey);
logger.LogInformation("Rolled back uploaded files {PictureKey}, {AudioKey}", pictureKey, audioKey);
}
async Task PublishUpdateEvent(Track track, CancellationToken cancellationToken)
@@ -110,7 +109,7 @@ namespace Musify.Application.Tracks.Handler
}
catch (Exception exception)
{
logger.LogError(exception, "Failed to publish resize picture event for Track with id={TrackId}.", track.Id);
logger.LogError(exception, "Failed to publish track picture update event for track {TrackId}", track.Id);
}
}
@@ -130,7 +129,7 @@ namespace Musify.Application.Tracks.Handler
}
catch (Exception exception)
{
logger.LogError(exception, "Failed to publish transcode audio event for Track with id={TrackId}.", track.Id);
logger.LogError(exception, "Failed to publish track audio update event for track {TrackId}", track.Id);
}
}
}
@@ -1,4 +1,4 @@
using Ardalis.Result;
using Ardalis.Result;
using DispatchR.Abstractions.Send;
using Microsoft.Extensions.Logging;
using Musify.Application.Configuration;
@@ -15,16 +15,15 @@ namespace Musify.Application.Tracks.Handler
public async Task<Result> Handle(DeleteTrackCommand request, CancellationToken cancellationToken)
{
var track = await database.Tracks.FindAsync(request.TrackId, cancellationToken);
if (track is null)
{
logger.LogInformation("Track with id={TrackId} not found.", request.TrackId);
logger.LogInformation("Track {TrackId} not found", request.TrackId);
return Result.NotFound();
}
if (track.Id != request.UserId)
{
logger.LogInformation("User with id={UserId} is not the owner of track with id={TrackId}.", request.UserId, request.TrackId);
logger.LogWarning("User {UserId} unauthorized to delete track {TrackId}", request.UserId, request.TrackId);
return Result.Unauthorized();
}
@@ -63,7 +62,7 @@ namespace Musify.Application.Tracks.Handler
async Task RemoveFile(string path, CancellationToken cancellationToken)
{
await storageHandler.RemoveFileAsync(storageConfiguration.BucketName, path, cancellationToken);
logger.LogDebug("File with path={Path} removed from storage.", path);
logger.LogDebug("Removed file {Path} from storage", path);
}
}
}
@@ -1,4 +1,4 @@
using Ardalis.Result;
using Ardalis.Result;
using DispatchR.Abstractions.Send;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Logging;
@@ -14,24 +14,24 @@ namespace Musify.Application.Users.Handlers
public async Task<Result<UserResponse>> Handle(CreateUserCommand request, CancellationToken cancellationToken)
{
var userExists = await database.Users.AnyAsync(u => u.Id == request.Id, cancellationToken);
if (!userExists)
if (userExists)
{
logger.LogInformation("User with Id={UserId} already exists.", request.Id);
return Result<UserResponse>.Conflict($"A user with Id {request.Id} already exists.");
logger.LogInformation("User {UserId} already exists", request.Id);
return Result<UserResponse>.Conflict($"User {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.");
logger.LogWarning("Keycloak user {UserId} not found", request.Id);
return Result.NotFound("Keycloak user not found");
}
var newUser = CreateUserCommand.ToEntity(request);
await database.Users.AddAsync(newUser, cancellationToken);
await database.SaveChangesAsync(cancellationToken);
logger.LogInformation("User with Id={UserId} created successfully.", newUser.Id);
logger.LogInformation("Created user {UserId}", newUser.Id);
return Result.Created(new UserResponse(
newUser.Id.ToString(),
keycloakUser.Value.Name,
@@ -10,7 +10,6 @@ namespace Musify.Application.Users.Commands
public async Task<Result> Handle(DeleteUserCommand request, CancellationToken cancellationToken)
{
var existingUser = await database.Users.FindAsync(request.Id, cancellationToken);
if (existingUser is null)
return Result.NotFound();
@@ -1,4 +1,4 @@
using Ardalis.Result;
using Ardalis.Result;
using DispatchR.Abstractions.Send;
using Microsoft.EntityFrameworkCore;
using Musify.Application.Contracts.Infrastructure;
@@ -15,14 +15,12 @@ namespace Musify.Application.Users.Handlers
var user = await database.Users
.AsNoTracking()
.SingleOrDefaultAsync(u => u.Id == request.Id, cancellationToken);
if (user is null)
return Result.NotFound("User not found.");
return Result.NotFound("User not found");
var keycloakUser = await keycloakUserClient.GetUserByIdAsync(user.Id.ToString(), cancellationToken);
if (!keycloakUser.IsSuccess)
return Result.NotFound("Keycloak user not found.");
return Result.NotFound("Keycloak user not found");
return Result.Success(new UserResponse(
user.Id.ToString(),
@@ -15,14 +15,12 @@ namespace Musify.Application.Users.Handlers
var user = await database.Users
.AsNoTracking()
.SingleOrDefaultAsync(u => u.Id == request.Id, cancellationToken);
if (user is null)
return Result.NotFound("User not found.");
return Result.NotFound("User not found");
var keycloakUser = await keycloakUserClient.GetUserByIdAsync(user.Id.ToString(), cancellationToken);
if (!keycloakUser.IsSuccess)
return Result.NotFound("Keycloak user not found.");
return Result.NotFound("Keycloak user not found");
return Result.Success(new UserResponse(
user.Id.ToString(),