manager-service/ManagerService/Controllers/SubscriptionPlanController.cs
2026-04-01 17:00:13 +02:00

151 lines
5.4 KiB
C#

using ManagerService.Data;
using ManagerService.DTOs;
using ManagerService.Helpers;
using ManagerService.Services;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Logging;
using NSwag.Annotations;
using System;
using System.Collections.Generic;
using System.Linq;
namespace ManagerService.Controllers
{
[Authorize(Policy = ManagerService.Service.Security.Policies.SuperAdmin)]
[ApiController, Route("api/[controller]")]
[OpenApiTag("SubscriptionPlan", Description = "Subscription plan management")]
public class SubscriptionPlanController : ControllerBase
{
private readonly MyInfoMateDbContext _myInfoMateDbContext;
private readonly ILogger<SubscriptionPlanController> _logger;
IHexIdGeneratorService idService = new HexIdGeneratorService();
public SubscriptionPlanController(ILogger<SubscriptionPlanController> logger, MyInfoMateDbContext myInfoMateDbContext)
{
_logger = logger;
_myInfoMateDbContext = myInfoMateDbContext;
}
[ProducesResponseType(typeof(List<SubscriptionPlanDTO>), 200)]
[ProducesResponseType(typeof(string), 500)]
[HttpGet]
public ObjectResult Get()
{
try
{
var plans = _myInfoMateDbContext.SubscriptionPlans.ToList();
return new OkObjectResult(plans.Select(p => p.ToDTO()).ToList());
}
catch (Exception ex)
{
return new ObjectResult(ex.Message) { StatusCode = 500 };
}
}
[ProducesResponseType(typeof(SubscriptionPlanDTO), 200)]
[ProducesResponseType(typeof(string), 404)]
[ProducesResponseType(typeof(string), 500)]
[HttpGet("{id}")]
public ObjectResult GetById(string id)
{
try
{
var plan = _myInfoMateDbContext.SubscriptionPlans.FirstOrDefault(p => p.Id == id);
if (plan == null)
return new NotFoundObjectResult("Subscription plan not found");
return new OkObjectResult(plan.ToDTO());
}
catch (Exception ex)
{
return new ObjectResult(ex.Message) { StatusCode = 500 };
}
}
[ProducesResponseType(typeof(SubscriptionPlanDTO), 200)]
[ProducesResponseType(typeof(string), 400)]
[ProducesResponseType(typeof(string), 500)]
[HttpPost]
public ObjectResult Create([FromBody] SubscriptionPlanDTO dto)
{
try
{
if (dto == null)
return new BadRequestObjectResult("Plan data is required");
var plan = new SubscriptionPlan().FromDTO(dto);
plan.Id = idService.GenerateHexId();
_myInfoMateDbContext.SubscriptionPlans.Add(plan);
_myInfoMateDbContext.SaveChanges();
return new OkObjectResult(plan.ToDTO());
}
catch (Exception ex)
{
return new ObjectResult(ex.Message) { StatusCode = 500 };
}
}
[ProducesResponseType(typeof(SubscriptionPlanDTO), 200)]
[ProducesResponseType(typeof(string), 400)]
[ProducesResponseType(typeof(string), 404)]
[ProducesResponseType(typeof(string), 500)]
[HttpPut]
public ObjectResult Update([FromBody] SubscriptionPlanDTO dto)
{
try
{
if (dto == null || dto.id == null)
return new BadRequestObjectResult("Plan id is required");
var plan = _myInfoMateDbContext.SubscriptionPlans.FirstOrDefault(p => p.Id == dto.id);
if (plan == null)
return new NotFoundObjectResult("Subscription plan not found");
plan.FromDTO(dto);
_myInfoMateDbContext.SaveChanges();
return new OkObjectResult(plan.ToDTO());
}
catch (Exception ex)
{
return new ObjectResult(ex.Message) { StatusCode = 500 };
}
}
[ProducesResponseType(typeof(string), 202)]
[ProducesResponseType(typeof(string), 400)]
[ProducesResponseType(typeof(string), 404)]
[ProducesResponseType(typeof(string), 409)]
[ProducesResponseType(typeof(string), 500)]
[HttpDelete("{id}")]
public ObjectResult Delete(string id)
{
try
{
if (id == null)
return new BadRequestObjectResult("Plan id is required");
var plan = _myInfoMateDbContext.SubscriptionPlans.FirstOrDefault(p => p.Id == id);
if (plan == null)
return new NotFoundObjectResult("Subscription plan not found");
var isUsed = _myInfoMateDbContext.Instances.Any(i => i.SubscriptionPlanId == id);
if (isUsed)
return new ConflictObjectResult("This plan is assigned to one or more instances");
_myInfoMateDbContext.SubscriptionPlans.Remove(plan);
_myInfoMateDbContext.SaveChanges();
return new ObjectResult("The subscription plan has been deleted") { StatusCode = 202 };
}
catch (Exception ex)
{
return new ObjectResult(ex.Message) { StatusCode = 500 };
}
}
}
}