Initial commit
This commit is contained in:
@@ -0,0 +1,65 @@
|
||||
using ExpenseTracker.Api.Data;
|
||||
using ExpenseTracker.Api.DTOs;
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
|
||||
namespace ExpenseTracker.Api.Controllers;
|
||||
|
||||
/// <summary>Endpoints for managing global application settings.</summary>
|
||||
[ApiController]
|
||||
[Route("api/settings")]
|
||||
[Produces("application/json")]
|
||||
public class SettingsController : ControllerBase
|
||||
{
|
||||
private readonly AppDbContext _db;
|
||||
private readonly ILogger<SettingsController> _logger;
|
||||
|
||||
public SettingsController(AppDbContext db, ILogger<SettingsController> logger)
|
||||
{
|
||||
_db = db;
|
||||
_logger = logger;
|
||||
}
|
||||
|
||||
// ── GET /api/settings ─────────────────────────────────────────────────────
|
||||
|
||||
/// <summary>Returns all application settings.</summary>
|
||||
[HttpGet]
|
||||
[ProducesResponseType(typeof(List<SettingsDto>), StatusCodes.Status200OK)]
|
||||
public async Task<IActionResult> GetAll()
|
||||
{
|
||||
var settings = await _db.AppSettings
|
||||
.OrderBy(s => s.Key)
|
||||
.Select(s => new SettingsDto(s.Key, s.Value))
|
||||
.ToListAsync();
|
||||
|
||||
return Ok(settings);
|
||||
}
|
||||
|
||||
// ── PUT /api/settings/default-salary ──────────────────────────────────────
|
||||
|
||||
/// <summary>Updates the default salary setting.</summary>
|
||||
[HttpPut("default-salary")]
|
||||
[ProducesResponseType(typeof(SettingsDto), StatusCodes.Status200OK)]
|
||||
[ProducesResponseType(StatusCodes.Status400BadRequest)]
|
||||
[ProducesResponseType(StatusCodes.Status404NotFound)]
|
||||
public async Task<IActionResult> UpdateDefaultSalary([FromBody] UpdateDefaultSalaryDto dto)
|
||||
{
|
||||
// FluentValidation auto-validation handles 400 before we get here.
|
||||
|
||||
var setting = await _db.AppSettings
|
||||
.FirstOrDefaultAsync(s => s.Key == "default_salary");
|
||||
|
||||
if (setting is null)
|
||||
{
|
||||
_logger.LogWarning("UpdateDefaultSalary: key 'default_salary' not found in app_settings.");
|
||||
return NotFound(new { error = "Setting 'default_salary' not found." });
|
||||
}
|
||||
|
||||
setting.Value = dto.Value.ToString(System.Globalization.CultureInfo.InvariantCulture);
|
||||
await _db.SaveChangesAsync();
|
||||
|
||||
_logger.LogInformation("Setting 'default_salary' updated to {Value}.", setting.Value);
|
||||
|
||||
return Ok(new SettingsDto(setting.Key, setting.Value));
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user