mirror of https://github.com/ErsatzTV/ErsatzTV.git
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.
39 lines
1.6 KiB
39 lines
1.6 KiB
using ErsatzTV.Core; |
|
using ErsatzTV.Core.Domain.Scheduling; |
|
using ErsatzTV.Infrastructure.Data; |
|
using Microsoft.EntityFrameworkCore; |
|
|
|
namespace ErsatzTV.Application.Scheduling; |
|
|
|
public class CreateDecoTemplateHandler(IDbContextFactory<TvContext> dbContextFactory) |
|
: IRequestHandler<CreateDecoTemplate, Either<BaseError, DecoTemplateViewModel>> |
|
{ |
|
public async Task<Either<BaseError, DecoTemplateViewModel>> Handle( |
|
CreateDecoTemplate request, |
|
CancellationToken cancellationToken) |
|
{ |
|
await using TvContext dbContext = await dbContextFactory.CreateDbContextAsync(cancellationToken); |
|
Validation<BaseError, DecoTemplate> validation = await Validate(request); |
|
return await validation.Apply(profile => PersistDecoTemplate(dbContext, profile)); |
|
} |
|
|
|
private static async Task<DecoTemplateViewModel> PersistDecoTemplate(TvContext dbContext, DecoTemplate decoTemplate) |
|
{ |
|
await dbContext.DecoTemplates.AddAsync(decoTemplate); |
|
await dbContext.SaveChangesAsync(); |
|
return Mapper.ProjectToViewModel(decoTemplate); |
|
} |
|
|
|
private static Task<Validation<BaseError, DecoTemplate>> Validate(CreateDecoTemplate request) => |
|
Task.FromResult( |
|
ValidateName(request).Map( |
|
name => new DecoTemplate |
|
{ |
|
DecoTemplateGroupId = request.DecoTemplateGroupId, |
|
Name = name |
|
})); |
|
|
|
private static Validation<BaseError, string> ValidateName(CreateDecoTemplate createDecoTemplate) => |
|
createDecoTemplate.NotEmpty(x => x.Name) |
|
.Bind(_ => createDecoTemplate.NotLongerThan(50)(x => x.Name)); |
|
}
|
|
|