Files
ersatztv/ErsatzTV.Application/Scheduling/Commands/DeleteDecoGroupHandler.cs
T
Jason DoveandGitHub 1c07df5bc3 use cancellation tokens in many places (#2350)
* use cancellation tokens everywhere

* more cancellation tokens
2025-08-27 03:20:35 +00:00

30 lines
1.1 KiB
C#

using ErsatzTV.Core;
using ErsatzTV.Core.Domain.Scheduling;
using ErsatzTV.Infrastructure.Data;
using ErsatzTV.Infrastructure.Extensions;
using Microsoft.EntityFrameworkCore;
namespace ErsatzTV.Application.Scheduling;
public class DeleteDecoGroupHandler(IDbContextFactory<TvContext> dbContextFactory)
: IRequestHandler<DeleteDecoGroup, Option<BaseError>>
{
public async Task<Option<BaseError>> Handle(DeleteDecoGroup request, CancellationToken cancellationToken)
{
await using TvContext dbContext = await dbContextFactory.CreateDbContextAsync(cancellationToken);
Option<DecoGroup> maybeDecoGroup = await dbContext.DecoGroups
.SelectOneAsync(p => p.Id, p => p.Id == request.DecoGroupId, cancellationToken);
foreach (DecoGroup decoGroup in maybeDecoGroup)
{
dbContext.DecoGroups.Remove(decoGroup);
await dbContext.SaveChangesAsync(cancellationToken);
}
return maybeDecoGroup.Match(
_ => Option<BaseError>.None,
() => BaseError.New($"DecoGroup {request.DecoGroupId} does not exist."));
}
}