55 lines
2.0 KiB
C#
55 lines
2.0 KiB
C#
using Distribution.Infra.Authorization;
|
|
using Distribution.Models;
|
|
using Microsoft.AspNetCore.Mvc;
|
|
|
|
namespace Distribution.Features.SysConfig;
|
|
|
|
[ApiController]
|
|
[Route("api/sys-config")]
|
|
public class SysConfigController(SysConfigService service, ICurrentUserService currentUser) : ControllerBase
|
|
{
|
|
[HttpGet]
|
|
public async Task<IActionResult> GetAll() =>
|
|
Ok(await service.GetAllAsync());
|
|
|
|
// Typed shortcut — must be declared before {key} to take precedence
|
|
[HttpGet("invoice-creation-mode")]
|
|
public async Task<IActionResult> GetInvoiceCreationMode()
|
|
{
|
|
var mode = await service.GetInvoiceCreationModeAsync();
|
|
return Ok(new { Mode = mode });
|
|
}
|
|
|
|
[HttpPut("invoice-creation-mode")]
|
|
public async Task<IActionResult> SetInvoiceCreationMode(SetInvoiceCreationModeRequest request, CancellationToken ct)
|
|
{
|
|
var userId = currentUser.UserId ?? throw new UnauthorizedAccessException();
|
|
try
|
|
{
|
|
var result = await service.SetAsync(SysConfigService.KeyInvoiceCreationMode, request.Mode.ToString(), userId, ct);
|
|
return Ok(result);
|
|
}
|
|
catch (InvalidOperationException ex) { return BadRequest(new { error = ex.Message }); }
|
|
}
|
|
|
|
[HttpGet("{key}")]
|
|
public async Task<IActionResult> GetByKey(string key)
|
|
{
|
|
var result = await service.GetByKeyAsync(key);
|
|
return result is null ? NotFound() : Ok(result);
|
|
}
|
|
|
|
[HttpPut("{key}")]
|
|
public async Task<IActionResult> Set(string key, SetSysConfigRequest request, CancellationToken ct)
|
|
{
|
|
var userId = currentUser.UserId ?? request.UserId;
|
|
if (userId is null) return BadRequest(new { error = "UserId is required while auth is unavailable" });
|
|
try
|
|
{
|
|
var result = await service.SetAsync(key, request.Value, userId.Value, ct);
|
|
return Ok(result);
|
|
}
|
|
catch (InvalidOperationException ex) { return BadRequest(new { error = ex.Message }); }
|
|
}
|
|
}
|