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.
69 lines
2.6 KiB
69 lines
2.6 KiB
using System.Collections.Generic; |
|
using System.Threading; |
|
using System.Threading.Tasks; |
|
using ErsatzTV.Core; |
|
using ErsatzTV.Core.Domain; |
|
using ErsatzTV.Infrastructure.Data; |
|
using LanguageExt; |
|
using MediatR; |
|
using Microsoft.EntityFrameworkCore; |
|
using static ErsatzTV.Application.MediaCollections.Mapper; |
|
using static LanguageExt.Prelude; |
|
|
|
namespace ErsatzTV.Application.MediaCollections.Commands |
|
{ |
|
public class CreateCollectionHandler : |
|
IRequestHandler<CreateCollection, Either<BaseError, MediaCollectionViewModel>> |
|
{ |
|
private readonly IDbContextFactory<TvContext> _dbContextFactory; |
|
|
|
public CreateCollectionHandler(IDbContextFactory<TvContext> dbContextFactory) => |
|
_dbContextFactory = dbContextFactory; |
|
|
|
public async Task<Either<BaseError, MediaCollectionViewModel>> Handle( |
|
CreateCollection request, |
|
CancellationToken cancellationToken) |
|
{ |
|
await using TvContext dbContext = _dbContextFactory.CreateDbContext(); |
|
Validation<BaseError, Collection> validation = await Validate(dbContext, request); |
|
return await validation.Apply(c => PersistCollection(dbContext, c)); |
|
} |
|
|
|
private static async Task<MediaCollectionViewModel> PersistCollection( |
|
TvContext dbContext, |
|
Collection collection) |
|
{ |
|
await dbContext.Collections.AddAsync(collection); |
|
await dbContext.SaveChangesAsync(); |
|
return ProjectToViewModel(collection); |
|
} |
|
|
|
private static Task<Validation<BaseError, Collection>> Validate( |
|
TvContext dbContext, |
|
CreateCollection request) => |
|
ValidateName(dbContext, request).MapT( |
|
name => new Collection |
|
{ |
|
Name = name, |
|
MediaItems = new List<MediaItem>() |
|
}); |
|
|
|
private static async Task<Validation<BaseError, string>> ValidateName( |
|
TvContext dbContext, |
|
CreateCollection createCollection) |
|
{ |
|
List<string> allNames = await dbContext.Collections |
|
.Map(c => c.Name) |
|
.ToListAsync(); |
|
|
|
Validation<BaseError, string> result1 = createCollection.NotEmpty(c => c.Name) |
|
.Bind(_ => createCollection.NotLongerThan(50)(c => c.Name)); |
|
|
|
var result2 = Optional(createCollection.Name) |
|
.Where(name => !allNames.Contains(name)) |
|
.ToValidation<BaseError>("Collection name must be unique"); |
|
|
|
return (result1, result2).Apply((_, _) => createCollection.Name); |
|
} |
|
} |
|
}
|
|
|