127 lines
4.2 KiB
C#
127 lines
4.2 KiB
C#
using Distribution.Infra.Authorization;
|
|
using Microsoft.AspNetCore.Authorization;
|
|
using Microsoft.AspNetCore.Mvc;
|
|
|
|
namespace Distribution.Features.Region;
|
|
|
|
[ApiController]
|
|
[Route("api/regions")]
|
|
public class RegionController(
|
|
RegionService service,
|
|
IScopeAccessService scopeAccess,
|
|
ICurrentUserService currentUser) : ControllerBase
|
|
{
|
|
[HttpGet]
|
|
[Authorize("region.view")]
|
|
public async Task<IActionResult> GetAll() =>
|
|
Ok(await service.GetAllAsync());
|
|
|
|
[HttpGet("next-code")]
|
|
[Authorize("region.view")]
|
|
public async Task<IActionResult> GetNextCode() =>
|
|
Ok(await service.GetNextCodeAsync());
|
|
|
|
[HttpGet("{id:guid}")]
|
|
[Authorize("region.view")]
|
|
public async Task<IActionResult> GetById(Guid id)
|
|
{
|
|
var userId = currentUser.UserId;
|
|
if (userId.HasValue && !await scopeAccess.HasRegionScopeAsync(userId.Value, id))
|
|
return StatusCode(403);
|
|
var result = await service.GetByIdAsync(id);
|
|
return result is null ? NotFound() : Ok(result);
|
|
}
|
|
|
|
[HttpPost]
|
|
[Authorize("region.manage")]
|
|
public async Task<IActionResult> Create(CreateRegionRequest request, CancellationToken ct)
|
|
{
|
|
var result = await service.CreateAsync(request, ct);
|
|
return CreatedAtAction(nameof(GetById), new { id = result.Id }, result);
|
|
}
|
|
|
|
[HttpPut("{id:guid}")]
|
|
[Authorize("region.manage")]
|
|
public async Task<IActionResult> Update(Guid id, UpdateRegionRequest request, CancellationToken ct)
|
|
{
|
|
var result = await service.UpdateAsync(id, request, ct);
|
|
return result is null ? NotFound() : Ok(result);
|
|
}
|
|
|
|
[HttpDelete("{id:guid}")]
|
|
[Authorize("region.manage")]
|
|
public async Task<IActionResult> Delete(Guid id, CancellationToken ct)
|
|
{
|
|
var deleted = await service.DeleteAsync(id, ct);
|
|
return deleted ? NoContent() : NotFound();
|
|
}
|
|
|
|
[HttpGet("{id:guid}/manager")]
|
|
[Authorize("region.view")]
|
|
public async Task<IActionResult> GetManager(Guid id)
|
|
{
|
|
var result = await service.GetManagerAsync(id);
|
|
return result is null ? NotFound() : Ok(result);
|
|
}
|
|
|
|
[HttpPut("{id:guid}/manager")]
|
|
[Authorize("region.manage")]
|
|
public async Task<IActionResult> SetManager(Guid id, SetRegionManagerRequest request, CancellationToken ct)
|
|
{
|
|
try
|
|
{
|
|
var result = await service.SetManagerAsync(id, request, ct);
|
|
return result is null ? NotFound() : Ok(result);
|
|
}
|
|
catch (InvalidOperationException ex)
|
|
{
|
|
return BadRequest(new { error = ex.Message });
|
|
}
|
|
}
|
|
|
|
[HttpDelete("{id:guid}/manager")]
|
|
[Authorize("region.manage")]
|
|
public async Task<IActionResult> RemoveManager(Guid id, CancellationToken ct)
|
|
{
|
|
var removed = await service.RemoveManagerAsync(id, ct);
|
|
return removed ? NoContent() : NotFound();
|
|
}
|
|
|
|
[HttpPut("{id:guid}/manage")]
|
|
[Authorize("region.manage")]
|
|
public async Task<IActionResult> Manage(Guid id, ManageRegionRequest request, CancellationToken ct)
|
|
{
|
|
var result = await service.ManageAsync(id, request, ct);
|
|
return result is null ? NotFound() : Ok(result);
|
|
}
|
|
|
|
[HttpGet("{id:guid}/hierarchy")]
|
|
[Authorize("region.view")]
|
|
public async Task<IActionResult> GetHierarchy(Guid id)
|
|
{
|
|
var result = await service.GetHierarchyAsync(id);
|
|
return result is null ? NotFound() : Ok(result);
|
|
}
|
|
|
|
[HttpGet("{id:guid}/dashboard")]
|
|
[Authorize("region.view")]
|
|
public async Task<IActionResult> GetDashboard(Guid id, [FromQuery] string period)
|
|
{
|
|
var result = await service.GetDashboardAsync(id, period);
|
|
return result is null ? NotFound() : Ok(result);
|
|
}
|
|
|
|
[HttpGet("manage-entries")]
|
|
[Authorize("region.view")]
|
|
public async Task<IActionResult> GetManageEntries() =>
|
|
Ok(await service.GetManageEntriesAsync());
|
|
|
|
[HttpDelete("manage-entries/{entryType}/{id:guid}")]
|
|
[Authorize("region.manage")]
|
|
public async Task<IActionResult> DeleteManageEntry(RegionManageEntryType entryType, Guid id, CancellationToken ct)
|
|
{
|
|
var deleted = await service.DeleteManageEntryAsync(entryType, id, ct);
|
|
return deleted ? NoContent() : NotFound();
|
|
}
|
|
}
|