💥 Changes to subscription api
This commit is contained in:
parent
d1d4eb180f
commit
aeeed24290
@ -33,6 +33,19 @@ public class AccountController(
|
|||||||
.FirstOrDefaultAsync();
|
.FirstOrDefaultAsync();
|
||||||
return account is null ? new NotFoundResult() : account;
|
return account is null ? new NotFoundResult() : account;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
[HttpGet("{name}/badges")]
|
||||||
|
[ProducesResponseType<List<Badge>>(StatusCodes.Status200OK)]
|
||||||
|
[ProducesResponseType(StatusCodes.Status404NotFound)]
|
||||||
|
public async Task<ActionResult<List<Badge>>> 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
|
public class AccountCreateRequest
|
||||||
{
|
{
|
||||||
|
@ -1,10 +1,11 @@
|
|||||||
using Microsoft.AspNetCore.Authorization;
|
using Microsoft.AspNetCore.Authorization;
|
||||||
using Microsoft.AspNetCore.Mvc;
|
using Microsoft.AspNetCore.Mvc;
|
||||||
|
using Microsoft.EntityFrameworkCore;
|
||||||
|
|
||||||
namespace DysonNetwork.Sphere.Post;
|
namespace DysonNetwork.Sphere.Post;
|
||||||
|
|
||||||
[ApiController]
|
[ApiController]
|
||||||
[Route("api/[controller]")]
|
[Route("/publishers")]
|
||||||
public class PublisherSubscriptionController(
|
public class PublisherSubscriptionController(
|
||||||
PublisherSubscriptionService subs,
|
PublisherSubscriptionService subs,
|
||||||
AppDatabase db,
|
AppDatabase db,
|
||||||
@ -24,85 +25,69 @@ public class PublisherSubscriptionController(
|
|||||||
public int? Tier { get; set; }
|
public int? Tier { get; set; }
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
[HttpGet("{name}/subscription")]
|
||||||
/// Check if the current user is subscribed to a publisher
|
|
||||||
/// </summary>
|
|
||||||
/// <param name="publisherId">Publisher ID to check</param>
|
|
||||||
/// <returns>Subscription status</returns>
|
|
||||||
[HttpGet("{publisherId}/status")]
|
|
||||||
[Authorize]
|
[Authorize]
|
||||||
public async Task<ActionResult<SubscriptionStatusResponse>> CheckSubscriptionStatus(long publisherId)
|
public async Task<ActionResult<SubscriptionStatusResponse>> CheckSubscriptionStatus(string name)
|
||||||
{
|
{
|
||||||
if (HttpContext.Items["CurrentUser"] is not Account.Account currentUser) return Unauthorized();
|
if (HttpContext.Items["CurrentUser"] is not Account.Account currentUser) return Unauthorized();
|
||||||
|
|
||||||
// Check if the publisher exists
|
// 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)
|
if (publisher == null)
|
||||||
return NotFound("Publisher not found");
|
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
|
return new SubscriptionStatusResponse
|
||||||
{
|
{
|
||||||
IsSubscribed = isSubscribed,
|
IsSubscribed = isSubscribed,
|
||||||
PublisherId = publisherId,
|
PublisherId = publisher.Id,
|
||||||
PublisherName = publisher.Name
|
PublisherName = publisher.Name
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
[HttpPost("{name}/subscribe")]
|
||||||
/// Create or activate a subscription to a publisher
|
|
||||||
/// </summary>
|
|
||||||
/// <param name="publisherId">Publisher ID to subscribe to</param>
|
|
||||||
/// <param name="request">Subscription details</param>
|
|
||||||
/// <returns>The created or activated subscription</returns>
|
|
||||||
[HttpPost("{publisherId}/subscribe")]
|
|
||||||
[Authorize]
|
[Authorize]
|
||||||
public async Task<ActionResult<PublisherSubscription>> Subscribe(
|
public async Task<ActionResult<PublisherSubscription>> Subscribe(
|
||||||
long publisherId,
|
string name,
|
||||||
[FromBody] SubscribeRequest request)
|
[FromBody] SubscribeRequest request)
|
||||||
{
|
{
|
||||||
if (HttpContext.Items["CurrentUser"] is not Account.Account currentUser) return Unauthorized();
|
if (HttpContext.Items["CurrentUser"] is not Account.Account currentUser) return Unauthorized();
|
||||||
|
|
||||||
// Check if the publisher exists
|
// 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)
|
if (publisher == null)
|
||||||
return NotFound("Publisher not found");
|
return NotFound("Publisher not found");
|
||||||
|
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
var subscription = await subs.CreateSubscriptionAsync(
|
var subscription = await subs.CreateSubscriptionAsync(
|
||||||
currentUser.Id,
|
currentUser.Id,
|
||||||
publisherId,
|
publisher.Id,
|
||||||
request.Tier ?? 0
|
request.Tier ?? 0
|
||||||
);
|
);
|
||||||
|
|
||||||
return subscription;
|
return subscription;
|
||||||
}
|
}
|
||||||
catch (Exception ex)
|
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");
|
return StatusCode(500, "Failed to create subscription");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
[HttpPost("{name}/unsubscribe")]
|
||||||
/// Cancel a subscription to a publisher
|
|
||||||
/// </summary>
|
|
||||||
/// <param name="publisherId">Publisher ID to unsubscribe from</param>
|
|
||||||
/// <returns>Success status</returns>
|
|
||||||
[HttpPost("{publisherId}/unsubscribe")]
|
|
||||||
[Authorize]
|
[Authorize]
|
||||||
public async Task<ActionResult> Unsubscribe(long publisherId)
|
public async Task<ActionResult> Unsubscribe(string name)
|
||||||
{
|
{
|
||||||
if (HttpContext.Items["CurrentUser"] is not Account.Account currentUser) return Unauthorized();
|
if (HttpContext.Items["CurrentUser"] is not Account.Account currentUser) return Unauthorized();
|
||||||
|
|
||||||
// Check if the publisher exists
|
// 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)
|
if (publisher == null)
|
||||||
return NotFound("Publisher not found");
|
return NotFound("Publisher not found");
|
||||||
|
|
||||||
var success = await subs.CancelSubscriptionAsync(currentUser.Id, publisherId);
|
var success = await subs.CancelSubscriptionAsync(currentUser.Id, publisher.Id);
|
||||||
|
|
||||||
if (success)
|
if (success)
|
||||||
return Ok(new { message = "Subscription cancelled successfully" });
|
return Ok(new { message = "Subscription cancelled successfully" });
|
||||||
@ -114,7 +99,7 @@ public class PublisherSubscriptionController(
|
|||||||
/// Get all subscriptions for the current user
|
/// Get all subscriptions for the current user
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <returns>List of active subscriptions</returns>
|
/// <returns>List of active subscriptions</returns>
|
||||||
[HttpGet("current")]
|
[HttpGet("subscriptions")]
|
||||||
[Authorize]
|
[Authorize]
|
||||||
public async Task<ActionResult<List<PublisherSubscription>>> GetCurrentSubscriptions()
|
public async Task<ActionResult<List<PublisherSubscription>>> GetCurrentSubscriptions()
|
||||||
{
|
{
|
||||||
|
Loading…
x
Reference in New Issue
Block a user