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.
33 lines
1.3 KiB
33 lines
1.3 KiB
using System.Threading; |
|
using System.Threading.Tasks; |
|
using ErsatzTV.Core; |
|
using ErsatzTV.Core.Interfaces.Repositories; |
|
using LanguageExt; |
|
using MediatR; |
|
|
|
namespace ErsatzTV.Application.MediaCollections.Commands |
|
{ |
|
public class DeleteCollectionHandler : IRequestHandler<DeleteCollection, Either<BaseError, Task>> |
|
{ |
|
private readonly IMediaCollectionRepository _mediaCollectionRepository; |
|
|
|
public DeleteCollectionHandler(IMediaCollectionRepository mediaCollectionRepository) => |
|
_mediaCollectionRepository = mediaCollectionRepository; |
|
|
|
public async Task<Either<BaseError, Task>> Handle( |
|
DeleteCollection request, |
|
CancellationToken cancellationToken) => |
|
(await CollectionMustExist(request)) |
|
.Map(DoDeletion) |
|
.ToEither<Task>(); |
|
|
|
private Task DoDeletion(int mediaCollectionId) => _mediaCollectionRepository.Delete(mediaCollectionId); |
|
|
|
private async Task<Validation<BaseError, int>> CollectionMustExist( |
|
DeleteCollection deleteMediaCollection) => |
|
(await _mediaCollectionRepository.Get(deleteMediaCollection.CollectionId)) |
|
.ToValidation<BaseError>( |
|
$"Collection {deleteMediaCollection.CollectionId} does not exist.") |
|
.Map(c => c.Id); |
|
} |
|
}
|
|
|