♻️ Simplified publisher subscription
This commit is contained in:
@@ -194,13 +194,6 @@ public class SnPublisherMember : ModelBase
|
||||
}
|
||||
}
|
||||
|
||||
public enum PublisherSubscriptionStatus
|
||||
{
|
||||
Active,
|
||||
Expired,
|
||||
Cancelled
|
||||
}
|
||||
|
||||
public class SnPublisherSubscription : ModelBase
|
||||
{
|
||||
public Guid Id { get; set; }
|
||||
@@ -208,9 +201,6 @@ public class SnPublisherSubscription : ModelBase
|
||||
public Guid PublisherId { get; set; }
|
||||
public SnPublisher Publisher { get; set; } = null!;
|
||||
public Guid AccountId { get; set; }
|
||||
|
||||
public PublisherSubscriptionStatus Status { get; set; } = PublisherSubscriptionStatus.Active;
|
||||
public int Tier { get; set; } = 0;
|
||||
}
|
||||
|
||||
public class SnPublisherFeature : ModelBase
|
||||
|
||||
1798
DysonNetwork.Sphere/Migrations/20260111165219_RemoveSubscriptionTierAndStatus.Designer.cs
generated
Normal file
1798
DysonNetwork.Sphere/Migrations/20260111165219_RemoveSubscriptionTierAndStatus.Designer.cs
generated
Normal file
File diff suppressed because it is too large
Load Diff
@@ -0,0 +1,40 @@
|
||||
using Microsoft.EntityFrameworkCore.Migrations;
|
||||
|
||||
#nullable disable
|
||||
|
||||
namespace DysonNetwork.Sphere.Migrations
|
||||
{
|
||||
/// <inheritdoc />
|
||||
public partial class RemoveSubscriptionTierAndStatus : Migration
|
||||
{
|
||||
/// <inheritdoc />
|
||||
protected override void Up(MigrationBuilder migrationBuilder)
|
||||
{
|
||||
migrationBuilder.DropColumn(
|
||||
name: "status",
|
||||
table: "publisher_subscriptions");
|
||||
|
||||
migrationBuilder.DropColumn(
|
||||
name: "tier",
|
||||
table: "publisher_subscriptions");
|
||||
}
|
||||
|
||||
/// <inheritdoc />
|
||||
protected override void Down(MigrationBuilder migrationBuilder)
|
||||
{
|
||||
migrationBuilder.AddColumn<int>(
|
||||
name: "status",
|
||||
table: "publisher_subscriptions",
|
||||
type: "integer",
|
||||
nullable: false,
|
||||
defaultValue: 0);
|
||||
|
||||
migrationBuilder.AddColumn<int>(
|
||||
name: "tier",
|
||||
table: "publisher_subscriptions",
|
||||
type: "integer",
|
||||
nullable: false,
|
||||
defaultValue: 0);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -649,7 +649,8 @@ namespace DysonNetwork.Sphere.Migrations
|
||||
|
||||
b.Property<Dictionary<string, object>>("Metadata")
|
||||
.HasColumnType("jsonb")
|
||||
.HasColumnName("metadata");
|
||||
.HasColumnName("metadata")
|
||||
.HasJsonPropertyName("meta");
|
||||
|
||||
b.Property<int?>("PinMode")
|
||||
.HasColumnType("integer")
|
||||
@@ -1234,14 +1235,6 @@ namespace DysonNetwork.Sphere.Migrations
|
||||
.HasColumnType("uuid")
|
||||
.HasColumnName("publisher_id");
|
||||
|
||||
b.Property<int>("Status")
|
||||
.HasColumnType("integer")
|
||||
.HasColumnName("status");
|
||||
|
||||
b.Property<int>("Tier")
|
||||
.HasColumnType("integer")
|
||||
.HasColumnName("tier");
|
||||
|
||||
b.Property<Instant>("UpdatedAt")
|
||||
.HasColumnType("timestamp with time zone")
|
||||
.HasColumnName("updated_at");
|
||||
|
||||
@@ -138,7 +138,6 @@ public class PublisherService(
|
||||
// If not in cache, fetch from a database
|
||||
var publishersId = await db.PublisherSubscriptions
|
||||
.Where(p => p.AccountId == userId)
|
||||
.Where(p => p.Status == PublisherSubscriptionStatus.Active)
|
||||
.Select(p => p.PublisherId)
|
||||
.ToListAsync();
|
||||
publishers = await db.Publishers
|
||||
|
||||
@@ -17,7 +17,6 @@ public class PublisherSubscriptionController(
|
||||
{
|
||||
public class SubscribeRequest
|
||||
{
|
||||
public int? Tier { get; set; }
|
||||
}
|
||||
|
||||
[HttpGet("{name}/subscription")]
|
||||
@@ -53,8 +52,7 @@ public class PublisherSubscriptionController(
|
||||
{
|
||||
var subscription = await subs.CreateSubscriptionAsync(
|
||||
Guid.Parse(currentUser.Id),
|
||||
publisher.Id,
|
||||
request.Tier ?? 0
|
||||
publisher.Id
|
||||
);
|
||||
|
||||
return subscription;
|
||||
@@ -101,7 +99,7 @@ public class PublisherSubscriptionController(
|
||||
|
||||
var pubQuery = db.PublisherSubscriptions
|
||||
.Include(ps => ps.Publisher)
|
||||
.Where(ps => ps.AccountId == accountId && ps.Status == PublisherSubscriptionStatus.Active)
|
||||
.Where(ps => ps.AccountId == accountId)
|
||||
.OrderByDescending(ps => ps.CreatedAt)
|
||||
.AsQueryable();
|
||||
|
||||
|
||||
@@ -27,8 +27,7 @@ public class PublisherSubscriptionService(
|
||||
{
|
||||
return await db.PublisherSubscriptions
|
||||
.AnyAsync(p => p.AccountId == accountId &&
|
||||
p.PublisherId == publisherId &&
|
||||
p.Status == PublisherSubscriptionStatus.Active);
|
||||
p.PublisherId == publisherId);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@@ -77,8 +76,7 @@ public class PublisherSubscriptionService(
|
||||
|
||||
// Gather subscribers
|
||||
var subscribers = await db.PublisherSubscriptions
|
||||
.Where(p => p.PublisherId == post.PublisherId &&
|
||||
p.Status == PublisherSubscriptionStatus.Active)
|
||||
.Where(p => p.PublisherId == post.PublisherId)
|
||||
.ToListAsync();
|
||||
if (subscribers.Count == 0)
|
||||
return 0;
|
||||
@@ -150,7 +148,7 @@ public class PublisherSubscriptionService(
|
||||
{
|
||||
return await db.PublisherSubscriptions
|
||||
.Include(p => p.Publisher)
|
||||
.Where(p => p.AccountId == accountId && p.Status == PublisherSubscriptionStatus.Active)
|
||||
.Where(p => p.AccountId == accountId)
|
||||
.ToListAsync();
|
||||
}
|
||||
|
||||
@@ -163,7 +161,6 @@ public class PublisherSubscriptionService(
|
||||
{
|
||||
return await db.PublisherSubscriptions
|
||||
.Where(p => p.PublisherId == publisherId)
|
||||
.Where(p => p.Status == PublisherSubscriptionStatus.Active)
|
||||
.ToListAsync();
|
||||
}
|
||||
|
||||
@@ -172,37 +169,23 @@ public class PublisherSubscriptionService(
|
||||
/// </summary>
|
||||
/// <param name="accountId">The account ID</param>
|
||||
/// <param name="publisherId">The publisher ID</param>
|
||||
/// <param name="tier">Optional subscription tier</param>
|
||||
/// <returns>The created subscription</returns>
|
||||
public async Task<SnPublisherSubscription> CreateSubscriptionAsync(
|
||||
Guid accountId,
|
||||
Guid publisherId,
|
||||
int tier = 0
|
||||
Guid publisherId
|
||||
)
|
||||
{
|
||||
// Check if a subscription already exists
|
||||
var existingSubscription = await GetSubscriptionAsync(accountId, publisherId);
|
||||
|
||||
if (existingSubscription != null)
|
||||
{
|
||||
// If it exists but is not active, reactivate it
|
||||
if (existingSubscription.Status == PublisherSubscriptionStatus.Active) return existingSubscription;
|
||||
existingSubscription.Status = PublisherSubscriptionStatus.Active;
|
||||
existingSubscription.Tier = tier;
|
||||
|
||||
await db.SaveChangesAsync();
|
||||
return existingSubscription;
|
||||
|
||||
// If it's already active, just return it
|
||||
}
|
||||
|
||||
// Create a new subscription
|
||||
var subscription = new SnPublisherSubscription
|
||||
{
|
||||
AccountId = accountId,
|
||||
PublisherId = publisherId,
|
||||
Status = PublisherSubscriptionStatus.Active,
|
||||
Tier = tier,
|
||||
};
|
||||
|
||||
db.PublisherSubscriptions.Add(subscription);
|
||||
@@ -214,18 +197,18 @@ public class PublisherSubscriptionService(
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Cancels a subscription
|
||||
/// Deletes a subscription
|
||||
/// </summary>
|
||||
/// <param name="accountId">The account ID</param>
|
||||
/// <param name="publisherId">The publisher ID</param>
|
||||
/// <returns>True if the subscription was cancelled, false if it wasn't found</returns>
|
||||
/// <returns>True if the subscription was deleted, false if it wasn't found</returns>
|
||||
public async Task<bool> CancelSubscriptionAsync(Guid accountId, Guid publisherId)
|
||||
{
|
||||
var subscription = await GetSubscriptionAsync(accountId, publisherId);
|
||||
if (subscription is not { Status: PublisherSubscriptionStatus.Active })
|
||||
if (subscription is null)
|
||||
return false;
|
||||
|
||||
subscription.Status = PublisherSubscriptionStatus.Cancelled;
|
||||
db.PublisherSubscriptions.Remove(subscription);
|
||||
await db.SaveChangesAsync();
|
||||
|
||||
await cache.RemoveAsync(string.Format(PublisherService.SubscribedPublishersCacheKey, accountId));
|
||||
|
||||
Reference in New Issue
Block a user