95 lines
3.3 KiB
C#
95 lines
3.3 KiB
C#
using HelpDesk.Data.Repositories;
|
|
using HelpDesk.Models.DTOs;
|
|
using HelpDesk.Models.Entities;
|
|
using Microsoft.AspNetCore.Authorization;
|
|
using Microsoft.AspNetCore.Mvc;
|
|
|
|
namespace HelpDesk.API.Controllers;
|
|
|
|
[ApiController]
|
|
[Route("api/[controller]")]
|
|
[Authorize(Policy = "Admin")]
|
|
public class SLAController : ControllerBase
|
|
{
|
|
private readonly ISLARepository _slaRepository;
|
|
|
|
public SLAController(ISLARepository slaRepository)
|
|
{
|
|
_slaRepository = slaRepository;
|
|
}
|
|
|
|
[HttpGet]
|
|
public async Task<ActionResult<IEnumerable<SLADto>>> GetSLAPolicies()
|
|
{
|
|
var policies = await _slaRepository.GetAllAsync();
|
|
return Ok(policies.Select(MapToDto));
|
|
}
|
|
|
|
[HttpGet("{id}")]
|
|
public async Task<ActionResult<SLADto>> GetSLAPolicy(Guid id)
|
|
{
|
|
var policy = await _slaRepository.GetByIdAsync(id);
|
|
if (policy == null) return NotFound(new { error = "SLA policy not found." });
|
|
return Ok(MapToDto(policy));
|
|
}
|
|
|
|
[HttpPost]
|
|
public async Task<ActionResult<SLADto>> CreateSLAPolicy([FromBody] CreateSLARequest request)
|
|
{
|
|
if (string.IsNullOrWhiteSpace(request.Name))
|
|
return BadRequest(new { error = "Name is required." });
|
|
if (request.ResponseTimeHours <= 0 || request.ResolutionTimeHours <= 0)
|
|
return BadRequest(new { error = "Response and resolution times must be positive." });
|
|
|
|
var policy = new SLAPolicy
|
|
{
|
|
Name = request.Name,
|
|
Description = request.Description,
|
|
ResponseTimeHours = request.ResponseTimeHours,
|
|
ResolutionTimeHours = request.ResolutionTimeHours,
|
|
IsActive = true,
|
|
CreatedAt = DateTime.UtcNow
|
|
};
|
|
|
|
var created = await _slaRepository.CreateAsync(policy);
|
|
return CreatedAtAction(nameof(GetSLAPolicy), new { id = created.Id }, MapToDto(created));
|
|
}
|
|
|
|
[HttpPut("{id}")]
|
|
public async Task<ActionResult<SLADto>> UpdateSLAPolicy(Guid id, [FromBody] UpdateSLARequest request)
|
|
{
|
|
var policy = await _slaRepository.GetByIdAsync(id);
|
|
if (policy == null) return NotFound(new { error = "SLA policy not found." });
|
|
|
|
if (request.Name != null) policy.Name = request.Name;
|
|
if (request.Description != null) policy.Description = request.Description;
|
|
if (request.ResponseTimeHours.HasValue) policy.ResponseTimeHours = request.ResponseTimeHours.Value;
|
|
if (request.ResolutionTimeHours.HasValue) policy.ResolutionTimeHours = request.ResolutionTimeHours.Value;
|
|
if (request.IsActive.HasValue) policy.IsActive = request.IsActive.Value;
|
|
|
|
var updated = await _slaRepository.UpdateAsync(policy);
|
|
if (updated == null) return StatusCode(500, new { error = "Failed to update SLA policy." });
|
|
|
|
return Ok(MapToDto(updated));
|
|
}
|
|
|
|
[HttpDelete("{id}")]
|
|
public async Task<IActionResult> DeleteSLAPolicy(Guid id)
|
|
{
|
|
var deleted = await _slaRepository.DeleteAsync(id);
|
|
if (!deleted) return NotFound(new { error = "SLA policy not found." });
|
|
return NoContent();
|
|
}
|
|
|
|
private static SLADto MapToDto(SLAPolicy s) => new()
|
|
{
|
|
Id = s.Id,
|
|
Name = s.Name,
|
|
Description = s.Description,
|
|
ResponseTimeHours = s.ResponseTimeHours,
|
|
ResolutionTimeHours = s.ResolutionTimeHours,
|
|
IsActive = s.IsActive,
|
|
CreatedAt = s.CreatedAt
|
|
};
|
|
}
|