From 0340904847c9b778616b49af44581913b2fa9c05 Mon Sep 17 00:00:00 2001 From: Jonathan Bourdon Date: Sat, 3 Aug 2024 22:56:12 -0400 Subject: [PATCH] Hold on Subscriptions --- .../Contents/Data/ContentDbContext.cs | 17 +++++----- .../Features/Contents/Data/Subscription.cs | 9 ++++++ .../Contents/Handlers/GetCreatorByAlias.cs | 2 +- .../Contents/Handlers/GetCreatorById.cs | 2 +- .../Contents/Handlers/GetSubscriptions.cs | 31 +++++++++++++++++++ .../Handlers/SubscripbeFromCreator.cs | 21 +++++++++++++ .../Contents/Handlers/SubscripbeToCreator.cs | 30 ++++++++++++++++++ 7 files changed, 103 insertions(+), 9 deletions(-) create mode 100644 src/Web/Features/Contents/Data/Subscription.cs create mode 100644 src/Web/Features/Contents/Handlers/GetSubscriptions.cs create mode 100644 src/Web/Features/Contents/Handlers/SubscripbeFromCreator.cs create mode 100644 src/Web/Features/Contents/Handlers/SubscripbeToCreator.cs diff --git a/src/Web/Features/Contents/Data/ContentDbContext.cs b/src/Web/Features/Contents/Data/ContentDbContext.cs index 1215d7f..4dd5476 100644 --- a/src/Web/Features/Contents/Data/ContentDbContext.cs +++ b/src/Web/Features/Contents/Data/ContentDbContext.cs @@ -8,6 +8,10 @@ public class ContentDbContext( { public const string SchemaName = "Content"; + public DbSet Contents => Set(); + public DbSet Creators => Set(); + public DbSet Subscriptions => Set(); + protected override void OnModelCreating(ModelBuilder modelBuilder) { modelBuilder.HasDefaultSchema("Content"); @@ -24,21 +28,20 @@ public class ContentDbContext( modelBuilder .Entity() - .OwnsOne(x => x.SocialNetworks); + .OwnsOne(x => x.SocialNetworks) + .ToTable(nameof(SocialNetworks)); modelBuilder .Entity() - .OwnsOne(x => x.ProfileColors); + .OwnsOne(x => x.ProfileColors) + .ToTable(nameof(ProfileColors)); modelBuilder .Entity() - .OwnsOne(x => x.StoredDataUrls); + .OwnsOne(x => x.StoredDataUrls) + .ToTable(nameof(StoredDataUrls)); } - public DbSet Contents { get; init; } = null!; - public DbSet Creators { get; init; } = null!; - - public async Task FindByCreatorAliasAsync( string creatorAlias, CancellationToken cancellationToken = default) diff --git a/src/Web/Features/Contents/Data/Subscription.cs b/src/Web/Features/Contents/Data/Subscription.cs new file mode 100644 index 0000000..d3a0cbd --- /dev/null +++ b/src/Web/Features/Contents/Data/Subscription.cs @@ -0,0 +1,9 @@ +namespace Hutopy.Web.Features.Contents.Data; + +public class Subscription +{ + public Guid Id { get; init; } + public Guid CreatorId { get; init; } + public Guid CreatedBy { get; init; } + public DateTimeOffset CreatedAt { get; init; } +} diff --git a/src/Web/Features/Contents/Handlers/GetCreatorByAlias.cs b/src/Web/Features/Contents/Handlers/GetCreatorByAlias.cs index 6204558..df15cac 100644 --- a/src/Web/Features/Contents/Handlers/GetCreatorByAlias.cs +++ b/src/Web/Features/Contents/Handlers/GetCreatorByAlias.cs @@ -28,7 +28,7 @@ public class GetCreatorByAliasHandler( public override void Configure() { Get("/api/creators/@{Name}"); - Options((o => o.WithTags("Creators"))); + Options((o => o.WithTags("Contents"))); AllowAnonymous(); } diff --git a/src/Web/Features/Contents/Handlers/GetCreatorById.cs b/src/Web/Features/Contents/Handlers/GetCreatorById.cs index a5ed6ad..0022515 100644 --- a/src/Web/Features/Contents/Handlers/GetCreatorById.cs +++ b/src/Web/Features/Contents/Handlers/GetCreatorById.cs @@ -27,7 +27,7 @@ public class GetCreatorByIdHandler( public override void Configure() { Get("/api/creators/{CreatorId}"); - Options((o => o.WithTags("Creators"))); + Options((o => o.WithTags("Contents"))); AllowAnonymous(); } diff --git a/src/Web/Features/Contents/Handlers/GetSubscriptions.cs b/src/Web/Features/Contents/Handlers/GetSubscriptions.cs new file mode 100644 index 0000000..d175102 --- /dev/null +++ b/src/Web/Features/Contents/Handlers/GetSubscriptions.cs @@ -0,0 +1,31 @@ +using FastEndpoints; +using Hutopy.Web.Common; +using Hutopy.Web.Features.Contents.Data; +using Microsoft.EntityFrameworkCore; + +namespace Hutopy.Web.Features.Contents.Handlers; + +public class GetSubscriptionsHandler( + ContentDbContext context) + : EndpointWithoutRequest +{ + public override void Configure() + { + Get("/api/subscriptions"); + Options((o => o.WithTags("Creators"))); + } + + public override async Task HandleAsync( + CancellationToken ct) + { + var userId = HttpContext.User.GetUserId(); + + await context + .Subscriptions + .Where(s => s.CreatedBy == userId) + .Include(s => s.Creator) + .ToListAsync(cancellationToken: ct); + + await SendOkAsync(ct); + } +} diff --git a/src/Web/Features/Contents/Handlers/SubscripbeFromCreator.cs b/src/Web/Features/Contents/Handlers/SubscripbeFromCreator.cs new file mode 100644 index 0000000..5c8787c --- /dev/null +++ b/src/Web/Features/Contents/Handlers/SubscripbeFromCreator.cs @@ -0,0 +1,21 @@ +using FastEndpoints; + +namespace Hutopy.Web.Features.Contents.Handlers; + +public record UnsubscribeFromCreatorRequest( + Guid CreatorId); + +public class UnsubscribeFromCreatorHandler + : Endpoint +{ public override void Configure() + { + Post(""); + } + + public override async Task HandleAsync( + UnsubscribeFromCreatorRequest req, + CancellationToken ct) + { + return base.HandleAsync(req, ct); + } +} diff --git a/src/Web/Features/Contents/Handlers/SubscripbeToCreator.cs b/src/Web/Features/Contents/Handlers/SubscripbeToCreator.cs new file mode 100644 index 0000000..7b4ce45 --- /dev/null +++ b/src/Web/Features/Contents/Handlers/SubscripbeToCreator.cs @@ -0,0 +1,30 @@ +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 +{ + 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); + } +}