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.
42 lines
1.4 KiB
42 lines
1.4 KiB
using System.Linq; |
|
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 Unit = LanguageExt.Unit; |
|
|
|
namespace ErsatzTV.Application.Playouts.Commands |
|
{ |
|
public class DeletePlayoutHandler : IRequestHandler<DeletePlayout, Either<BaseError, Unit>> |
|
{ |
|
private readonly IDbContextFactory<TvContext> _dbContextFactory; |
|
|
|
public DeletePlayoutHandler(IDbContextFactory<TvContext> dbContextFactory) => |
|
_dbContextFactory = dbContextFactory; |
|
|
|
public async Task<Either<BaseError, Unit>> Handle( |
|
DeletePlayout request, |
|
CancellationToken cancellationToken) |
|
{ |
|
await using TvContext dbContext = _dbContextFactory.CreateDbContext(); |
|
|
|
Option<Playout> maybePlayout = await dbContext.Playouts |
|
.OrderBy(p => p.Id) |
|
.FirstOrDefaultAsync(p => p.Id == request.PlayoutId, cancellationToken); |
|
|
|
foreach (Playout playout in maybePlayout) |
|
{ |
|
dbContext.Playouts.Remove(playout); |
|
await dbContext.SaveChangesAsync(cancellationToken); |
|
} |
|
|
|
return maybePlayout |
|
.Map(_ => Unit.Default) |
|
.ToEither(BaseError.New($"Playout {request.PlayoutId} does not exist.")); |
|
} |
|
} |
|
}
|
|
|