mirror of https://github.com/ErsatzTV/ErsatzTV.git
7 changed files with 250 additions and 22 deletions
@ -0,0 +1,15 @@ |
|||||||
|
using System.Collections.Generic; |
||||||
|
using ErsatzTV.Core; |
||||||
|
using LanguageExt; |
||||||
|
|
||||||
|
namespace ErsatzTV.Application.MediaCollections.Commands |
||||||
|
{ |
||||||
|
public record RemoveItemsFromSimpleMediaCollection |
||||||
|
(int MediaCollectionId) : MediatR.IRequest<Either<BaseError, Unit>> |
||||||
|
{ |
||||||
|
public List<int> MovieIds { get; set; } = new(); |
||||||
|
public List<int> TelevisionShowIds { get; set; } = new(); |
||||||
|
public List<int> TelevisionSeasonIds { get; set; } = new(); |
||||||
|
public List<int> TelevisionEpisodeIds { get; set; } = new(); |
||||||
|
} |
||||||
|
} |
||||||
@ -0,0 +1,74 @@ |
|||||||
|
using System.Linq; |
||||||
|
using System.Threading; |
||||||
|
using System.Threading.Tasks; |
||||||
|
using ErsatzTV.Core; |
||||||
|
using ErsatzTV.Core.Domain; |
||||||
|
using ErsatzTV.Core.Interfaces.Repositories; |
||||||
|
using LanguageExt; |
||||||
|
|
||||||
|
namespace ErsatzTV.Application.MediaCollections.Commands |
||||||
|
{ |
||||||
|
public class |
||||||
|
RemoveItemsFromSimpleMediaCollectionHandler : MediatR.IRequestHandler< |
||||||
|
RemoveItemsFromSimpleMediaCollection, |
||||||
|
Either<BaseError, Unit>> |
||||||
|
{ |
||||||
|
private readonly IMediaCollectionRepository _mediaCollectionRepository; |
||||||
|
|
||||||
|
public RemoveItemsFromSimpleMediaCollectionHandler( |
||||||
|
IMediaCollectionRepository mediaCollectionRepository) => |
||||||
|
_mediaCollectionRepository = mediaCollectionRepository; |
||||||
|
|
||||||
|
public Task<Either<BaseError, Unit>> Handle( |
||||||
|
RemoveItemsFromSimpleMediaCollection request, |
||||||
|
CancellationToken cancellationToken) => |
||||||
|
Validate(request) |
||||||
|
.MapT(collection => ApplyAddTelevisionEpisodeRequest(request, collection)) |
||||||
|
.Bind(v => v.ToEitherAsync()); |
||||||
|
|
||||||
|
private Task<Unit> ApplyAddTelevisionEpisodeRequest( |
||||||
|
RemoveItemsFromSimpleMediaCollection request, |
||||||
|
SimpleMediaCollection collection) |
||||||
|
{ |
||||||
|
var moviesToRemove = collection.Movies |
||||||
|
.Filter(m => request.MovieIds.Contains(m.Id)) |
||||||
|
.ToList(); |
||||||
|
|
||||||
|
moviesToRemove.ForEach(m => collection.Movies.Remove(m)); |
||||||
|
|
||||||
|
var showsToRemove = collection.TelevisionShows |
||||||
|
.Filter(s => request.TelevisionShowIds.Contains(s.Id)) |
||||||
|
.ToList(); |
||||||
|
|
||||||
|
showsToRemove.ForEach(s => collection.TelevisionShows.Remove(s)); |
||||||
|
|
||||||
|
var seasonsToRemove = collection.TelevisionSeasons |
||||||
|
.Filter(s => request.TelevisionSeasonIds.Contains(s.Id)) |
||||||
|
.ToList(); |
||||||
|
|
||||||
|
seasonsToRemove.ForEach(s => collection.TelevisionSeasons.Remove(s)); |
||||||
|
|
||||||
|
var episodesToRemove = collection.TelevisionEpisodes |
||||||
|
.Filter(e => request.TelevisionEpisodeIds.Contains(e.Id)) |
||||||
|
.ToList(); |
||||||
|
|
||||||
|
episodesToRemove.ForEach(e => collection.TelevisionEpisodes.Remove(e)); |
||||||
|
|
||||||
|
if (moviesToRemove.Any() || showsToRemove.Any() || seasonsToRemove.Any() || episodesToRemove.Any()) |
||||||
|
{ |
||||||
|
return _mediaCollectionRepository.Update(collection).ToUnit(); |
||||||
|
} |
||||||
|
|
||||||
|
return Task.FromResult(Unit.Default); |
||||||
|
} |
||||||
|
|
||||||
|
private Task<Validation<BaseError, SimpleMediaCollection>> Validate( |
||||||
|
RemoveItemsFromSimpleMediaCollection request) => |
||||||
|
SimpleMediaCollectionMustExist(request); |
||||||
|
|
||||||
|
private Task<Validation<BaseError, SimpleMediaCollection>> SimpleMediaCollectionMustExist( |
||||||
|
RemoveItemsFromSimpleMediaCollection updateSimpleMediaCollection) => |
||||||
|
_mediaCollectionRepository.GetSimpleMediaCollectionWithItems(updateSimpleMediaCollection.MediaCollectionId) |
||||||
|
.Map(v => v.ToValidation<BaseError>("SimpleMediaCollection does not exist.")); |
||||||
|
} |
||||||
|
} |
||||||
@ -0,0 +1,43 @@ |
|||||||
|
@inject IMediator Mediator |
||||||
|
|
||||||
|
<MudDialog> |
||||||
|
<DialogContent> |
||||||
|
<MudContainer Class="mb-6"> |
||||||
|
<MudHighlighter Class="mud-primary-text" |
||||||
|
Style="background-color: transparent; font-weight: bold" |
||||||
|
Text="@FormatText()" |
||||||
|
HighlightedText="@EntityName"/> |
||||||
|
</MudContainer> |
||||||
|
</DialogContent> |
||||||
|
<DialogActions> |
||||||
|
<MudButton OnClick="Cancel">Cancel</MudButton> |
||||||
|
<MudButton Color="Color.Error" Variant="Variant.Filled" OnClick="Submit"> |
||||||
|
Remove From Collection |
||||||
|
</MudButton> |
||||||
|
</DialogActions> |
||||||
|
</MudDialog> |
||||||
|
|
||||||
|
@code { |
||||||
|
|
||||||
|
[CascadingParameter] |
||||||
|
MudDialogInstance MudDialog { get; set; } |
||||||
|
|
||||||
|
[Parameter] |
||||||
|
public string EntityType { get; set; } |
||||||
|
|
||||||
|
[Parameter] |
||||||
|
public string EntityName { get; set; } |
||||||
|
|
||||||
|
[Parameter] |
||||||
|
public string DetailText { get; set; } |
||||||
|
|
||||||
|
[Parameter] |
||||||
|
public string DetailHighlight { get; set; } |
||||||
|
|
||||||
|
private string FormatText() => $"Are you sure you want to remove the {EntityType} {EntityName} from this collection?"; |
||||||
|
|
||||||
|
private void Submit() => MudDialog.Close(DialogResult.Ok(true)); |
||||||
|
|
||||||
|
private void Cancel() => MudDialog.Cancel(); |
||||||
|
|
||||||
|
} |
||||||
Loading…
Reference in new issue