|
|
|
@ -1,4 +1,6 @@
@@ -1,4 +1,6 @@
|
|
|
|
|
using System.Threading; |
|
|
|
|
using System.Collections.Generic; |
|
|
|
|
using System.Linq; |
|
|
|
|
using System.Threading; |
|
|
|
|
using System.Threading.Tasks; |
|
|
|
|
using ErsatzTV.Core; |
|
|
|
|
using ErsatzTV.Core.Domain; |
|
|
|
@ -6,6 +8,7 @@ using ErsatzTV.Core.Interfaces.Repositories;
@@ -6,6 +8,7 @@ using ErsatzTV.Core.Interfaces.Repositories;
|
|
|
|
|
using LanguageExt; |
|
|
|
|
using MediatR; |
|
|
|
|
using static ErsatzTV.Application.ProgramSchedules.Mapper; |
|
|
|
|
using static LanguageExt.Prelude; |
|
|
|
|
|
|
|
|
|
namespace ErsatzTV.Application.ProgramSchedules.Commands |
|
|
|
|
{ |
|
|
|
@ -22,22 +25,33 @@ namespace ErsatzTV.Application.ProgramSchedules.Commands
@@ -22,22 +25,33 @@ namespace ErsatzTV.Application.ProgramSchedules.Commands
|
|
|
|
|
CreateProgramSchedule request, |
|
|
|
|
CancellationToken cancellationToken) => |
|
|
|
|
Validate(request) |
|
|
|
|
.Map(PersistProgramSchedule) |
|
|
|
|
.ToEitherAsync(); |
|
|
|
|
.MapT(PersistProgramSchedule) |
|
|
|
|
.Bind(v => v.ToEitherAsync()); |
|
|
|
|
|
|
|
|
|
private Task<ProgramScheduleViewModel> PersistProgramSchedule(ProgramSchedule c) => |
|
|
|
|
_programScheduleRepository.Add(c).Map(ProjectToViewModel); |
|
|
|
|
|
|
|
|
|
private Validation<BaseError, ProgramSchedule> Validate(CreateProgramSchedule request) => |
|
|
|
|
private Task<Validation<BaseError, ProgramSchedule>> Validate(CreateProgramSchedule request) => |
|
|
|
|
ValidateName(request) |
|
|
|
|
.Map( |
|
|
|
|
.MapT( |
|
|
|
|
name => new ProgramSchedule |
|
|
|
|
{ |
|
|
|
|
Name = name, MediaCollectionPlaybackOrder = request.MediaCollectionPlaybackOrder |
|
|
|
|
}); |
|
|
|
|
|
|
|
|
|
private Validation<BaseError, string> ValidateName(CreateProgramSchedule createProgramSchedule) => |
|
|
|
|
createProgramSchedule.NotEmpty(c => c.Name) |
|
|
|
|
private async Task<Validation<BaseError, string>> ValidateName(CreateProgramSchedule createProgramSchedule) |
|
|
|
|
{ |
|
|
|
|
List<string> allNames = await _programScheduleRepository.GetAll() |
|
|
|
|
.Map(list => list.Map(c => c.Name).ToList()); |
|
|
|
|
|
|
|
|
|
Validation<BaseError, string> result1 = createProgramSchedule.NotEmpty(c => c.Name) |
|
|
|
|
.Bind(_ => createProgramSchedule.NotLongerThan(50)(c => c.Name)); |
|
|
|
|
|
|
|
|
|
var result2 = Optional(createProgramSchedule.Name) |
|
|
|
|
.Filter(name => !allNames.Contains(name)) |
|
|
|
|
.ToValidation<BaseError>("Schedule name must be unique"); |
|
|
|
|
|
|
|
|
|
return (result1, result2).Apply((_, _) => createProgramSchedule.Name); |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|