Stream custom live channels using your own media
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 

35 lines
1.5 KiB

using ErsatzTV.Core;
using ErsatzTV.Core.Domain;
using ErsatzTV.Infrastructure.Data;
using Microsoft.EntityFrameworkCore;
namespace ErsatzTV.Application.MediaCollections;
public class CreatePlaylistGroupHandler(IDbContextFactory<TvContext> dbContextFactory)
: IRequestHandler<CreatePlaylistGroup, Either<BaseError, PlaylistGroupViewModel>>
{
public async Task<Either<BaseError, PlaylistGroupViewModel>> Handle(
CreatePlaylistGroup request,
CancellationToken cancellationToken)
{
await using TvContext dbContext = await dbContextFactory.CreateDbContextAsync(cancellationToken);
Validation<BaseError, PlaylistGroup> validation = await Validate(request);
return await validation.Apply(profile => PersistPlaylistGroup(dbContext, profile));
}
private static async Task<PlaylistGroupViewModel> PersistPlaylistGroup(
TvContext dbContext,
PlaylistGroup playlistGroup)
{
await dbContext.PlaylistGroups.AddAsync(playlistGroup);
await dbContext.SaveChangesAsync();
return Mapper.ProjectToViewModel(playlistGroup);
}
private static Task<Validation<BaseError, PlaylistGroup>> Validate(CreatePlaylistGroup request) =>
Task.FromResult(ValidateName(request).Map(name => new PlaylistGroup { Name = name, Playlists = [] }));
private static Validation<BaseError, string> ValidateName(CreatePlaylistGroup createPlaylistGroup) =>
createPlaylistGroup.NotEmpty(x => x.Name)
.Bind(_ => createPlaylistGroup.NotLongerThan(50)(x => x.Name));
}