Adds subscriptions
This commit is contained in:
42
src/Web/Features/Contents/Handlers/UnsubscribeFromCreator.cs
Normal file
42
src/Web/Features/Contents/Handlers/UnsubscribeFromCreator.cs
Normal 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);
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user