2026-04-17 14:57:18 -04:00
|
|
|
using Api.SeaHavenIndustries.DTOs;
|
2026-07-24 17:35:34 -03:00
|
|
|
using Api.SeaHavenIndustries.Helper;
|
2026-04-17 14:57:18 -04:00
|
|
|
using Data.SeaHavenIndustries;
|
|
|
|
|
using Microsoft.AspNetCore.Authorization;
|
|
|
|
|
using Microsoft.AspNetCore.Mvc;
|
2026-07-24 17:35:34 -03:00
|
|
|
using Microsoft.Extensions.Logging;
|
|
|
|
|
using SeaHaven.Services.DTOs;
|
|
|
|
|
using SeaHaven.Services.Interfaces;
|
2026-04-17 14:57:18 -04:00
|
|
|
|
|
|
|
|
namespace Api.SeaHavenIndustries.Controllers
|
|
|
|
|
{
|
|
|
|
|
[Authorize]
|
|
|
|
|
[ApiController]
|
|
|
|
|
[Route("api/[controller]")]
|
|
|
|
|
[Route("api/tasklisttemplates")]
|
|
|
|
|
public class TaskListTemplateController : Controller
|
|
|
|
|
{
|
2026-07-24 17:35:34 -03:00
|
|
|
private readonly ITaskListTemplateService _taskListTemplateService;
|
|
|
|
|
private readonly ILogger<TaskListTemplateController> _logger;
|
2026-04-17 14:57:18 -04:00
|
|
|
|
2026-07-24 17:35:34 -03:00
|
|
|
public TaskListTemplateController(ITaskListTemplateService taskListTemplateService, ILogger<TaskListTemplateController> logger)
|
2026-04-17 14:57:18 -04:00
|
|
|
{
|
2026-07-24 17:35:34 -03:00
|
|
|
_taskListTemplateService = taskListTemplateService;
|
|
|
|
|
_logger = logger;
|
2026-04-17 14:57:18 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[HttpGet]
|
2026-07-24 17:35:34 -03:00
|
|
|
public async Task<IActionResult> GetAll(CancellationToken cancellationToken)
|
2026-04-17 14:57:18 -04:00
|
|
|
{
|
2026-07-24 17:35:34 -03:00
|
|
|
var data = await _taskListTemplateService.GetAllAsync(cancellationToken);
|
2026-04-17 14:57:18 -04:00
|
|
|
return Ok(data);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[HttpGet("{id}")]
|
2026-07-24 17:35:34 -03:00
|
|
|
public async Task<IActionResult> GetById(int id, CancellationToken cancellationToken)
|
2026-04-17 14:57:18 -04:00
|
|
|
{
|
2026-07-24 17:35:34 -03:00
|
|
|
var template = await _taskListTemplateService.GetByIdAsync(id, cancellationToken);
|
2026-04-17 14:57:18 -04:00
|
|
|
|
|
|
|
|
if (template == null)
|
|
|
|
|
return NotFound(new Response { Status = "Error", Message = "Template not found" });
|
|
|
|
|
|
|
|
|
|
return Ok(template);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[HttpPost]
|
2026-07-24 17:35:34 -03:00
|
|
|
public async Task<IActionResult> Create([FromBody] TaskListTemplate_DTO model, CancellationToken cancellationToken)
|
2026-04-17 14:57:18 -04:00
|
|
|
{
|
|
|
|
|
try
|
|
|
|
|
{
|
2026-07-24 17:35:34 -03:00
|
|
|
var input = MapToInput(model);
|
|
|
|
|
var id = await _taskListTemplateService.CreateAsync(input, cancellationToken);
|
|
|
|
|
return Ok(new { message = "Template created", id });
|
2026-04-17 14:57:18 -04:00
|
|
|
}
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
{
|
2026-07-24 17:35:34 -03:00
|
|
|
return BadRequest(new Response { Status = "Error", Message = _logger.Sanitize(ex) });
|
2026-04-17 14:57:18 -04:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[HttpPut("{id}")]
|
2026-07-24 17:35:34 -03:00
|
|
|
public async Task<IActionResult> Update(int id, [FromBody] TaskListTemplate_DTO model, CancellationToken cancellationToken)
|
2026-04-17 14:57:18 -04:00
|
|
|
{
|
|
|
|
|
try
|
|
|
|
|
{
|
2026-07-24 17:35:34 -03:00
|
|
|
var input = MapToInput(model);
|
|
|
|
|
var updated = await _taskListTemplateService.UpdateAsync(id, input, cancellationToken);
|
|
|
|
|
if (!updated)
|
2026-04-17 14:57:18 -04:00
|
|
|
return NotFound(new Response { Status = "Error", Message = "Template not found" });
|
|
|
|
|
|
|
|
|
|
return Ok(new { message = "Template updated" });
|
|
|
|
|
}
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
{
|
2026-07-24 17:35:34 -03:00
|
|
|
return BadRequest(new Response { Status = "Error", Message = _logger.Sanitize(ex) });
|
2026-04-17 14:57:18 -04:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[HttpDelete("{id}")]
|
2026-07-24 17:35:34 -03:00
|
|
|
public async Task<IActionResult> Delete(int id, CancellationToken cancellationToken)
|
2026-04-17 14:57:18 -04:00
|
|
|
{
|
2026-07-24 17:35:34 -03:00
|
|
|
var deleted = await _taskListTemplateService.DeleteAsync(id, cancellationToken);
|
|
|
|
|
if (!deleted)
|
2026-04-17 14:57:18 -04:00
|
|
|
return NotFound(new Response { Status = "Error", Message = "Template not found" });
|
|
|
|
|
|
|
|
|
|
return Ok(new { message = "Template deleted" });
|
|
|
|
|
}
|
2026-07-24 17:35:34 -03:00
|
|
|
|
|
|
|
|
private static TaskListTemplateInput MapToInput(TaskListTemplate_DTO model)
|
|
|
|
|
{
|
|
|
|
|
return new TaskListTemplateInput
|
|
|
|
|
{
|
|
|
|
|
Name = model.Name,
|
|
|
|
|
Description = model.Description,
|
|
|
|
|
IsActive = model.IsActive,
|
|
|
|
|
Items = model.Items.Select(item => new TaskListTemplateInputItem
|
|
|
|
|
{
|
|
|
|
|
ItemText = item.ItemText,
|
|
|
|
|
SortOrder = item.SortOrder
|
|
|
|
|
}).ToList()
|
|
|
|
|
};
|
|
|
|
|
}
|
2026-04-17 14:57:18 -04:00
|
|
|
}
|
|
|
|
|
}
|