mirror of
https://github.com/Sea-Haven-Industries/shoc-backend.git
synced 2026-09-30 07:13:12 +00:00
147 lines
4.8 KiB
C#
147 lines
4.8 KiB
C#
using Api.SeaHavenIndustries.DTOs;
|
|
using Data.SeaHavenIndustries;
|
|
using FluentValidation;
|
|
using Microsoft.AspNetCore.Mvc;
|
|
using Microsoft.EntityFrameworkCore;
|
|
using SeaHaven.Services.Interfaces;
|
|
using SeaHaven.Services.DTOs;
|
|
using System.Security.Claims;
|
|
|
|
[ApiController]
|
|
[Route("api/PMSchedule")]
|
|
[Route("api/pmschedules")]
|
|
public class PMScheduleController : ControllerBase
|
|
{
|
|
private readonly IPMScheduleService _pmScheduleService;
|
|
|
|
public PMScheduleController(IPMScheduleService pmScheduleService)
|
|
{
|
|
_pmScheduleService = pmScheduleService;
|
|
}
|
|
|
|
[HttpGet("GetList")]
|
|
public async Task<IActionResult> GetList()
|
|
{
|
|
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
|
|
});
|
|
|
|
return Ok(data);
|
|
}
|
|
|
|
[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 });
|
|
}
|
|
}
|
|
|
|
[HttpPost("Save")]
|
|
public async Task<IActionResult> Save(PMSchedule_DTO dto)
|
|
{
|
|
try
|
|
{
|
|
var userId = User.FindFirstValue(ClaimTypes.NameIdentifier);
|
|
if (userId == null)
|
|
return Unauthorized(new Response { Status = "Error", Message = "User not authenticated" });
|
|
|
|
if (dto.Id == 0)
|
|
{
|
|
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();
|
|
}
|
|
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" });
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
return StatusCode(500, new Response { Status = "Error", Message = ex.Message });
|
|
}
|
|
}
|
|
}
|