189 lines
6.6 KiB
C#
189 lines
6.6 KiB
C#
using LMS.Common.Extensions;
|
||
using LMS.Repository.DB;
|
||
using LMS.Repository.DTO;
|
||
using LMS.Repository.DTO.OptionDto;
|
||
using LMS.Repository.Models.DB;
|
||
using LMS.Repository.Options;
|
||
using LMS.service.Service;
|
||
using Microsoft.AspNetCore.Authorization;
|
||
using Microsoft.AspNetCore.Mvc;
|
||
using System.ComponentModel.DataAnnotations;
|
||
using static LMS.Common.Enums.ResponseCodeEnum;
|
||
|
||
namespace LMS.service.Controllers
|
||
{
|
||
/// <summary>
|
||
/// Laitool 的配置项控制器
|
||
/// </summary>
|
||
/// <param name="optionsService"></param>
|
||
[Route("lms/[controller]/[action]")]
|
||
[ApiController]
|
||
public class LaitoolOptionsController(OptionsService optionsService) : ControllerBase
|
||
{
|
||
private readonly OptionsService _optionsService = optionsService;
|
||
|
||
#region 获取简单的配置项,无需权限
|
||
/// <summary>
|
||
/// 获取简单的配置项,无需权限
|
||
/// </summary>
|
||
/// <param name="request"></param>
|
||
/// <returns></returns>
|
||
[HttpGet("{optionsKey}")]
|
||
public async Task<ActionResult<APIResponseModel<List<OptionsDto>>>> GetSimpleOptions(string optionsKey)
|
||
{
|
||
long userId = ConvertExtension.ObjectToLong(HttpContext.Items["UserId"] ?? 0);
|
||
return await _optionsService.GetSimpleOptions(optionsKey);
|
||
}
|
||
|
||
#endregion
|
||
|
||
#region 获取指定的操作,需要管理员权限
|
||
|
||
[HttpGet("{optionsKey}")]
|
||
[Authorize]
|
||
public async Task<ActionResult<APIResponseModel<List<OptionsDto>>>> GetAllOptions(string optionsKey)
|
||
{
|
||
long userId = ConvertExtension.ObjectToLong(HttpContext.Items["UserId"] ?? 0);
|
||
return await _optionsService.GetAllOptions(optionsKey, userId);
|
||
}
|
||
|
||
#endregion
|
||
|
||
#region 修改配置项
|
||
|
||
[HttpPost]
|
||
[Authorize]
|
||
public async Task<ActionResult<APIResponseModel<string>>> ModifyOptions([FromBody] List<ModofyOptionsModel> model)
|
||
{
|
||
if (!ModelState.IsValid)
|
||
{
|
||
return APIResponseModel<string>.CreateErrorResponseModel(ResponseCode.ParameterError);
|
||
}
|
||
long userId = ConvertExtension.ObjectToLong(HttpContext.Items["UserId"] ?? 0);
|
||
return await _optionsService.ModifyOptions(model, userId);
|
||
}
|
||
|
||
#endregion
|
||
|
||
#region 测试邮箱发送
|
||
|
||
[HttpPost]
|
||
[Authorize]
|
||
public async Task<ActionResult<APIResponseModel<string>>> TestSendMail()
|
||
{
|
||
long userId = ConvertExtension.ObjectToLong(HttpContext.Items["UserId"] ?? 0);
|
||
return await _optionsService.TestSendMail(userId);
|
||
}
|
||
|
||
#endregion
|
||
|
||
|
||
}
|
||
|
||
/// <summary>
|
||
/// Laitool 的配置项控制器
|
||
/// </summary>
|
||
/// <param name="optionsService"></param>
|
||
[Route("lms/[controller]/[action]")]
|
||
[ApiController]
|
||
public class OptionsController(OptionsService optionsService) : ControllerBase
|
||
{
|
||
private readonly OptionsService _optionsService = optionsService;
|
||
|
||
#region 获取指定的配置项
|
||
/// <summary>
|
||
/// 获取简单的配置项,无需权限
|
||
/// </summary>
|
||
/// <param name="request"></param>
|
||
/// <returns></returns>
|
||
[HttpGet("{userId}/{category}/{optionsKey}")]
|
||
public async Task<ActionResult<APIResponseModel<OptionsDto>>> GetOptionsByKey(long userId, int category, string optionsKey)
|
||
{
|
||
return await _optionsService.GetOptionsByKey(userId, category, optionsKey);
|
||
}
|
||
|
||
#endregion
|
||
|
||
#region 获取指定的配置项,全部
|
||
/// <summary>
|
||
/// 获取简单的配置项,无需权限
|
||
/// </summary>
|
||
/// <param name="request"></param>
|
||
/// <returns></returns>
|
||
[HttpGet("{category}/{optionsKey}")]
|
||
[Authorize]
|
||
public async Task<ActionResult<APIResponseModel<OptionSimpleDto>>> GetAllMessageOptionsByKey(int category, string optionsKey)
|
||
{
|
||
long userId = ConvertExtension.ObjectToLong(HttpContext.Items["UserId"] ?? 0);
|
||
return await _optionsService.GetAllMessageOptionsByKey(category, optionsKey, userId);
|
||
}
|
||
|
||
#endregion
|
||
|
||
|
||
#region 获取指定条件的数据信息集合
|
||
/// <summary>
|
||
/// 获取简单的配置项,无需权限
|
||
/// </summary>
|
||
/// <param name="request"></param>
|
||
/// <returns></returns>
|
||
[HttpGet]
|
||
[Authorize]
|
||
public async Task<ActionResult<APIResponseModel<CollectionResponse<OptionSimpleDto>>>> QueryOptionCollection([Required] int page, [Required] int pageSize, string? key, int? type, int? category, [FromQuery] string[]? roleNames)
|
||
{
|
||
|
||
long userId = ConvertExtension.ObjectToLong(HttpContext.Items["UserId"] ?? 0);
|
||
return await _optionsService.QueryOptionCollection(page, pageSize, key, type, category, roleNames, userId);
|
||
}
|
||
|
||
#endregion
|
||
|
||
#region 修改数据配置项
|
||
|
||
[HttpPost("{key}")]
|
||
[Authorize]
|
||
public async Task<ActionResult<APIResponseModel<string>>> ModifyOptionsByKey(string key, [FromBody] ModifyOptionByKeyModel model)
|
||
{
|
||
if (!ModelState.IsValid)
|
||
{
|
||
return APIResponseModel<string>.CreateErrorResponseModel(ResponseCode.ParameterError);
|
||
}
|
||
long userId = ConvertExtension.ObjectToLong(HttpContext.Items["UserId"] ?? 0);
|
||
return await _optionsService.ModifyOptionsByKey(key, model, userId);
|
||
}
|
||
|
||
#endregion
|
||
|
||
#region 新增指定的数据项
|
||
|
||
[HttpPost]
|
||
[Authorize]
|
||
public async Task<ActionResult<APIResponseModel<string>>> AddOptions([FromBody] AddOptionModel option)
|
||
{
|
||
if (!ModelState.IsValid)
|
||
{
|
||
return APIResponseModel<string>.CreateErrorResponseModel(ResponseCode.ParameterError);
|
||
}
|
||
long userId = ConvertExtension.ObjectToLong(HttpContext.Items["UserId"] ?? 0);
|
||
return await _optionsService.AddOptions(option, userId);
|
||
}
|
||
#endregion
|
||
|
||
#region 删除指定的key的数据
|
||
|
||
[HttpDelete("{category}/{key}")]
|
||
[Authorize]
|
||
public async Task<ActionResult<APIResponseModel<string>>> DeleteOptionsByKey(int category, string key)
|
||
{
|
||
if (!ModelState.IsValid)
|
||
{
|
||
return APIResponseModel<string>.CreateErrorResponseModel(ResponseCode.ParameterError);
|
||
}
|
||
long userId = ConvertExtension.ObjectToLong(HttpContext.Items["UserId"] ?? 0);
|
||
return await _optionsService.DeleteOptionsByKey(category, key, userId);
|
||
}
|
||
|
||
#endregion
|
||
}
|
||
}
|