hrms-api-backend/BMA.EHR.Insignia.Service/Controllers/InsigniaPeriodController.cs
2023-07-19 10:25:54 +07:00

150 lines
No EOL
5.3 KiB
C#

using System.Security.Claims;
using BMA.EHR.Application.Repositories;
using BMA.EHR.Application.Requests;
using BMA.EHR.Domain.Common;
using BMA.EHR.Domain.Models.Insignias;
using BMA.EHR.Domain.Shared;
using BMA.EHR.Infrastructure.Persistence;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using Swashbuckle.AspNetCore.Annotations;
namespace BMA.EHR.Insignia.Service.Controllers
{
[Route("api/v{version:apiVersion}/insignia/period")]
[ApiVersion("1.0")]
[ApiController]
[Produces("application/json")]
[Authorize]
[SwaggerTag("รอบเครื่องราช")]
public class InsigniaPeriodController : BaseController
{
private readonly ApplicationDBContext _context;
private readonly MinIOService _documentService;
private readonly IHttpContextAccessor _httpContextAccessor;
private readonly InsigniaPeriodsRepository _repository;
public InsigniaPeriodController(ApplicationDBContext context,
MinIOService documentService,
InsigniaPeriodsRepository repository,
IHttpContextAccessor httpContextAccessor)
{
_context = context;
_documentService = documentService;
_repository = repository;
_httpContextAccessor = httpContextAccessor;
}
#region " Properties "
private string? UserId => _httpContextAccessor?.HttpContext?.User?.FindFirst(ClaimTypes.NameIdentifier)?.Value;
private string? FullName => _httpContextAccessor?.HttpContext?.User?.FindFirst("name")?.Value;
#endregion
[HttpGet("{type}")]
public async Task<ActionResult<ResponseObject>> GetList(string type)
{
var data = _context.InsigniaPeriods.AsQueryable()
.Where(x => x.Type == type)
.OrderByDescending(x => x.Year)
.ThenByDescending(x => x.StartDate)
.Select(p => new
{
period_id = p.Id,
period_amount = p.Amount,
period_name = p.Name,
period_start = p.StartDate,
period_end = p.EndDate,
period_status = _repository.CalStatusByDate(p.StartDate, p.EndDate, p.Year.ToString()),
period_year = p.Year
})
.ToList();
return Success(data);
}
[HttpGet("{id:length(36)}")]
public async Task<ActionResult<ResponseObject>> GetById(Guid id)
{
var data = _context.InsigniaPeriods.AsQueryable()
.Where(x => x.Id == id)
.Select(p => new
{
period_id = p.Id,
period_amount = p.Amount,
period_name = p.Name,
period_start = p.StartDate,
period_end = p.EndDate,
period_status = _repository.CalStatusByDate(p.StartDate, p.EndDate, p.Year.ToString()),
period_year = p.Year,
docs = new List<dynamic>()
})
.FirstOrDefault();
return Success(data);
}
[HttpPost("{type}")]
public async Task<ActionResult<ResponseObject>> Post([FromBody] InsigniaPeriodRequest req, string type)
{
if (req == null)
return BadRequest();
var period = new InsigniaPeriod
{
Name = req.Name,
Year = req.Year,
StartDate = req.StartDate,
EndDate = req.EndDate,
Amount = req.Amount,
Type = type
};
_context.InsigniaPeriods.Add(period);
_context.SaveChanges();
return Success();
}
[HttpDelete("{id:length(36)}")]
public async Task<ActionResult<ResponseObject>> Delete(Guid id)
{
var deleted = _context.InsigniaPeriods.AsQueryable()
.FirstOrDefault(x => x.Id == id);
if (deleted == null)
return NotFound();
_context.InsigniaPeriods.Remove(deleted);
_context.SaveChanges();
return Success();
}
[HttpPut("{type}/{id:length(36)}")]
public async Task<ActionResult<ResponseObject>> Put([FromBody] InsigniaPeriodRequest req, Guid id, string type)
{
if (req == null)
return BadRequest();
var uppdated = _context.InsigniaPeriods.AsQueryable()
.FirstOrDefault(x => x.Id == id);
if (uppdated == null)
return NotFound();
uppdated.Name = req.Name;
uppdated.Year = req.Year;
uppdated.StartDate = req.StartDate;
uppdated.EndDate = req.EndDate;
uppdated.Amount = req.Amount;
uppdated.Type = type;
_context.SaveChanges();
return Success();
}
}
}