Adds subscriptions
This commit is contained in:
@@ -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);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,6 @@
|
||||
namespace Hutopy.Web.Features.Contents.Handlers.Models;
|
||||
|
||||
public record SubscriptionModel(
|
||||
Guid CreatorId,
|
||||
string CreatorName,
|
||||
string? CreatorPortraitUrl);
|
||||
54
src/Web/Features/Contents/Handlers/SubscribeToCreator.cs
Normal file
54
src/Web/Features/Contents/Handlers/SubscribeToCreator.cs
Normal 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);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -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);
|
||||
}
|
||||
}
|
||||
@@ -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);
|
||||
}
|
||||
}
|
||||
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