shoc-backend/Api.SeaHavenIndustries/Controllers/SettingsController.cs
Alexandre Brandizzi 7d245eb717
refactor: enforce backend boundaries and optimize dispatch (#30)
* refactor(api): enforce service and data-service boundaries

* refactor(api): complete feature service boundaries

* refactor(identity): enforce service and data boundaries

* refactor(vendors): enforce service and data boundaries

* refactor(workorders): enforce service and data boundaries

* refactor(backend): enforce architecture and optimize dispatch

* style(backend): format changed architecture files

* fix(architecture): address backend review follow-ups

* fix(backend): sanitize exception disclosure in changed API endpoints

Replace raw exception-message disclosure (ex.Message) returned to API
callers with a stable sanitized public message plus correlated structured
internal logging, across the endpoints changed in this PR.

- Add SanitizedErrors helper: logs the original exception at Error with a
  generated correlation id and returns a stable public message referencing
  it so support can trace without exposing internals.
- Inject ILogger<T> into the 14 changed controllers and route every
  ex.Message/dbex.Message disclosure through the helper, preserving status
  codes, response shapes, and business data (e.g. OpenWorkOrders).
- Leave FluentValidation (vex.Errors) and existing fixed-message catches
  untouched; out-of-scope controllers (Account/Contact/Employee/Asset/
  PMSchedule) are unchanged.
- Add focused tests proving internal exception text is not returned and
  that Error logging carrying the original exception is invoked.

* fix(architecture): abstract job run state access

* style: format board update service

* test: use collection assertion idiom
2026-07-24 17:35:34 -03:00

172 lines
6.2 KiB
C#

using Api.SeaHavenIndustries.DTOs;
using Api.SeaHavenIndustries.Helper;
using Api.SeaHavenIndustries.Helper.ViewModel;
using Data.SeaHavenIndustries;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Logging;
using SeaHaven.Services.Interfaces;
namespace Api.SeaHavenIndustries.Controllers
{
[Authorize]
[ApiController]
[Route("[controller]")]
public class SettingsController : Controller
{
private readonly ISettingsService _settingsService;
private readonly ILogger<SettingsController> _logger;
public SettingsController(ISettingsService settingsService, ILogger<SettingsController> logger)
{
_settingsService = settingsService;
_logger = logger;
}
[HttpPost]
[Route("AddCategory")]
public async Task<IActionResult> AddCategory(Category_DTO model, CancellationToken cancellationToken)
{
try
{
await _settingsService.AddCategoryAsync(model.Name, cancellationToken);
return Ok(new DataResponse { Message = "Successfully Saved Data", Status = "200" });
}
catch (Exception ex)
{
return BadRequest(new Response { Status = "Error", Message = _logger.Sanitize(ex) });
}
}
[HttpPost]
[Route("EditCategory")]
public async Task<IActionResult> EditCategory(EditCategory_DTO model, CancellationToken cancellationToken)
{
try
{
var found = await _settingsService.UpdateCategoryAsync(model.Id, model.Name, cancellationToken);
if (!found)
return BadRequest(new Response { Status = "Error", Message = "Category not found" });
return Ok(new DataResponse { Message = "Updated Successfully", Status = "200" });
}
catch (Exception ex)
{
return BadRequest(new Response { Status = "Error", Message = _logger.Sanitize(ex) });
}
}
[HttpGet]
[Route("DeleteCategory")]
public async Task<IActionResult> DeleteCategory(int id, CancellationToken cancellationToken)
{
try
{
var found = await _settingsService.DeleteCategoryAsync(id, cancellationToken);
if (!found)
return BadRequest(new Response { Status = "Error", Message = "ID not matched!" });
return Ok(new DataResponse { Message = "Deleted Successfully", Status = "200" });
}
catch (Exception ex)
{
return BadRequest(new Response { Status = "Error", Message = _logger.Sanitize(ex) });
}
}
[HttpGet]
[Route("GetCategoryList")]
public async Task<IActionResult> GetCategoryList(int page = 1, int pageSize = 10, CancellationToken cancellationToken = default)
{
var (items, total) = await _settingsService.GetCategoryListAsync(page, pageSize, cancellationToken);
var totalPages = (int)Math.Ceiling(total / (double)pageSize);
var viewModel = new Pagination_DTO
{
Data = items,
PageNumber = page,
PageSize = pageSize,
TotalCount = total,
TotalPages = totalPages
};
return Ok(viewModel);
}
[HttpGet]
[Route("GetTemplates")]
public async Task<IActionResult> GetTemplates(int page = 1, int pageSize = 10, CancellationToken cancellationToken = default)
{
try
{
var (items, total) = await _settingsService.GetTemplatesAsync(page, pageSize, cancellationToken);
var totalPages = (int)Math.Ceiling(total / (double)pageSize);
var viewModel = new Pagination_DTO
{
Data = items,
PageNumber = page,
PageSize = pageSize,
TotalCount = total,
TotalPages = totalPages
};
return Ok(viewModel);
}
catch (Exception ex)
{
return BadRequest(new Response { Status = "Error", Message = _logger.Sanitize(ex) });
}
}
[HttpGet]
[Route("GetUsers")]
public async Task<IActionResult> GetUsers(string? search = "", int page = 1, int pageSize = 10, CancellationToken cancellationToken = default)
{
try
{
var (users, total) = await _settingsService.GetUsersAsync(search, page, pageSize, cancellationToken);
var filteredUsers = users.Select(u => new UserRoleViewModel
{
RoleName = u.RoleName,
Email = u.Email,
UserName = u.UserName,
Date = u.Date,
Status = u.Status,
Id = u.Id
}).ToList();
var totalPages = (int)Math.Ceiling(total / (double)pageSize);
var viewModel = new Pagination_DTO
{
Data = filteredUsers,
PageNumber = page,
PageSize = pageSize,
TotalCount = total,
TotalPages = totalPages
};
return Ok(viewModel);
}
catch (Exception ex)
{
return BadRequest(new { Status = "Error", Message = _logger.Sanitize(ex) });
}
}
[HttpGet]
[Route("GetRoles")]
public async Task<IActionResult> GetRoles(CancellationToken cancellationToken)
{
try
{
var roles = await _settingsService.GetRolesAsync(cancellationToken);
var result = roles.Select(r => new Roles_DTO { Name = r.Name, Description = r.Description }).ToList();
return Ok(result);
}
catch (Exception ex)
{
return BadRequest(new Response { Status = "Error", Message = _logger.Sanitize(ex) });
}
}
}
}