From aeeed242904efd02fd145f2e0cbef00952bd1a50 Mon Sep 17 00:00:00 2001 From: LittleSheep Date: Wed, 14 May 2025 18:57:50 +0800 Subject: [PATCH] :boom: Changes to subscription api --- .../Account/AccountController.cs | 13 ++++ .../Post/PublisherSubscriptionController.cs | 61 +++++++------------ 2 files changed, 36 insertions(+), 38 deletions(-) diff --git a/DysonNetwork.Sphere/Account/AccountController.cs b/DysonNetwork.Sphere/Account/AccountController.cs index 0782cc5..9080a72 100644 --- a/DysonNetwork.Sphere/Account/AccountController.cs +++ b/DysonNetwork.Sphere/Account/AccountController.cs @@ -33,6 +33,19 @@ public class AccountController( .FirstOrDefaultAsync(); return account is null ? new NotFoundResult() : account; } + + [HttpGet("{name}/badges")] + [ProducesResponseType>(StatusCodes.Status200OK)] + [ProducesResponseType(StatusCodes.Status404NotFound)] + public async Task>> GetBadgesByName(string name) + { + var account = await db.Accounts + .Include(e => e.Badges) + .Where(a => a.Name == name) + .FirstOrDefaultAsync(); + return account is null ? NotFound() : account.Badges.ToList(); + } + public class AccountCreateRequest { diff --git a/DysonNetwork.Sphere/Post/PublisherSubscriptionController.cs b/DysonNetwork.Sphere/Post/PublisherSubscriptionController.cs index f1ec237..211d7f6 100644 --- a/DysonNetwork.Sphere/Post/PublisherSubscriptionController.cs +++ b/DysonNetwork.Sphere/Post/PublisherSubscriptionController.cs @@ -1,10 +1,11 @@ using Microsoft.AspNetCore.Authorization; using Microsoft.AspNetCore.Mvc; +using Microsoft.EntityFrameworkCore; namespace DysonNetwork.Sphere.Post; [ApiController] -[Route("api/[controller]")] +[Route("/publishers")] public class PublisherSubscriptionController( PublisherSubscriptionService subs, AppDatabase db, @@ -24,85 +25,69 @@ public class PublisherSubscriptionController( public int? Tier { get; set; } } - /// - /// Check if the current user is subscribed to a publisher - /// - /// Publisher ID to check - /// Subscription status - [HttpGet("{publisherId}/status")] + [HttpGet("{name}/subscription")] [Authorize] - public async Task> CheckSubscriptionStatus(long publisherId) + public async Task> CheckSubscriptionStatus(string name) { if (HttpContext.Items["CurrentUser"] is not Account.Account currentUser) return Unauthorized(); - + // Check if the publisher exists - var publisher = await db.Publishers.FindAsync(publisherId); + var publisher = await db.Publishers.FirstOrDefaultAsync(p => p.Name == name); if (publisher == null) return NotFound("Publisher not found"); - - var isSubscribed = await subs.SubscriptionExistsAsync(currentUser.Id, publisherId); - + + var isSubscribed = await subs.SubscriptionExistsAsync(currentUser.Id, publisher.Id); + return new SubscriptionStatusResponse { IsSubscribed = isSubscribed, - PublisherId = publisherId, + PublisherId = publisher.Id, PublisherName = publisher.Name }; } - /// - /// Create or activate a subscription to a publisher - /// - /// Publisher ID to subscribe to - /// Subscription details - /// The created or activated subscription - [HttpPost("{publisherId}/subscribe")] + [HttpPost("{name}/subscribe")] [Authorize] public async Task> Subscribe( - long publisherId, + string name, [FromBody] SubscribeRequest request) { if (HttpContext.Items["CurrentUser"] is not Account.Account currentUser) return Unauthorized(); - + // Check if the publisher exists - var publisher = await db.Publishers.FindAsync(publisherId); + var publisher = await db.Publishers.FirstOrDefaultAsync(p => p.Name == name); if (publisher == null) return NotFound("Publisher not found"); - + try { var subscription = await subs.CreateSubscriptionAsync( currentUser.Id, - publisherId, + publisher.Id, request.Tier ?? 0 ); - + return subscription; } catch (Exception ex) { - logger.LogError(ex, "Error subscribing to publisher {PublisherId}", publisherId); + logger.LogError(ex, "Error subscribing to publisher {PublisherName}", name); return StatusCode(500, "Failed to create subscription"); } } - /// - /// Cancel a subscription to a publisher - /// - /// Publisher ID to unsubscribe from - /// Success status - [HttpPost("{publisherId}/unsubscribe")] + [HttpPost("{name}/unsubscribe")] [Authorize] - public async Task Unsubscribe(long publisherId) + public async Task Unsubscribe(string name) { if (HttpContext.Items["CurrentUser"] is not Account.Account currentUser) return Unauthorized(); // Check if the publisher exists - var publisher = await db.Publishers.FindAsync(publisherId); + var publisher = await db.Publishers.FirstOrDefaultAsync(e => e.Name == name); if (publisher == null) return NotFound("Publisher not found"); - var success = await subs.CancelSubscriptionAsync(currentUser.Id, publisherId); + var success = await subs.CancelSubscriptionAsync(currentUser.Id, publisher.Id); if (success) return Ok(new { message = "Subscription cancelled successfully" }); @@ -114,7 +99,7 @@ public class PublisherSubscriptionController( /// Get all subscriptions for the current user /// /// List of active subscriptions - [HttpGet("current")] + [HttpGet("subscriptions")] [Authorize] public async Task>> GetCurrentSubscriptions() {