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

@@ -1,13 +1,14 @@
using FastEndpoints;
using Hutopy.Web.Common;
using Hutopy.Web.Features.Contents.Data;
using Hutopy.Web.Features.Contents.Handlers.Models;
using Microsoft.EntityFrameworkCore;
namespace Hutopy.Web.Features.Contents.Handlers;
public class GetSubscriptionsHandler(
ContentDbContext context)
: EndpointWithoutRequest
: EndpointWithoutRequest<List<SubscriptionModel>>
{
public override void Configure()
{
@@ -20,12 +21,15 @@ public class GetSubscriptionsHandler(
{
var userId = HttpContext.User.GetUserId();
await context
var subscriptions = await context
.Subscriptions
.Where(s => s.CreatedBy == userId)
.Include(s => s.Creator)
.Select(s => new SubscriptionModel(
s.CreatorId,
s.Creator!.Name,
s.Creator.StoredDataUrls.ProfilePictureUrl))
.ToListAsync(cancellationToken: ct);
await SendOkAsync(ct);
await SendOkAsync(subscriptions, ct);
}
}

View File

@@ -0,0 +1,6 @@
namespace Hutopy.Web.Features.Contents.Handlers.Models;
public record SubscriptionModel(
Guid CreatorId,
string CreatorName,
string? CreatorPortraitUrl);

View File

@@ -0,0 +1,54 @@
using FastEndpoints;
using Hutopy.Web.Common;
using Hutopy.Web.Features.Contents.Data;
using Hutopy.Web.Features.Contents.Handlers.Models;
using Microsoft.EntityFrameworkCore;
namespace Hutopy.Web.Features.Contents.Handlers;
public sealed class SubscribeToCreatorRequest
{
public Guid CreatorId { get; set; }
}
public sealed class SubscribeToCreatorHandler(
ContentDbContext context)
: Endpoint<SubscribeToCreatorRequest, SubscriptionModel>
{
public override void Configure()
{
Post("/api/creators/{CreatorId}/subscribe");
Options((o => o.WithTags("Creators")));
Description(x => x.Accepts<string>("*/*"));
}
public override async Task HandleAsync(
SubscribeToCreatorRequest req,
CancellationToken ct)
{
await context.Subscriptions.AddAsync(
new() { CreatedBy = HttpContext.User.GetUserId(), CreatorId = req.CreatorId },
ct);
await context.SaveChangesAsync(ct);
var creator = await context
.Creators
.Where(c => c.Id == req.CreatorId)
.Select(c => new SubscriptionModel(
req.CreatorId,
c.Name,
c.StoredDataUrls.ProfilePictureUrl
))
.FirstOrDefaultAsync(cancellationToken: ct);
if (creator is null)
{
await SendNotFoundAsync(ct);
}
else
{
await SendOkAsync(creator, ct);
}
}
}

View File

@@ -1,21 +0,0 @@
using FastEndpoints;
namespace Hutopy.Web.Features.Contents.Handlers;
public record UnsubscribeFromCreatorRequest(
Guid CreatorId);
public class UnsubscribeFromCreatorHandler
: Endpoint<UnsubscribeFromCreatorRequest>
{ public override void Configure()
{
Post("");
}
public override async Task HandleAsync(
UnsubscribeFromCreatorRequest req,
CancellationToken ct)
{
return base.HandleAsync(req, ct);
}
}

View File

@@ -1,30 +0,0 @@
using FastEndpoints;
using Hutopy.Web.Common;
using Hutopy.Web.Features.Contents.Data;
namespace Hutopy.Web.Features.Contents.Handlers;
public record SubscribeToCreatorRequest(
Guid CreatorId);
public sealed class SubscribeToCreatorHandler(
ContentDbContext context)
: Endpoint<UnsubscribeFromCreatorRequest>
{
public override void Configure()
{
Post("/api/creators/{CreatorId}/subscribe");
Options((o => o.WithTags("Creators")));
}
public override async Task HandleAsync(
UnsubscribeFromCreatorRequest req,
CancellationToken ct)
{
await context.Subscriptions.AddAsync(
new() { CreatedBy = HttpContext.User.GetUserId(), CreatorId = req.CreatorId },
ct);
await SendOkAsync(ct);
}
}

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);
}
}
}