shoc-backend/Api.SeaHavenIndustries/Controllers/PMScheduleController.cs

148 lines
4.8 KiB
C#
Raw Normal View History

2026-01-21 15:41:19 -06:00
using Api.SeaHavenIndustries.DTOs;
using Data.SeaHavenIndustries;
2026-05-14 11:00:12 -05:00
using FluentValidation;
2026-01-21 15:41:19 -06:00
using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore;
2026-05-06 10:49:33 -05:00
using SeaHaven.Services.Interfaces;
using SeaHaven.Services.DTOs;
2026-05-14 11:00:12 -05:00
using System.Security.Claims;
2026-01-21 15:41:19 -06:00
[ApiController]
[Route("api/PMSchedule")]
[Route("api/pmschedules")]
2026-01-21 15:41:19 -06:00
public class PMScheduleController : ControllerBase
{
2026-05-06 10:49:33 -05:00
private readonly IPMScheduleService _pmScheduleService;
2026-01-21 15:41:19 -06:00
2026-05-06 10:49:33 -05:00
public PMScheduleController(IPMScheduleService pmScheduleService)
2026-01-21 15:41:19 -06:00
{
2026-05-06 10:49:33 -05:00
_pmScheduleService = pmScheduleService;
2026-01-21 15:41:19 -06:00
}
[HttpGet("GetList")]
2026-05-06 10:49:33 -05:00
public async Task<IActionResult> GetList()
2026-01-21 15:41:19 -06:00
{
2026-05-06 10:49:33 -05:00
var items = await _pmScheduleService.GetAllPMSchedulesAsync();
var data = items.Select(x => new
{
Id = x.Id,
Name = x.Name,
Frequency = x.Frequency,
StartDate = x.StartDate,
IsActive = x.IsActive,
Asset = x.AssetName,
Location = x.LocationName
});
2026-01-21 15:41:19 -06:00
return Ok(data);
}
2026-05-14 11:00:12 -05:00
[HttpGet("GetById")]
public async Task<IActionResult> GetById([FromQuery] int id)
{
try
{
var pmSchedule = await _pmScheduleService.GetPMScheduleByIdAsync(id);
if (pmSchedule == null)
return NotFound(new Response { Status = "Error", Message = "PM Schedule not found" });
return Ok(new
{
Id = pmSchedule.Id,
Name = pmSchedule.Name,
Frequency = pmSchedule.Frequency,
StartDate = pmSchedule.StartDate,
IsActive = pmSchedule.IsActive,
AssetId = pmSchedule.AssetId,
LocationId = pmSchedule.LocationId,
Asset = pmSchedule.AssetName,
Location = pmSchedule.LocationName
});
}
catch (KeyNotFoundException)
{
return NotFound(new Response { Status = "Error", Message = "PM Schedule not found" });
}
catch (Exception ex)
{
return StatusCode(500, new Response { Status = "Error", Message = ex.Message });
}
}
2026-01-21 15:41:19 -06:00
[HttpPost("Save")]
public async Task<IActionResult> Save(PMSchedule_DTO dto)
{
2026-05-06 10:49:33 -05:00
try
2026-01-21 15:41:19 -06:00
{
2026-05-14 11:00:12 -05:00
var userId = User.FindFirstValue(ClaimTypes.NameIdentifier);
if (userId == null)
return Unauthorized(new Response { Status = "Error", Message = "User not authenticated" });
2026-05-06 10:49:33 -05:00
if (dto.Id == 0)
2026-01-21 15:41:19 -06:00
{
2026-05-06 10:49:33 -05:00
var createDto = new CreatePMScheduleDTO
{
Name = dto.Name,
Frequency = dto.Frequency,
StartDate = dto.StartDate,
IsActive = dto.IsActive,
AssetId = dto.AssetId,
LocationId = dto.LocationId
};
await _pmScheduleService.CreatePMScheduleAsync(createDto, userId);
}
else
{
var updateDto = new UpdatePMScheduleDTO
{
Name = dto.Name,
Frequency = dto.Frequency,
StartDate = dto.StartDate,
IsActive = dto.IsActive,
AssetId = dto.AssetId,
LocationId = dto.LocationId
};
await _pmScheduleService.UpdatePMScheduleAsync(dto.Id, updateDto, userId);
}
return Ok();
2026-01-21 15:41:19 -06:00
}
2026-05-14 11:00:12 -05:00
catch (ValidationException vex)
{
var errors = string.Join(", ", vex.Errors.Select(e => e.ErrorMessage));
return BadRequest(new Response { Status = "Validation Error", Message = errors });
}
catch (KeyNotFoundException)
{
return NotFound(new Response { Status = "Error", Message = "PM Schedule not found" });
}
catch (Exception ex)
{
return StatusCode(500, new Response { Status = "Error", Message = ex.Message });
}
}
[HttpPost("Delete")]
public async Task<IActionResult> Delete([FromQuery] int id)
{
try
{
var userId = User.FindFirstValue(ClaimTypes.NameIdentifier);
if (userId == null)
return Unauthorized(new Response { Status = "Error", Message = "User not authenticated" });
await _pmScheduleService.DeletePMScheduleAsync(id, userId);
return Ok(new DataResponse { Message = "PM Schedule deleted successfully", Status = "200" });
}
catch (KeyNotFoundException)
{
return NotFound(new Response { Status = "Error", Message = "PM Schedule not found" });
}
2026-05-06 10:49:33 -05:00
catch (Exception ex)
2026-01-21 15:41:19 -06:00
{
2026-05-14 11:00:12 -05:00
return StatusCode(500, new Response { Status = "Error", Message = ex.Message });
2026-01-21 15:41:19 -06:00
}
}
}