BililiveRecorder/BililiveRecorder.Web/Api/ConfigController.cs
2022-07-08 21:13:00 +08:00

51 lines
1.7 KiB
C#

using System;
using AutoMapper;
using BililiveRecorder.Core;
using BililiveRecorder.Core.Config.V3;
using BililiveRecorder.Web.Models;
using BililiveRecorder.Web.Models.Rest;
using Microsoft.AspNetCore.Mvc;
namespace BililiveRecorder.Web.Api
{
[ApiController, Route("api/[controller]", Name = "[controller] [action]")]
public sealed class ConfigController : ControllerBase
{
private readonly IMapper mapper;
private readonly IRecorder recorder;
public ConfigController(IMapper mapper, IRecorder recorder)
{
this.mapper = mapper ?? throw new ArgumentNullException(nameof(mapper));
this.recorder = recorder ?? throw new ArgumentNullException(nameof(recorder));
}
/// <summary>
/// 获取软件默认设置
/// </summary>
/// <returns></returns>
[HttpGet("default")]
public ActionResult<DefaultConfig> GetDefaultConfig() => DefaultConfig.Instance;
/// <summary>
/// 获取全局设置
/// </summary>
/// <returns></returns>
[HttpGet("global")]
public ActionResult<GlobalConfigDto> GetGlobalConfig() => this.mapper.Map<GlobalConfigDto>(this.recorder.Config.Global);
/// <summary>
/// 设置全局设置
/// </summary>
/// <param name="config"></param>
/// <returns></returns>
[HttpPost("global")]
public ActionResult<GlobalConfigDto> SetGlobalConfig([FromBody] SetGlobalConfig config)
{
config.ApplyTo(this.recorder.Config.Global);
this.recorder.SaveConfig();
return this.mapper.Map<GlobalConfigDto>(this.recorder.Config.Global);
}
}
}