Spaces:
Sleeping
Sleeping
Yuyuqt
fix: add Librarian approval for pay in cash, balance update loading time fix, membership approval page added
6469525 | using LibraryManagement.Shared.Models; | |
| using Microsoft.AspNetCore.Authorization; | |
| using Microsoft.AspNetCore.Mvc; | |
| using System.Security.Claims; | |
| namespace Backend.Features.Subscriptions | |
| { | |
| [] | |
| [] | |
| public class SubscriptionController : ControllerBase | |
| { | |
| private readonly ISubscriptionService _subscriptionService; | |
| public SubscriptionController(ISubscriptionService subscriptionService) | |
| { | |
| _subscriptionService = subscriptionService; | |
| } | |
| [] | |
| [] | |
| public async Task<ActionResult<IEnumerable<MembershipDto>>> GetMemberships() | |
| { | |
| return Ok(await _subscriptionService.GetMembershipsAsync()); | |
| } | |
| [] | |
| [] | |
| public async Task<ActionResult<SubscriptionDto>> GetMySubscription() | |
| { | |
| var userIdStr = User.FindFirstValue(ClaimTypes.NameIdentifier); | |
| if (string.IsNullOrEmpty(userIdStr)) return Unauthorized(); | |
| var userId = Guid.Parse(userIdStr); | |
| var subscription = await _subscriptionService.GetUserSubscriptionAsync(userId); | |
| if (subscription == null) return NotFound(new { message = "No active subscription found." }); | |
| return Ok(subscription); | |
| } | |
| [] | |
| [] | |
| public async Task<ActionResult<IEnumerable<SubscriptionDto>>> GetMyAllSubscriptions() | |
| { | |
| var userIdStr = User.FindFirstValue(ClaimTypes.NameIdentifier); | |
| if (string.IsNullOrEmpty(userIdStr)) return Unauthorized(); | |
| var userId = Guid.Parse(userIdStr); | |
| var subscriptions = await _subscriptionService.GetUserAllSubscriptionsAsync(userId); | |
| return Ok(subscriptions); | |
| } | |
| [] | |
| [] | |
| public async Task<ActionResult<SubscriptionDto>> Subscribe([FromBody] SubscribeRequest request) | |
| { | |
| try | |
| { | |
| var userIdStr = User.FindFirstValue(ClaimTypes.NameIdentifier); | |
| if (string.IsNullOrEmpty(userIdStr)) return Unauthorized(); | |
| var userId = Guid.Parse(userIdStr); | |
| // For cash payments, we create a pending subscription | |
| var subscription = await _subscriptionService.CreatePendingSubscriptionAsync(userId, request.MembershipId); | |
| return Ok(subscription); | |
| } | |
| catch (Exception ex) | |
| { | |
| return BadRequest(new { message = ex.Message }); | |
| } | |
| } | |
| [] | |
| [] | |
| public async Task<ActionResult<SubscriptionDto>> GetUserSubscription(Guid userId) | |
| { | |
| var subscription = await _subscriptionService.GetUserSubscriptionAsync(userId); | |
| if (subscription == null) return NotFound(new { message = "No active subscription found." }); | |
| return Ok(subscription); | |
| } | |
| [] | |
| [] | |
| public async Task<ActionResult<IEnumerable<SubscriptionDto>>> GetSubscriptions() | |
| { | |
| return Ok(await _subscriptionService.GetAllSubscriptionsAsync()); | |
| } | |
| [] | |
| [] | |
| public async Task<ActionResult<SubscriptionDto>> AdminSubscribe([FromBody] AdminSubscribeRequest request) | |
| { | |
| try | |
| { | |
| var subscription = await _subscriptionService.SubscribeUserAsync(request.UserId, request.MembershipId); | |
| return Ok(subscription); | |
| } | |
| catch (Exception ex) | |
| { | |
| return BadRequest(new { message = ex.Message }); | |
| } | |
| } | |
| [] | |
| [] | |
| public async Task<ActionResult<SubscriptionUpgradePreviewDto>> GetUpgradePreview(int membershipId) | |
| { | |
| var userIdStr = User.FindFirstValue(ClaimTypes.NameIdentifier); | |
| if (string.IsNullOrEmpty(userIdStr)) return Unauthorized(); | |
| var preview = await _subscriptionService.GetUpgradePreviewAsync(Guid.Parse(userIdStr), membershipId); | |
| return Ok(preview); | |
| } | |
| [] | |
| [] | |
| public async Task<ActionResult<SubscriptionDto>> SubscribeWithWallet([FromBody] SubscribeRequest request) | |
| { | |
| try | |
| { | |
| var userIdStr = User.FindFirstValue(ClaimTypes.NameIdentifier); | |
| if (string.IsNullOrEmpty(userIdStr)) return Unauthorized(); | |
| var subscription = await _subscriptionService.SubscribeWithWalletAsync(Guid.Parse(userIdStr), request.MembershipId); | |
| return Ok(subscription); | |
| } | |
| catch (Exception ex) | |
| { | |
| return BadRequest(new { message = ex.Message }); | |
| } | |
| } | |
| [] | |
| [] | |
| public async Task<ActionResult<IEnumerable<SubscriptionDto>>> GetPendingSubscriptions() | |
| { | |
| return Ok(await _subscriptionService.GetPendingSubscriptionsAsync()); | |
| } | |
| [] | |
| [] | |
| public async Task<IActionResult> ApproveSubscription([FromBody] ApproveSubscriptionRequest request) | |
| { | |
| var success = await _subscriptionService.ApproveSubscriptionAsync(request.SubscriptionId, request.Approve); | |
| if (!success) return NotFound(new { message = "Subscription not found." }); | |
| return Ok(new { message = request.Approve ? "Subscription approved." : "Subscription rejected." }); | |
| } | |
| } | |
| } | |