Files
ersatztv/ErsatzTV.Application/ProgramSchedules/Commands/ReplaceProgramScheduleItemsHandler.cs
T
Jason DoveandGitHub 871a031467 rework television media (#26)
* rework television media

* refactor poster saving

* television and movie views are working again

* remove dead code

* use paper styling for all cards

* add show poster, plot to seasons page

* remove missing shows; cleanup interfaces

* fix split show display (same show in different folders/sources)

* add placeholder "add to schedule" button

* no more duplicate television shows, even with the same show split across sources

* stop releasing CLI for now

* use season number as season placeholder

* add television shows to collections

* add television seasons to collections

* add television episodes to collections

* add movies to collections

* remove movies, shows, seasons, episodes from collections

* fix page width and menus

* fix buffer size defaults

* fix chronological episode ordering

* allow deleting media collections

* don't get stuck building a playout with an empty collection

* schedule editing and playouts work again

* minor cleanup

* remove dead code

* fix bugs with viewing movies as they are loading

* add scanner tests; support nested movie folders

* update collections docs

* rearrange order of schedule items

* add show and season to schedule

* delete schedules that use legacy collections, reset all posters

* move cleanup to new migration

* load fallback metadata when nfo fails; don't require metadata in ui

* update readme and screenshots
2021-02-22 00:54:41 +00:00

74 lines
3.1 KiB
C#

using System.Collections.Generic;
using System.Linq;
using System.Threading;
using System.Threading.Channels;
using System.Threading.Tasks;
using ErsatzTV.Application.Playouts.Commands;
using ErsatzTV.Core;
using ErsatzTV.Core.Domain;
using ErsatzTV.Core.Interfaces.Repositories;
using LanguageExt;
using MediatR;
using static ErsatzTV.Application.ProgramSchedules.Mapper;
namespace ErsatzTV.Application.ProgramSchedules.Commands
{
public class ReplaceProgramScheduleItemsHandler : ProgramScheduleItemCommandBase,
IRequestHandler<ReplaceProgramScheduleItems,
Either<BaseError, IEnumerable<ProgramScheduleItemViewModel>>>
{
private readonly ChannelWriter<IBackgroundServiceRequest> _channel;
private readonly IProgramScheduleRepository _programScheduleRepository;
public ReplaceProgramScheduleItemsHandler(
IProgramScheduleRepository programScheduleRepository,
ChannelWriter<IBackgroundServiceRequest> channel)
: base(programScheduleRepository)
{
_programScheduleRepository = programScheduleRepository;
_channel = channel;
}
public Task<Either<BaseError, IEnumerable<ProgramScheduleItemViewModel>>> Handle(
ReplaceProgramScheduleItems request,
CancellationToken cancellationToken) =>
Validate(request)
.MapT(programSchedule => PersistItems(request, programSchedule))
.Bind(v => v.ToEitherAsync());
private async Task<IEnumerable<ProgramScheduleItemViewModel>> PersistItems(
ReplaceProgramScheduleItems request,
ProgramSchedule programSchedule)
{
programSchedule.Items = request.Items.Map(i => BuildItem(programSchedule, i.Index, i)).ToList();
await _programScheduleRepository.Update(programSchedule);
// rebuild any playouts that use this schedule
foreach (Playout playout in programSchedule.Playouts)
{
await _channel.WriteAsync(new BuildPlayout(playout.Id, true));
}
return programSchedule.Items.Map(ProjectToViewModel);
}
private Task<Validation<BaseError, ProgramSchedule>> Validate(ReplaceProgramScheduleItems request) =>
ProgramScheduleMustExist(request.ProgramScheduleId)
.BindT(programSchedule => PlayoutModesMustBeValid(request, programSchedule))
.BindT(programSchedule => CollectionTypesMustBeValid(request, programSchedule));
private Validation<BaseError, ProgramSchedule> PlayoutModesMustBeValid(
ReplaceProgramScheduleItems request,
ProgramSchedule programSchedule) =>
request.Items.Map(item => PlayoutModeMustBeValid(item, programSchedule)).Sequence()
.Map(_ => programSchedule);
private Validation<BaseError, ProgramSchedule> CollectionTypesMustBeValid(
ReplaceProgramScheduleItems request,
ProgramSchedule programSchedule) =>
request.Items.Map(item => CollectionTypeMustBeValid(item, programSchedule)).Sequence()
.Map(_ => programSchedule);
}
}