mirror of
https://github.com/Sea-Haven-Industries/shoc-backend.git
synced 2026-09-30 06:03:12 +00:00
110 lines
4.3 KiB
C#
110 lines
4.3 KiB
C#
using Data.SeaHavenIndustries.Enums;
|
|
using Microsoft.AspNetCore.Authorization;
|
|
using Microsoft.AspNetCore.Mvc;
|
|
using SeaHaven.Services.DTOs;
|
|
using SeaHaven.Services.Exceptions;
|
|
using SeaHaven.Services.Interfaces;
|
|
|
|
namespace Api.SeaHavenIndustries.Controllers
|
|
{
|
|
[Authorize]
|
|
[ApiController]
|
|
[Route("api/services")]
|
|
public class ServicesRegistryController : Controller
|
|
{
|
|
private readonly IServicesRegistryService _servicesRegistryService;
|
|
|
|
public ServicesRegistryController(IServicesRegistryService servicesRegistryService)
|
|
{
|
|
_servicesRegistryService = servicesRegistryService;
|
|
}
|
|
|
|
[HttpGet]
|
|
public async Task<IActionResult> GetAll([FromQuery] bool? isActive, [FromQuery] WorkOrderType? workOrderType, CancellationToken cancellationToken)
|
|
{
|
|
try
|
|
{
|
|
var services = await _servicesRegistryService.GetAllAsync(isActive, workOrderType, cancellationToken);
|
|
return Ok(services);
|
|
}
|
|
catch (ServicesRegistryValidationException ex)
|
|
{
|
|
return BadRequest(new ServicesRegistryValidationErrorDto { Code = ex.Code, Message = ex.Message });
|
|
}
|
|
}
|
|
|
|
[HttpGet("{id}")]
|
|
public async Task<IActionResult> GetById(int id, CancellationToken cancellationToken)
|
|
{
|
|
var service = await _servicesRegistryService.GetByIdAsync(id, cancellationToken);
|
|
if (service == null)
|
|
return NotFound(new ServicesRegistryValidationErrorDto { Code = "NotFound", Message = "Service not found." });
|
|
|
|
return Ok(service);
|
|
}
|
|
|
|
[HttpPost]
|
|
public async Task<IActionResult> Create([FromBody] ServiceInput input, CancellationToken cancellationToken)
|
|
{
|
|
try
|
|
{
|
|
var created = await _servicesRegistryService.CreateAsync(User, input, cancellationToken);
|
|
return StatusCode(StatusCodes.Status201Created, created);
|
|
}
|
|
catch (ServicesRegistryValidationException ex) when (ex.Code == "Forbidden")
|
|
{
|
|
return MapForbidden(ex);
|
|
}
|
|
catch (ServicesRegistryValidationException ex)
|
|
{
|
|
return BadRequest(new ServicesRegistryValidationErrorDto { Code = ex.Code, Message = ex.Message });
|
|
}
|
|
}
|
|
|
|
[HttpPut("{id}")]
|
|
public async Task<IActionResult> Update(int id, [FromBody] ServiceInput input, CancellationToken cancellationToken)
|
|
{
|
|
try
|
|
{
|
|
var updated = await _servicesRegistryService.UpdateAsync(User, id, input, cancellationToken);
|
|
return Ok(updated);
|
|
}
|
|
catch (ServicesRegistryValidationException ex) when (ex.Code == "NotFound")
|
|
{
|
|
return NotFound(new ServicesRegistryValidationErrorDto { Code = ex.Code, Message = ex.Message });
|
|
}
|
|
catch (ServicesRegistryValidationException ex) when (ex.Code == "Forbidden")
|
|
{
|
|
return MapForbidden(ex);
|
|
}
|
|
catch (ServicesRegistryValidationException ex)
|
|
{
|
|
return BadRequest(new ServicesRegistryValidationErrorDto { Code = ex.Code, Message = ex.Message });
|
|
}
|
|
}
|
|
|
|
[HttpPost("{id}/deactivate")]
|
|
public async Task<IActionResult> Deactivate(int id, CancellationToken cancellationToken)
|
|
{
|
|
try
|
|
{
|
|
await _servicesRegistryService.DeactivateAsync(User, id, cancellationToken);
|
|
return Ok(new { message = "Service deactivated" });
|
|
}
|
|
catch (ServicesRegistryValidationException ex) when (ex.Code == "NotFound")
|
|
{
|
|
return NotFound(new ServicesRegistryValidationErrorDto { Code = ex.Code, Message = ex.Message });
|
|
}
|
|
catch (ServicesRegistryValidationException ex) when (ex.Code == "Forbidden")
|
|
{
|
|
return MapForbidden(ex);
|
|
}
|
|
}
|
|
|
|
private IActionResult MapForbidden(ServicesRegistryValidationException ex)
|
|
{
|
|
return StatusCode(StatusCodes.Status403Forbidden,
|
|
new ServicesRegistryValidationErrorDto { Code = ex.Code, Message = ex.Message });
|
|
}
|
|
}
|
|
}
|