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

81 lines
2.3 KiB
C#
Raw Normal View History

2026-01-21 15:41:19 -06:00
using Api.SeaHavenIndustries.DTOs;
using Data.SeaHavenIndustries;
using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore;
2026-05-06 10:49:33 -05:00
using SeaHaven.Services.Interfaces;
using SeaHaven.Services.DTOs;
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);
}
[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-06 10:49:33 -05:00
var userId = User.Identity?.Name ?? "system";
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-06 10:49:33 -05:00
catch (Exception ex)
2026-01-21 15:41:19 -06:00
{
2026-05-06 10:49:33 -05:00
return BadRequest(new { Message = ex.Message });
2026-01-21 15:41:19 -06:00
}
}
}