Spaces:
Sleeping
Sleeping
File size: 2,077 Bytes
87c9973 f53877e 87c9973 f53877e 87c9973 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 | using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using System.Security.Claims;
namespace LibraryManagement.Backend.Features.Subscriptions
{
[ApiController]
[Route("api")]
public class SubscriptionController : ControllerBase
{
private readonly ISubscriptionService _subscriptionService;
public SubscriptionController(ISubscriptionService subscriptionService)
{
_subscriptionService = subscriptionService;
}
[HttpGet("memberships")]
[AllowAnonymous]
public async Task<ActionResult<IEnumerable<MembershipDto>>> GetMemberships()
{
return Ok(await _subscriptionService.GetMembershipsAsync());
}
[HttpGet("subscriptions/me")]
//[Authorize]
public async Task<ActionResult<SubscriptionDto>> GetMySubscription()
{
var userIdStr = User.FindFirstValue(ClaimTypes.NameIdentifier);
if (string.IsNullOrEmpty(userIdStr)) return Unauthorized();
var userId = int.Parse(userIdStr);
var subscription = await _subscriptionService.GetUserSubscriptionAsync(userId);
if (subscription == null) return NotFound(new { message = "No active subscription found." });
return Ok(subscription);
}
[HttpPost("subscriptions/subscribe")]
//[Authorize]
public async Task<ActionResult<SubscriptionDto>> Subscribe([FromBody] SubscribeRequest request)
{
try
{
var userIdStr = User.FindFirstValue(ClaimTypes.NameIdentifier);
if (string.IsNullOrEmpty(userIdStr)) return Unauthorized();
var userId = int.Parse(userIdStr);
var subscription = await _subscriptionService.SubscribeUserAsync(userId, request.MembershipId);
return Ok(subscription);
}
catch (Exception ex)
{
return BadRequest(new { message = ex.Message });
}
}
}
}
|