From 84baa3a1477e7b100fdb7867f893982ce2f22e88 Mon Sep 17 00:00:00 2001 From: "ashutosh.nehete" Date: Tue, 16 Sep 2025 18:17:43 +0530 Subject: [PATCH] Added the subscription list API in Market Controller --- .../Controllers/MarketController.cs | 94 ++++++++++++++++--- 1 file changed, 83 insertions(+), 11 deletions(-) diff --git a/Marco.Pms.Services/Controllers/MarketController.cs b/Marco.Pms.Services/Controllers/MarketController.cs index e9bfde7..c9e1923 100644 --- a/Marco.Pms.Services/Controllers/MarketController.cs +++ b/Marco.Pms.Services/Controllers/MarketController.cs @@ -1,9 +1,12 @@ -using Marco.Pms.DataAccess.Data; +using AutoMapper; +using Marco.Pms.DataAccess.Data; +using Marco.Pms.Helpers.Utility; using Marco.Pms.Model.Dtos.Util; using Marco.Pms.Model.Mapper; using Marco.Pms.Model.Master; +using Marco.Pms.Model.TenantModels; using Marco.Pms.Model.Utilities; -using MarcoBMS.Services.Helpers; +using Marco.Pms.Model.ViewModels.Tenant; using MarcoBMS.Services.Service; using Microsoft.AspNetCore.Mvc; using Microsoft.EntityFrameworkCore; @@ -14,24 +17,30 @@ namespace Marco.Pms.Services.Controllers [ApiController] public class MarketController : ControllerBase { - private readonly ApplicationDbContext _context; - private readonly UserHelper _userHelper; + private readonly IDbContextFactory _dbContextFactory; + private readonly FeatureDetailsHelper _featureDetailsHelper; + private readonly IServiceScopeFactory _serviceScopeFactory; private readonly IEmailSender _emailSender; private readonly IConfiguration _configuration; - public MarketController(ApplicationDbContext context, UserHelper userHelper, RefreshTokenService refreshTokenService, - IEmailSender emailSender, IConfiguration configuration, EmployeeHelper employeeHelper) + public MarketController(IDbContextFactory dbContextFactory, + IServiceScopeFactory serviceScopeFactory, + IEmailSender emailSender, + IConfiguration configuration, + FeatureDetailsHelper featureDetailsHelper) { - _context = context; - _userHelper = userHelper; - _emailSender = emailSender; - _configuration = configuration; + _dbContextFactory = dbContextFactory ?? throw new ArgumentNullException(nameof(dbContextFactory)); + _serviceScopeFactory = serviceScopeFactory ?? throw new ArgumentNullException(nameof(serviceScopeFactory)); + _emailSender = emailSender ?? throw new ArgumentNullException(nameof(emailSender)); + _configuration = configuration ?? throw new ArgumentNullException(nameof(configuration)); + _featureDetailsHelper = featureDetailsHelper ?? throw new ArgumentNullException(nameof(featureDetailsHelper)); } [HttpGet] [Route("industries")] public async Task GetIndustries() { - var tenantId = _userHelper.GetTenantId(); + await using var _context = await _dbContextFactory.CreateDbContextAsync(); + var industries = await _context.Industries.ToListAsync(); return Ok(ApiResponse.SuccessResponse(industries, "Success.", 200)); @@ -40,6 +49,8 @@ namespace Marco.Pms.Services.Controllers [HttpPost("enquire")] public async Task RequestDemo([FromBody] InquiryDto inquiryDto) { + await using var _context = await _dbContextFactory.CreateDbContextAsync(); + Inquiries inquiry = inquiryDto.ToInquiriesFromInquiriesDto(); _context.Inquiries.Add(inquiry); await _context.SaveChangesAsync(); @@ -58,5 +69,66 @@ namespace Marco.Pms.Services.Controllers } return NotFound(ApiResponse.ErrorResponse("Industry not found.", "Industry not found.", 404)); } + + [HttpGet("list/subscription-plan")] + public async Task GetSubscriptionPlanList([FromQuery] PLAN_FREQUENCY? frequency) + { + using var scope = _serviceScopeFactory.CreateScope(); + + var _logger = scope.ServiceProvider.GetRequiredService(); + var _mapper = scope.ServiceProvider.GetRequiredService(); + _logger.LogInfo("GetSubscriptionPlanList called with frequency: {Frequency}", frequency ?? PLAN_FREQUENCY.MONTHLY); + + // Initialize the list to store subscription plan view models + List detailsVM = new List(); + + try + { + // Create DbContext + await using var _context = await _dbContextFactory.CreateDbContextAsync(); + + // Load subscription plans with optional frequency filtering + IQueryable query = _context.SubscriptionPlanDetails.Include(sp => sp.Plan).Include(sp => sp.Currency); + + if (frequency.HasValue) + { + query = query.Where(sp => sp.Frequency == frequency.Value); + _logger.LogInfo("Filtering subscription plans by frequency: {Frequency}", frequency); + } + else + { + _logger.LogInfo("Fetching all subscription plans without frequency filter"); + } + + var subscriptionPlans = await query.ToListAsync(); + + // Map and fetch feature details for each subscription plan + foreach (var subscriptionPlan in subscriptionPlans) + { + var response = _mapper.Map(subscriptionPlan); + + try + { + response.Features = await _featureDetailsHelper.GetFeatureDetails(subscriptionPlan.FeaturesId); + } + catch (Exception exFeature) + { + _logger.LogError(exFeature, "Failed to fetch features for FeaturesId: {FeaturesId}", subscriptionPlan.FeaturesId); + response.Features = null; // or set to a default/fallback value + } + + detailsVM.Add(response); + } + + _logger.LogInfo("Successfully fetched {Count} subscription plans", detailsVM.Count); + + return Ok(ApiResponse.SuccessResponse(detailsVM, "List of plans fetched successfully", 200)); + } + catch (Exception ex) + { + _logger.LogError(ex, "Error occurred while fetching subscription plans"); + return StatusCode(500, ApiResponse.ErrorResponse("An error occurred while fetching subscription plans.")); + } + } } } -- 2.43.0