shoc-backend/Api.SeaHavenIndustries/Controllers/PMScheduleController.cs
2026-01-21 15:41:19 -06:00

69 lines
1.7 KiB
C#

using Api.SeaHavenIndustries.DTOs;
using Data.SeaHavenIndustries;
using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore;
[ApiController]
[Route("api/PMSchedule")]
public class PMScheduleController : ControllerBase
{
private readonly ApplicationDbContext _db;
public PMScheduleController(ApplicationDbContext db)
{
_db = db;
}
[HttpGet("GetList")]
public IActionResult GetList()
{
var data = _db.PMSchedules
.Include(x => x.Asset)
.Include(x => x.Location)
.OrderByDescending(x => x.Id)
.Select(x => new
{
x.Id,
x.Name,
x.Frequency,
x.StartDate,
x.IsActive,
Asset = x.Asset != null ? x.Asset.Name : null,
Location = x.Location != null ? x.Location.Name : null
})
.ToList();
return Ok(data);
}
[HttpPost("Save")]
public async Task<IActionResult> Save(PMSchedule_DTO dto)
{
PMSchedules model;
if (dto.Id == 0)
{
model = new PMSchedules
{
CreatedDate = DateTime.UtcNow
};
_db.PMSchedules.Add(model);
}
else
{
model = await _db.PMSchedules.FindAsync(dto.Id);
if (model == null) return NotFound();
model.LastModificationTime = DateTime.UtcNow;
}
model.Name = dto.Name;
model.Frequency = dto.Frequency;
model.StartDate = dto.StartDate;
model.IsActive = dto.IsActive;
model.AssetId = dto.AssetId;
model.LocationId = dto.LocationId;
await _db.SaveChangesAsync();
return Ok();
}
}