54 lines
2.5 KiB
C#
54 lines
2.5 KiB
C#
using Helpdesk.Api.Infrastructure;
|
||||
|
|
using Helpdesk.Application.Catalogs;
|
|||
|
|
using Microsoft.AspNetCore.Authorization;
|
|||
|
|
using Microsoft.AspNetCore.Mvc;
|
|||
|
|
|
|||
|
|
namespace Helpdesk.Api.Controllers;
|
|||
|
|
|
|||
|
|
/// <summary>Číselníky pro formuláře (budovy, kategorie závad a návrhů) a jejich správa.</summary>
|
|||
|
|
[ApiController]
|
|||
|
|
[Authorize]
|
|||
|
|
[Route("api")]
|
|||
|
|
[Produces("application/json")]
|
|||
|
|
public sealed class CatalogsController(CatalogService catalogs) : ControllerBase
|
|||
|
|
{
|
|||
|
|
/// <summary>Budovy. Administrátor může přidat <c>includeInactive=true</c> a vidět i deaktivované.</summary>
|
|||
|
|
[HttpGet("buildings")]
|
|||
|
|
public Task<IReadOnlyList<BuildingDto>> GetBuildings([FromQuery] bool includeInactive = false, CancellationToken ct = default) =>
|
|||
|
|
catalogs.GetBuildingsAsync(includeInactive && User.IsInRole(Roles.Admin), ct);
|
|||
|
|
|
|||
|
|
[HttpGet("issue-categories")]
|
|||
|
|
public Task<IReadOnlyList<CategoryDto>> GetIssueCategories(CancellationToken ct) => catalogs.GetIssueCategoriesAsync(ct);
|
|||
|
|
|
|||
|
|
[HttpGet("proposal-categories")]
|
|||
|
|
public Task<IReadOnlyList<CategoryDto>> GetProposalCategories(CancellationToken ct) => catalogs.GetProposalCategoriesAsync(ct);
|
|||
|
|
|
|||
|
|
[Authorize(Roles = Roles.Admin)]
|
|||
|
|
[HttpPost("buildings")]
|
|||
|
|
[ProducesResponseType<BuildingDto>(StatusCodes.Status201Created)]
|
|||
|
|
[ProducesResponseType<ProblemDetails>(StatusCodes.Status409Conflict)]
|
|||
|
|
public async Task<ActionResult<BuildingDto>> CreateBuilding(SaveBuildingRequest request, CancellationToken ct)
|
|||
|
|
{
|
|||
|
|
var building = await catalogs.CreateBuildingAsync(request, ct);
|
|||
|
|
return Created($"/api/buildings/{building.Id}", building);
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
[Authorize(Roles = Roles.Admin)]
|
|||
|
|
[HttpPut("buildings/{id:int}")]
|
|||
|
|
[ProducesResponseType<BuildingDto>(StatusCodes.Status200OK)]
|
|||
|
|
[ProducesResponseType<ProblemDetails>(StatusCodes.Status404NotFound)]
|
|||
|
|
[ProducesResponseType<ProblemDetails>(StatusCodes.Status409Conflict)]
|
|||
|
|
public Task<BuildingDto> UpdateBuilding(int id, SaveBuildingRequest request, CancellationToken ct) =>
|
|||
|
|
catalogs.UpdateBuildingAsync(id, request, ct);
|
|||
|
|
|
|||
|
|
/// <summary>Deaktivace budovy – záznam se nemaže, jen se přestane nabízet ve formulářích.</summary>
|
|||
|
|
[Authorize(Roles = Roles.Admin)]
|
|||
|
|
[HttpDelete("buildings/{id:int}")]
|
|||
|
|
[ProducesResponseType(StatusCodes.Status204NoContent)]
|
|||
|
|
[ProducesResponseType<ProblemDetails>(StatusCodes.Status404NotFound)]
|
|||
|
|
public async Task<IActionResult> DeactivateBuilding(int id, CancellationToken ct)
|
|||
|
|
{
|
|||
|
|
await catalogs.DeactivateBuildingAsync(id, ct);
|
|||
|
|
return NoContent();
|
|||
|
|
}
|
|||
|
|
}
|