Adds subscriptions

This commit is contained in:
Jonathan Bourdon
2024-08-04 03:27:21 -04:00
parent 303619cc18
commit 68ef947e26
12 changed files with 413 additions and 147 deletions

View File

@@ -0,0 +1,42 @@
using FastEndpoints;
using Hutopy.Web.Common;
using Hutopy.Web.Features.Contents.Data;
namespace Hutopy.Web.Features.Contents.Handlers;
public sealed class UnsubscribeFromCreatorRequest
{
public Guid CreatorId { get; set; }
}
public class UnsubscribeFromCreatorHandler(
ContentDbContext context)
: Endpoint<UnsubscribeFromCreatorRequest>
{
public override void Configure()
{
Post("/api/creators/{CreatorId}/unsubscribe");
Options((o => o.WithTags("Creators")));
Description(x => x.Accepts<string>("*/*"));
}
public override async Task HandleAsync(
UnsubscribeFromCreatorRequest req,
CancellationToken ct)
{
var subscription = new Subscription { CreatorId = req.CreatorId, CreatedBy = HttpContext.User.GetUserId() };
context.Subscriptions.Attach(subscription);
context.Subscriptions.Remove(subscription);
try
{
await context.SaveChangesAsync(ct);
await SendOkAsync(ct);
}
catch (Exception)
{
await SendNotFoundAsync(ct);
}
}
}