225 lines
11 KiB
C#
225 lines
11 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 Microsoft.EntityFrameworkCore;
|
|
using Newtonsoft.Json;
|
|
using Swashbuckle.AspNetCore.Annotations;
|
|
|
|
namespace BMA.EHR.Insignia.Service.Controllers
|
|
{
|
|
[Route("api/v{version:apiVersion}/insignia/manage")]
|
|
[ApiVersion("1.0")]
|
|
[ApiController]
|
|
[Produces("application/json")]
|
|
[Authorize]
|
|
[SwaggerTag("จัดสรรเครื่องราช")]
|
|
public class InsigniaManageController : BaseController
|
|
{
|
|
private readonly ApplicationDBContext _context;
|
|
private readonly MinIOService _documentService;
|
|
private readonly IHttpContextAccessor _httpContextAccessor;
|
|
private readonly InsigniaPeriodsRepository _repository;
|
|
private readonly string Royal_Type = "Royal";
|
|
|
|
public InsigniaManageController(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
|
|
|
|
/// <summary>
|
|
/// list จัดสรรเครื่องราช
|
|
/// </summary>
|
|
/// <param name="type">ประเภทเครื่องราช(insignia=เครื่องราช,coin=เหรียญ)</param>
|
|
/// <returns></returns>
|
|
/// <response code="200"></response>
|
|
/// <response code="400">ค่าตัวแปรที่ส่งมาไม่ถูกต้อง</response>
|
|
/// <response code="401">ไม่ได้ Login เข้าระบบ</response>
|
|
/// <response code="500">เมื่อเกิดข้อผิดพลาดในการทำงาน</response>
|
|
[HttpGet("{type}")]
|
|
public async Task<ActionResult<ResponseObject>> GetList(string type)
|
|
{
|
|
var data = await _context.InsigniaManages.AsQueryable()
|
|
.Where(x => x.Type == type.Trim().ToUpper())
|
|
.OrderByDescending(x => x.Year)
|
|
.Select(p => new
|
|
{
|
|
Id = p.Id,
|
|
Insignia = p.Insignia.Name,
|
|
InsigniaId = p.Insignia.Id,
|
|
Year = p.Year,
|
|
Total = p.Total,
|
|
Type = p.Type,
|
|
LastUpdatedAt = p.LastUpdatedAt,
|
|
CreatedAt = p.CreatedAt,
|
|
})
|
|
.ToListAsync();
|
|
return Success(data);
|
|
}
|
|
|
|
/// <summary>
|
|
/// get รายละเอียดจัดสรรเครื่องราช
|
|
/// </summary>
|
|
/// <param name="id">Id จัดสรรเครื่องราช</param>
|
|
/// <param name="type">ประเภทเครื่องราช(insignia=เครื่องราช,coin=เหรียญ)</param>
|
|
/// <returns></returns>
|
|
/// <response code="200"></response>
|
|
/// <response code="400">ค่าตัวแปรที่ส่งมาไม่ถูกต้อง</response>
|
|
/// <response code="401">ไม่ได้ Login เข้าระบบ</response>
|
|
/// <response code="500">เมื่อเกิดข้อผิดพลาดในการทำงาน</response>
|
|
[HttpGet("{type}/{id:length(36)}")]
|
|
public async Task<ActionResult<ResponseObject>> GetById(string type, Guid id)
|
|
{
|
|
var data = await _context.InsigniaManages.AsQueryable()
|
|
.Where(x => x.Type == type.Trim().ToUpper())
|
|
.Where(x => x.Id == id)
|
|
.Select(p => new
|
|
{
|
|
Id = p.Id,
|
|
Insignia = p.Insignia.Name,
|
|
InsigniaId = p.Insignia.Id,
|
|
Year = p.Year,
|
|
Total = p.Total,
|
|
Type = p.Type,
|
|
LastUpdatedAt = p.LastUpdatedAt,
|
|
CreatedAt = p.CreatedAt,
|
|
})
|
|
.FirstOrDefaultAsync();
|
|
if (data == null)
|
|
return Error(GlobalMessages.InsigniaManageNotFound, 404);
|
|
|
|
return Success(data);
|
|
}
|
|
|
|
/// <summary>
|
|
/// สร้างจัดสรรเครื่องราช
|
|
/// </summary>
|
|
/// <param name="type">ประเภทเครื่องราช(insignia=เครื่องราช,coin=เหรียญ)</param>
|
|
/// <returns></returns>
|
|
/// <response code="200"></response>
|
|
/// <response code="400">ค่าตัวแปรที่ส่งมาไม่ถูกต้อง</response>
|
|
/// <response code="401">ไม่ได้ Login เข้าระบบ</response>
|
|
/// <response code="500">เมื่อเกิดข้อผิดพลาดในการทำงาน</response>
|
|
[HttpPost("{type}")]
|
|
public async Task<ActionResult<ResponseObject>> Post([FromForm] InsigniaManageRequest req, string type)
|
|
{
|
|
var insignia = await _context.Insignias.AsQueryable()
|
|
.FirstOrDefaultAsync(x => x.Id == req.Insignia);
|
|
if (insignia == null)
|
|
return Error(GlobalMessages.InsigniaNotFound);
|
|
|
|
var insigniaManage = await _context.InsigniaManages.AsQueryable()
|
|
.Where(x => x.Insignia == insignia && x.Year == req.Year)
|
|
.FirstOrDefaultAsync();
|
|
if (insigniaManage != null)
|
|
return Error(GlobalMessages.InsigniaManageNotFound);
|
|
|
|
var period = new InsigniaManage
|
|
{
|
|
Insignia = insignia,
|
|
Year = req.Year,
|
|
Total = req.Total,
|
|
Type = type.Trim().ToUpper(),
|
|
CreatedUserId = FullName ?? "",
|
|
CreatedFullName = UserId ?? "System Administrator",
|
|
CreatedAt = DateTime.Now,
|
|
LastUpdateFullName = FullName ?? "System Administrator",
|
|
LastUpdateUserId = UserId ?? "",
|
|
LastUpdatedAt = DateTime.Now,
|
|
};
|
|
await _context.InsigniaManages.AddAsync(period);
|
|
await _context.SaveChangesAsync();
|
|
|
|
return Success();
|
|
}
|
|
|
|
/// <summary>
|
|
/// ลบจัดสรรเครื่องราช
|
|
/// </summary>
|
|
/// <param name="id">Id จัดสรรเครื่องราช</param>
|
|
/// <param name="type">ประเภทเครื่องราช(insignia=เครื่องราช,coin=เหรียญ)</param>
|
|
/// <returns></returns>
|
|
/// <response code="200"></response>
|
|
/// <response code="400">ค่าตัวแปรที่ส่งมาไม่ถูกต้อง</response>
|
|
/// <response code="401">ไม่ได้ Login เข้าระบบ</response>
|
|
/// <response code="500">เมื่อเกิดข้อผิดพลาดในการทำงาน</response>
|
|
[HttpDelete("{type}/{id:length(36)}")]
|
|
public async Task<ActionResult<ResponseObject>> Delete(Guid id, string type)
|
|
{
|
|
var deleted = await _context.InsigniaManages.AsQueryable()
|
|
.Where(x => x.Type == type.Trim().ToUpper())
|
|
.Where(x => x.Id == id)
|
|
.FirstOrDefaultAsync();
|
|
|
|
if (deleted == null)
|
|
return Error(GlobalMessages.InsigniaManageNotFound);
|
|
_context.InsigniaManages.Remove(deleted);
|
|
await _context.SaveChangesAsync();
|
|
|
|
return Success();
|
|
}
|
|
|
|
/// <summary>
|
|
/// แก้ไขจัดสรรเครื่องราช
|
|
/// </summary>
|
|
/// <param name="id">Id จัดสรรเครื่องราช</param>
|
|
/// <param name="type">ประเภทเครื่องราช(insignia=เครื่องราช,coin=เหรียญ)</param>
|
|
/// <returns></returns>
|
|
/// <response code="200"></response>
|
|
/// <response code="400">ค่าตัวแปรที่ส่งมาไม่ถูกต้อง</response>
|
|
/// <response code="401">ไม่ได้ Login เข้าระบบ</response>
|
|
/// <response code="500">เมื่อเกิดข้อผิดพลาดในการทำงาน</response>
|
|
[HttpPut("{type}/{id:length(36)}")]
|
|
public async Task<ActionResult<ResponseObject>> Put([FromForm] InsigniaManageRequest req, Guid id, string type)
|
|
{
|
|
var insignia = await _context.Insignias.AsQueryable()
|
|
.FirstOrDefaultAsync(x => x.Id == req.Insignia);
|
|
if (insignia == null)
|
|
return Error(GlobalMessages.InsigniaNotFound);
|
|
|
|
var insigniaManage = await _context.InsigniaManages.AsQueryable()
|
|
.Where(x => x.Insignia == insignia && x.Year == req.Year && x.Id != id)
|
|
.FirstOrDefaultAsync();
|
|
if (insigniaManage != null)
|
|
return Error(GlobalMessages.InsigniaManageNotFound);
|
|
|
|
var uppdated = await _context.InsigniaManages.AsQueryable()
|
|
.Include(x => x.Insignia)
|
|
.FirstOrDefaultAsync(x => x.Id == id);
|
|
if (uppdated == null)
|
|
return Error(GlobalMessages.InsigniaManageNotFound);
|
|
|
|
uppdated.Insignia = insignia;
|
|
uppdated.Year = req.Year;
|
|
uppdated.Total = req.Total;
|
|
uppdated.Type = type.Trim().ToUpper();
|
|
uppdated.LastUpdateFullName = FullName ?? "System Administrator";
|
|
uppdated.LastUpdateUserId = UserId ?? "";
|
|
uppdated.LastUpdatedAt = DateTime.Now;
|
|
|
|
await _context.SaveChangesAsync();
|
|
|
|
return Success();
|
|
}
|
|
|
|
}
|
|
}
|