A lot of changes
This commit is contained in:
@@ -24,4 +24,8 @@
|
||||
<ProjectReference Include="..\Domain\Domain.csproj" />
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<Folder Include="Tracks\Queries\" />
|
||||
</ItemGroup>
|
||||
|
||||
</Project>
|
||||
|
||||
@@ -10,7 +10,8 @@ namespace Musify.Application.Configuration
|
||||
{
|
||||
serviceDescriptors
|
||||
.AddValidatedConfiguration<ApplicationStorageConfiguration>(configuration, ApplicationStorageConfiguration.SectionName)
|
||||
.AddValidatedConfiguration<PlayListConfiguration>(configuration, PlayListConfiguration.SectionName);
|
||||
.AddValidatedConfiguration<PlayListConfiguration>(configuration, PlayListConfiguration.SectionName)
|
||||
.AddValidatedConfiguration<TrackConfiguration>(configuration, TrackConfiguration.SectionName);
|
||||
|
||||
return serviceDescriptors;
|
||||
}
|
||||
|
||||
@@ -7,14 +7,17 @@ namespace Musify.Application.Configuration
|
||||
public const string SectionName = "PlayListConfiguration";
|
||||
|
||||
[Required]
|
||||
public Routes Routes { get; set; } = new Routes();
|
||||
public PlayListRoutes Routes { get; set; } = new PlayListRoutes();
|
||||
|
||||
[Required]
|
||||
public PicturesSizes PicturesSizes { get; set; } = new PicturesSizes();
|
||||
public PlayListPicturesSizes PicturesSizes { get; set; } = new PlayListPicturesSizes();
|
||||
}
|
||||
|
||||
public class Routes
|
||||
public class PlayListRoutes
|
||||
{
|
||||
[Required]
|
||||
public string ParentFolders { get; set; } = "PlayLists";
|
||||
|
||||
[Required]
|
||||
public string OriginalPictures { get; set; } = "OriginalPictures";
|
||||
|
||||
@@ -37,7 +40,7 @@ namespace Musify.Application.Configuration
|
||||
public string PresetLargePicture { get; set; } = "PresetLargePicture.webp";
|
||||
}
|
||||
|
||||
public class PicturesSizes
|
||||
public class PlayListPicturesSizes
|
||||
{
|
||||
[Range(1, 1024)]
|
||||
public int SmallPictureWidth { get; set; } = 128;
|
||||
|
||||
@@ -0,0 +1,63 @@
|
||||
using System.ComponentModel.DataAnnotations;
|
||||
|
||||
namespace Musify.Application.Configuration
|
||||
{
|
||||
public class TrackConfiguration
|
||||
{
|
||||
public const string SectionName = "TrackConfiguration";
|
||||
|
||||
[Required]
|
||||
public TrackRoutes Routes { get; set; } = new TrackRoutes();
|
||||
|
||||
[Required]
|
||||
public TrackPicturesSizes PicturesSizes { get; set; } = new TrackPicturesSizes();
|
||||
}
|
||||
|
||||
public class TrackRoutes
|
||||
{
|
||||
[Required]
|
||||
public string ParentFolder { get; set; } = "Tracks";
|
||||
|
||||
[Required]
|
||||
public string OriginalPictures { get; set; } = "OriginalTracks";
|
||||
|
||||
[Required]
|
||||
public string SmallPictures { get; set; } = "SmallTracks";
|
||||
|
||||
[Required]
|
||||
public string MediumPictures { get; set; } = "MediumTracks";
|
||||
|
||||
[Required]
|
||||
public string LargePictures { get; set; } = "LargeTracks";
|
||||
|
||||
[Required]
|
||||
public string PresetSmallPicture { get; set; } = "PresetSmallPicture.webp";
|
||||
|
||||
[Required]
|
||||
public string PresetMediumPicture { get; set; } = "PresetMediumPicture.webp";
|
||||
|
||||
[Required]
|
||||
public string PresetLargePicture { get; set; } = "PresetLargePicture.webp";
|
||||
}
|
||||
|
||||
public class TrackPicturesSizes
|
||||
{
|
||||
[Range(1, 1024)]
|
||||
public int SmallPictureWidth { get; set; } = 128;
|
||||
|
||||
[Range(1, 1024)]
|
||||
public int SmallPictureHeight { get; set; } = 128;
|
||||
|
||||
[Range(1, 1024)]
|
||||
public int MediumPictureWidth { get; private set; } = 256;
|
||||
|
||||
[Range(1, 1024)]
|
||||
public int MediumPictureHeight { get; private set; } = 256;
|
||||
|
||||
[Range(1, 1024)]
|
||||
public int LargePictureWidth { get; private set; } = 512;
|
||||
|
||||
[Range(1, 1024)]
|
||||
public int LargePictureHeight { get; private set; } = 512;
|
||||
}
|
||||
}
|
||||
@@ -11,6 +11,8 @@ namespace Musify.Application.Contracts.Infrastructure
|
||||
|
||||
DbSet<Track> Tracks { get; }
|
||||
|
||||
DbSet<UserHasTrack> UserHasTracks { get; }
|
||||
|
||||
DbSet<PlayListHasTrack> PlayListHasTracks { get; }
|
||||
|
||||
DbSet<Upload> Uploads { get; }
|
||||
|
||||
@@ -0,0 +1,4 @@
|
||||
namespace Musify.Application.Events
|
||||
{
|
||||
public record ProcessTrackEvent();
|
||||
}
|
||||
@@ -1,6 +1,4 @@
|
||||
using Musify.Application.Contracts.Application;
|
||||
|
||||
namespace Musify.Application.Events
|
||||
namespace Musify.Application.Events
|
||||
{
|
||||
public record RemoveFileEvent(
|
||||
string BucketName,
|
||||
|
||||
@@ -0,0 +1,16 @@
|
||||
namespace Musify.Application.Events
|
||||
{
|
||||
public record UpdateTrackPictureEvent(
|
||||
Guid TrackId,
|
||||
string BucketName,
|
||||
string OriginalPictureKeyName,
|
||||
string SmallPictureKeyName,
|
||||
int SmallPictureWidth,
|
||||
int SmallPictureHeight,
|
||||
string MediumPictureKeyName,
|
||||
int MediumPictureWidth,
|
||||
int MediumPictureHeight,
|
||||
string LargePictureKeyName,
|
||||
int LargePictureWidth,
|
||||
int LargePictureHeight);
|
||||
}
|
||||
@@ -10,9 +10,8 @@ using Musify.Domain.Entities;
|
||||
|
||||
namespace Musify.Application.PlayLists.Commands.CreatePlayList
|
||||
{
|
||||
public class CreatePlayListCommandHandler(IEventBus eventBus, IDatabase database, IStorageHandler storageHandler,
|
||||
ILogger<CreatePlayListCommandHandler> logger, ApplicationStorageConfiguration storageConfiguration,
|
||||
PlayListConfiguration playListConfiguration, IPictureHandler pictureHandler)
|
||||
public class CreatePlayListCommandHandler(IEventBus eventBus, IDatabase database, IStorageHandler storageHandler, IPictureHandler pictureHandler,
|
||||
ILogger<CreatePlayListCommandHandler> logger, ApplicationStorageConfiguration storageConfiguration, PlayListConfiguration playListConfiguration)
|
||||
: IRequestHandler<CreatePlayListCommand, Task<Result<PlayListResponse>> >
|
||||
{
|
||||
public async Task<Result<PlayListResponse>> Handle(CreatePlayListCommand request, CancellationToken cancellationToken)
|
||||
@@ -55,8 +54,6 @@ namespace Musify.Application.PlayLists.Commands.CreatePlayList
|
||||
playList.Id, string.Join("; ", uploadResult.Errors));
|
||||
return Result.Error($"Failed to upload picture for PlayList with Id {playList.Id} to storage.");
|
||||
}
|
||||
|
||||
playList.OriginalPictureKeyName = pictureName;
|
||||
logger.LogInformation("Playlist image uploaded successfully for PlayList with Id={PlayListId}.", playList.Id);
|
||||
|
||||
var smallPictureKeyName = Path.Combine(
|
||||
|
||||
@@ -27,36 +27,28 @@ namespace Musify.Application.PlayLists.Commands.DeletePlayList
|
||||
return Result.Unauthorized();
|
||||
}
|
||||
|
||||
if (!string.IsNullOrWhiteSpace(playList.OriginalPictureKeyName))
|
||||
var keysToRemove = new List<string>();
|
||||
|
||||
if (playList.SmallPictureKeyName != playListConfiguration.Routes.PresetSmallPicture)
|
||||
{
|
||||
var keysToRemove = new List<string>
|
||||
{
|
||||
Path.Join(playListConfiguration.Routes.OriginalPictures, playList.OriginalPictureKeyName)
|
||||
};
|
||||
keysToRemove.Add(playList.SmallPictureKeyName);
|
||||
}
|
||||
if (playList.MediumPictureKeyName != playListConfiguration.Routes.PresetMediumPicture)
|
||||
{
|
||||
keysToRemove.Add(playList.MediumPictureKeyName);
|
||||
}
|
||||
if (playList.LargePictureKeyName != playListConfiguration.Routes.PresetLargePicture)
|
||||
{
|
||||
keysToRemove.Add(playList.LargePictureKeyName);
|
||||
}
|
||||
|
||||
if (playList.SmallPictureKeyName != playListConfiguration.Routes.PresetSmallPicture)
|
||||
{
|
||||
keysToRemove.Add(playList.SmallPictureKeyName);
|
||||
}
|
||||
foreach (var key in keysToRemove)
|
||||
{
|
||||
var publishResult = await TryPublishRemoveFileEventAsync(playList.Id, key, cancellationToken);
|
||||
|
||||
if (playList.MediumPictureKeyName != playListConfiguration.Routes.PresetMediumPicture)
|
||||
if (!publishResult.IsSuccess)
|
||||
{
|
||||
keysToRemove.Add(playList.MediumPictureKeyName);
|
||||
}
|
||||
|
||||
if (playList.LargePictureKeyName != playListConfiguration.Routes.PresetLargePicture)
|
||||
{
|
||||
keysToRemove.Add(playList.LargePictureKeyName);
|
||||
}
|
||||
|
||||
foreach (var key in keysToRemove)
|
||||
{
|
||||
var publishResult = await TryPublishRemoveFileEventAsync(playList.Id, key, cancellationToken);
|
||||
|
||||
if (!publishResult.IsSuccess)
|
||||
{
|
||||
return publishResult;
|
||||
}
|
||||
return publishResult;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -47,68 +47,46 @@ namespace Musify.Application.PlayLists.Commands.UpdatePlayList
|
||||
return Result.Success(PlayListResponse.FromEntity(playList));
|
||||
}
|
||||
|
||||
if (!string.IsNullOrWhiteSpace(playList.OriginalPictureKeyName))
|
||||
if (playList.SmallPictureKeyName != playListConfiguration.Routes.PresetSmallPicture)
|
||||
{
|
||||
var originalImageStorageKey = Path.Join(playListConfiguration.Routes.OriginalPictures, playList.OriginalPictureKeyName);
|
||||
|
||||
try
|
||||
{
|
||||
await eventBus.PublishAsync(new RemoveFileEvent(
|
||||
storageConfiguration.BucketName,
|
||||
originalImageStorageKey), cancellationToken);
|
||||
storageConfiguration.BucketName,
|
||||
playList.SmallPictureKeyName), cancellationToken);
|
||||
}
|
||||
catch (Exception exception)
|
||||
{
|
||||
logger.LogError(exception, "Failed to publish remove file event for original picture of PlayList id={PlayListId}.", playList.Id);
|
||||
logger.LogError(exception, "Failed to publish remove file event for small picture of PlayList id={PlayListId}.", playList.Id);
|
||||
return Result.Error($"Failed to publish remove file event for PlayList with id {playList.Id}.");
|
||||
}
|
||||
|
||||
logger.LogInformation("Published event to remove old picture with key name {KeyName} from bucket {BucketName}",
|
||||
originalImageStorageKey, storageConfiguration.BucketName);
|
||||
|
||||
if (playList.SmallPictureKeyName != playListConfiguration.Routes.PresetSmallPicture)
|
||||
}
|
||||
if (playList.MediumPictureKeyName != playListConfiguration.Routes.PresetMediumPicture)
|
||||
{
|
||||
try
|
||||
{
|
||||
try
|
||||
{
|
||||
await eventBus.PublishAsync(new RemoveFileEvent(
|
||||
storageConfiguration.BucketName,
|
||||
playList.SmallPictureKeyName), cancellationToken);
|
||||
}
|
||||
catch (Exception exception)
|
||||
{
|
||||
logger.LogError(exception, "Failed to publish remove file event for small picture of PlayList id={PlayListId}.", playList.Id);
|
||||
return Result.Error($"Failed to publish remove file event for PlayList with id {playList.Id}.");
|
||||
}
|
||||
await eventBus.PublishAsync(new RemoveFileEvent(
|
||||
storageConfiguration.BucketName,
|
||||
playList.MediumPictureKeyName), cancellationToken);
|
||||
}
|
||||
|
||||
if (playList.MediumPictureKeyName != playListConfiguration.Routes.PresetMediumPicture)
|
||||
catch (Exception exception)
|
||||
{
|
||||
try
|
||||
{
|
||||
await eventBus.PublishAsync(new RemoveFileEvent(
|
||||
storageConfiguration.BucketName,
|
||||
playList.MediumPictureKeyName), cancellationToken);
|
||||
}
|
||||
catch (Exception exception)
|
||||
{
|
||||
logger.LogError(exception, "Failed to publish remove file event for medium picture of PlayList id={PlayListId}.", playList.Id);
|
||||
return Result.Error($"Failed to publish remove file event for PlayList with id {playList.Id}.");
|
||||
}
|
||||
logger.LogError(exception, "Failed to publish remove file event for medium picture of PlayList id={PlayListId}.", playList.Id);
|
||||
return Result.Error($"Failed to publish remove file event for PlayList with id {playList.Id}.");
|
||||
}
|
||||
|
||||
if (playList.LargePictureKeyName != playListConfiguration.Routes.PresetLargePicture)
|
||||
}
|
||||
if (playList.LargePictureKeyName != playListConfiguration.Routes.PresetLargePicture)
|
||||
{
|
||||
try
|
||||
{
|
||||
try
|
||||
{
|
||||
await eventBus.PublishAsync(new RemoveFileEvent(
|
||||
storageConfiguration.BucketName,
|
||||
playList.LargePictureKeyName), cancellationToken);
|
||||
}
|
||||
catch (Exception exception)
|
||||
{
|
||||
logger.LogError(exception, "Failed to publish remove file event for large picture of PlayList id={PlayListId}.", playList.Id);
|
||||
return Result.Error($"Failed to publish remove file event for PlayList with id {playList.Id}.");
|
||||
}
|
||||
await eventBus.PublishAsync(new RemoveFileEvent(
|
||||
storageConfiguration.BucketName,
|
||||
playList.LargePictureKeyName), cancellationToken);
|
||||
}
|
||||
catch (Exception exception)
|
||||
{
|
||||
logger.LogError(exception, "Failed to publish remove file event for large picture of PlayList id={PlayListId}.", playList.Id);
|
||||
return Result.Error($"Failed to publish remove file event for PlayList with id {playList.Id}.");
|
||||
}
|
||||
}
|
||||
|
||||
@@ -130,8 +108,6 @@ namespace Musify.Application.PlayLists.Commands.UpdatePlayList
|
||||
return Result.Error($"Failed to upload picture for PlayList with Id {playList.Id} to storage.");
|
||||
}
|
||||
|
||||
playList.OriginalPictureKeyName = pictureName;
|
||||
|
||||
var smallPictureKeyName = Path.Combine(
|
||||
playListConfiguration.Routes.SmallPictures,
|
||||
$"{playList.Id}.{pictureHandler.FileExtension}");
|
||||
|
||||
@@ -0,0 +1,14 @@
|
||||
using Ardalis.Result;
|
||||
using DispatchR.Abstractions.Send;
|
||||
using Musify.Application.Contracts.Application;
|
||||
using Musify.Application.Tracks.Contracts;
|
||||
|
||||
namespace Musify.Application.Tracks.Commands.CreateTrack
|
||||
{
|
||||
public record CreateTrackCommand(
|
||||
Guid UserId,
|
||||
string Title,
|
||||
IFileData Picture,
|
||||
IFileData Audio)
|
||||
: IRequest<CreateTrackCommand, Task<Result<TrackResponse> >>;
|
||||
}
|
||||
@@ -0,0 +1,87 @@
|
||||
using Ardalis.Result;
|
||||
using DispatchR.Abstractions.Send;
|
||||
using Microsoft.Extensions.Logging;
|
||||
using Musify.Application.Configuration;
|
||||
using Musify.Application.Contracts.Infrastructure;
|
||||
using Musify.Application.Events;
|
||||
using Musify.Application.Tracks.Contracts;
|
||||
using Musify.Domain.Entities;
|
||||
|
||||
namespace Musify.Application.Tracks.Commands.CreateTrack
|
||||
{
|
||||
public class CreateTrackCommandHandler(IDatabase database, IPictureHandler pictureHandler, IEventBus eventBus, IStorageHandler storageHandler,
|
||||
ILogger<CreateTrackCommandHandler> logger, ApplicationStorageConfiguration storageConfiguration, TrackConfiguration trackConfiguration)
|
||||
: IRequestHandler<CreateTrackCommand, Task<Result<TrackResponse> >>
|
||||
{
|
||||
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");
|
||||
}
|
||||
|
||||
var originalPictureKeyName = $"{Guid.NewGuid() }.{request.Picture.FileType}";
|
||||
var track = new Track
|
||||
{
|
||||
Title = request.Title,
|
||||
NormalizedTitle = request.Title.Trim().ToUpperInvariant(),
|
||||
SmallPictureKeyName = trackConfiguration.Routes.PresetSmallPicture,
|
||||
MediumPictureKeyName = trackConfiguration.Routes.PresetMediumPicture,
|
||||
LargePictureKeyName = trackConfiguration.Routes.PresetLargePicture,
|
||||
};
|
||||
|
||||
var pictureUploadResult = await storageHandler.UploadFileAsync(
|
||||
request.Picture.FileStream,
|
||||
request.Picture.ContentType,
|
||||
storageConfiguration.BucketName,
|
||||
originalPictureKeyName,
|
||||
cancellationToken);
|
||||
|
||||
if (!pictureUploadResult.IsSuccess)
|
||||
{
|
||||
logger.LogError("Failed to upload picture for track {TrackTitle}", request.Title);
|
||||
return Result.Error($"Failed to upload picture for track {request.Title}");
|
||||
}
|
||||
|
||||
var smallPictureKeyName = Path.Combine(
|
||||
trackConfiguration.Routes.SmallPictures,
|
||||
$"{track.Id}.{pictureHandler.FileExtension}");
|
||||
var mediumPictureKeyName = Path.Combine(
|
||||
trackConfiguration.Routes.MediumPictures,
|
||||
$"{track.Id}.{pictureHandler.FileExtension}");
|
||||
var largePictureKeyName = Path.Combine(
|
||||
trackConfiguration.Routes.LargePictures,
|
||||
$"{track.Id}.{pictureHandler.FileExtension}");
|
||||
|
||||
try
|
||||
{
|
||||
await eventBus.PublishAsync(new UpdatePlayListPictureEvent(
|
||||
track.Id,
|
||||
storageConfiguration.BucketName,
|
||||
originalPictureKeyName,
|
||||
smallPictureKeyName,
|
||||
trackConfiguration.PicturesSizes.SmallPictureWidth,
|
||||
trackConfiguration.PicturesSizes.SmallPictureHeight,
|
||||
mediumPictureKeyName,
|
||||
trackConfiguration.PicturesSizes.MediumPictureWidth,
|
||||
trackConfiguration.PicturesSizes.MediumPictureHeight,
|
||||
largePictureKeyName,
|
||||
trackConfiguration.PicturesSizes.LargePictureWidth,
|
||||
trackConfiguration.PicturesSizes.LargePictureHeight), cancellationToken);
|
||||
}
|
||||
catch (Exception exception)
|
||||
{
|
||||
logger.LogError(exception, "Failed to publish resize picture event for Track with id={TrackId}.", track.Id);
|
||||
return Result.Error($"Failed to publish resize picture event for Track with id {track.Id}.");
|
||||
}
|
||||
|
||||
await database.Tracks.AddAsync(track, cancellationToken);
|
||||
await database.SaveChangesAsync(cancellationToken);
|
||||
|
||||
return Result.Success();
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,20 @@
|
||||
using Musify.Domain.Entities;
|
||||
|
||||
namespace Musify.Application.Tracks.Contracts
|
||||
{
|
||||
public record TrackResponse(
|
||||
Guid Id,
|
||||
string Title,
|
||||
DateTime CreatedDate,
|
||||
DateTime UpdatedDate)
|
||||
{
|
||||
public static TrackResponse FromEntity(Track track)
|
||||
{
|
||||
return new TrackResponse(
|
||||
track.Id,
|
||||
track.Title,
|
||||
track.CreatedDate,
|
||||
track.UpdatedDate);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -25,9 +25,6 @@ namespace Musify.Domain.Entities
|
||||
[MaxLength(254)]
|
||||
public required string Description { get; set; } = "No description was provided.";
|
||||
|
||||
[MaxLength(64)]
|
||||
public string? OriginalPictureKeyName { get; set; }
|
||||
|
||||
[Required]
|
||||
[MaxLength(64)]
|
||||
public required string SmallPictureKeyName { get; set; }
|
||||
|
||||
@@ -18,7 +18,15 @@ namespace Musify.Domain.Entities
|
||||
|
||||
[Required]
|
||||
[MaxLength(64)]
|
||||
public required string ImageName { get; set; }
|
||||
public required string SmallPictureKeyName { get; set; }
|
||||
|
||||
[Required]
|
||||
[MaxLength(64)]
|
||||
public required string MediumPictureKeyName { get; set; }
|
||||
|
||||
[Required]
|
||||
[MaxLength(64)]
|
||||
public required string LargePictureKeyName { get; set; }
|
||||
|
||||
[Required]
|
||||
public DateTime CreatedDate { get; set; } = DateTime.UtcNow;
|
||||
@@ -26,7 +34,7 @@ namespace Musify.Domain.Entities
|
||||
[Required]
|
||||
public DateTime UpdatedDate { get; set; } = DateTime.UtcNow;
|
||||
|
||||
public ICollection<User> Users { get; set; } = new List<User>();
|
||||
public ICollection<UserHasTrack> UserTracks { get; set; } = new List<UserHasTrack>();
|
||||
|
||||
public ICollection<PlayListHasTrack> PlayListTracks { get; set; } = new List<PlayListHasTrack>();
|
||||
}
|
||||
|
||||
@@ -15,7 +15,7 @@ namespace Musify.Domain.Entities
|
||||
[Required]
|
||||
public DateTime UpdatedAt { get; set; } = DateTime.Now;
|
||||
|
||||
public ICollection<Track> Tracks { get; set; } = new List<Track>();
|
||||
public ICollection<UserHasTrack> UserTracks { get; set; } = new List<UserHasTrack>();
|
||||
|
||||
public ICollection<PlayList> PlayLists { get; set; } = new List<PlayList>();
|
||||
}
|
||||
|
||||
@@ -0,0 +1,24 @@
|
||||
using System.ComponentModel.DataAnnotations;
|
||||
using System.ComponentModel.DataAnnotations.Schema;
|
||||
|
||||
namespace Musify.Domain.Entities
|
||||
{
|
||||
#pragma warning disable CS8618
|
||||
[Table("UserHasTrack")]
|
||||
public class UserHasTrack
|
||||
{
|
||||
public Guid Id { get; set; } = Guid.NewGuid();
|
||||
|
||||
[Required]
|
||||
public required Guid UserId { get; set; }
|
||||
|
||||
[Required]
|
||||
public required Guid TrackId { get; set; }
|
||||
|
||||
[ForeignKey(nameof(UserId)) ]
|
||||
public User User { get; set; }
|
||||
|
||||
[ForeignKey(nameof(TrackId)) ]
|
||||
public Track Track { get; set; }
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,8 @@
|
||||
namespace Musify.Infrastructure.Messaging.Activities.Arguments
|
||||
{
|
||||
public record UpdateTrackPictureArguments(
|
||||
Guid TrackId,
|
||||
string SmallPictureKeyName,
|
||||
string MediumPictureKeyName,
|
||||
string LargePictureKeyName);
|
||||
}
|
||||
@@ -0,0 +1,53 @@
|
||||
using MassTransit;
|
||||
using Musify.Application.Contracts.Infrastructure;
|
||||
using Musify.Domain.Entities;
|
||||
using Musify.Infrastructure.Messaging.Activities.Arguments;
|
||||
|
||||
namespace Musify.Infrastructure.Messaging.Activities
|
||||
{
|
||||
public class UpdateTrackPictureActivity(IDatabase database, IProcessTrackingStore processTrackingStore)
|
||||
: IExecuteActivity<UpdateTrackPictureArguments>
|
||||
{
|
||||
public const string ExecuteEndpointName = "Update-Track-Picture";
|
||||
|
||||
public async Task<ExecutionResult> Execute(ExecuteContext<UpdateTrackPictureArguments> executeContext)
|
||||
{
|
||||
var processId = await processTrackingStore.GetOrCreateProcessAsync(
|
||||
"RoutingSlip",
|
||||
executeContext.CorrelationId ?? executeContext.TrackingNumber,
|
||||
executeContext.ConversationId,
|
||||
executeContext.MessageId,
|
||||
executeContext.CancellationToken);
|
||||
|
||||
var stepId = await processTrackingStore.StartStepAsync(
|
||||
processId,
|
||||
nameof(UpdateTrackPictureActivity),
|
||||
ProcessStepComponentType.Activity,
|
||||
0,
|
||||
executeContext.CancellationToken);
|
||||
|
||||
try
|
||||
{
|
||||
var track = await database.Tracks.FindAsync(executeContext.Arguments.TrackId, executeContext.CancellationToken);
|
||||
|
||||
if (track is null)
|
||||
throw new Exception($"Track with id {executeContext.Arguments.TrackId} not found.");
|
||||
|
||||
track.SmallPictureKeyName = executeContext.Arguments.SmallPictureKeyName;
|
||||
track.MediumPictureKeyName = executeContext.Arguments.MediumPictureKeyName;
|
||||
track.LargePictureKeyName = executeContext.Arguments.LargePictureKeyName;
|
||||
|
||||
database.Tracks.Update(track);
|
||||
await database.SaveChangesAsync(executeContext.CancellationToken);
|
||||
|
||||
await processTrackingStore.CompleteStepAsync(processId, stepId, executeContext.CancellationToken);
|
||||
return executeContext.Completed();
|
||||
}
|
||||
catch (Exception exception)
|
||||
{
|
||||
await processTrackingStore.FailStepAsync(processId, stepId, exception.Message, executeContext.CancellationToken);
|
||||
throw;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -44,7 +44,10 @@ namespace Musify.Infrastructure.Messaging.Consumers
|
||||
routingSlipBuilder.AddActivity(
|
||||
"DeleteOriginal",
|
||||
BuildExecuteUri(RemoveFilesActivity.ExecuteEndpointName),
|
||||
new RemoveFileArguments(consumeContext.Message.BucketName, consumeContext.Message.OriginalPictureKeyName));
|
||||
new RemoveFileArguments(
|
||||
consumeContext.Message.BucketName,
|
||||
consumeContext.Message.OriginalPictureKeyName)
|
||||
);
|
||||
|
||||
routingSlipBuilder.AddActivity(
|
||||
"UpdatePlayListPicture",
|
||||
|
||||
@@ -0,0 +1,88 @@
|
||||
using MassTransit;
|
||||
using Musify.Application.Events;
|
||||
using Musify.Infrastructure.Messaging.Activities;
|
||||
using Musify.Infrastructure.Messaging.Activities.Arguments;
|
||||
|
||||
namespace Musify.Infrastructure.Messaging.Consumers
|
||||
{
|
||||
public class UpdateTrackPictureConsumer(IBus bus)
|
||||
: IConsumer<UpdateTrackPictureEvent>
|
||||
{
|
||||
public const string QueueName = "update-track-picture";
|
||||
|
||||
public async Task Consume(ConsumeContext<UpdateTrackPictureEvent> consumeContext)
|
||||
{
|
||||
var routingSlipBuilder = new RoutingSlipBuilder(NewId.NextGuid());
|
||||
|
||||
AddResizeActivity(
|
||||
routingSlipBuilder,
|
||||
"ResizeSmall",
|
||||
consumeContext.Message.SmallPictureKeyName,
|
||||
consumeContext.Message.SmallPictureWidth,
|
||||
consumeContext.Message.SmallPictureHeight,
|
||||
consumeContext.Message.OriginalPictureKeyName,
|
||||
consumeContext.Message.BucketName);
|
||||
|
||||
AddResizeActivity(
|
||||
routingSlipBuilder,
|
||||
"ResizeMedium",
|
||||
consumeContext.Message.SmallPictureKeyName,
|
||||
consumeContext.Message.MediumPictureWidth,
|
||||
consumeContext.Message.MediumPictureHeight,
|
||||
consumeContext.Message.OriginalPictureKeyName,
|
||||
consumeContext.Message.BucketName);
|
||||
|
||||
AddResizeActivity(
|
||||
routingSlipBuilder,
|
||||
"ResizeMedium",
|
||||
consumeContext.Message.SmallPictureKeyName,
|
||||
consumeContext.Message.LargePictureWidth,
|
||||
consumeContext.Message.LargePictureHeight,
|
||||
consumeContext.Message.OriginalPictureKeyName,
|
||||
consumeContext.Message.BucketName);
|
||||
|
||||
routingSlipBuilder.AddActivity(
|
||||
"DeleteOriginal",
|
||||
BuildExecuteUri(RemoveFilesActivity.ExecuteEndpointName),
|
||||
new RemoveFileArguments(
|
||||
consumeContext.Message.BucketName,
|
||||
consumeContext.Message.OriginalPictureKeyName)
|
||||
);
|
||||
|
||||
routingSlipBuilder.AddActivity(
|
||||
"UpdateTrackPictureActivity",
|
||||
BuildExecuteUri(UpdateTrackPictureActivity.ExecuteEndpointName),
|
||||
new UpdateTrackPictureArguments(
|
||||
consumeContext.Message.TrackId,
|
||||
consumeContext.Message.SmallPictureKeyName,
|
||||
consumeContext.Message.MediumPictureKeyName,
|
||||
consumeContext.Message.LargePictureKeyName));
|
||||
|
||||
var routingSlip = routingSlipBuilder.Build();
|
||||
await bus.Execute(routingSlip);
|
||||
}
|
||||
|
||||
void AddResizeActivity(
|
||||
RoutingSlipBuilder routingSlipBuilder,
|
||||
string activityName,
|
||||
string destinationKeyName,
|
||||
int width,
|
||||
int height,
|
||||
string originalPictureKeyName,
|
||||
string bucketName)
|
||||
{
|
||||
routingSlipBuilder.AddActivity(
|
||||
activityName,
|
||||
BuildExecuteUri(ResizePictureActivity.ExecuteEndpointName),
|
||||
new ResizePictureArgument(
|
||||
bucketName,
|
||||
originalPictureKeyName,
|
||||
bucketName,
|
||||
destinationKeyName,
|
||||
width,
|
||||
height));
|
||||
}
|
||||
|
||||
static Uri BuildExecuteUri(string endpointName) => new($"queue:{endpointName}_execute");
|
||||
}
|
||||
}
|
||||
@@ -19,6 +19,8 @@ namespace Musify.Infrastructure.Persistence
|
||||
|
||||
public DbSet<PlayList> PlayLists => Set<PlayList>();
|
||||
|
||||
public DbSet<UserHasTrack> UserHasTracks => Set<UserHasTrack>();
|
||||
|
||||
public DbSet<PlayListHasTrack> PlayListHasTracks => Set<PlayListHasTrack>();
|
||||
|
||||
public DbSet<Upload> Uploads => Set<Upload>();
|
||||
|
||||
Reference in New Issue
Block a user