190 lines
9.1 KiB
C#
190 lines
9.1 KiB
C#
using BMA.EHR.Application.Repositories;
|
|
using BMA.EHR.Application.Repositories.MessageQueue;
|
|
using BMA.EHR.Discipline.Service.Requests;
|
|
using BMA.EHR.Domain.Common;
|
|
using BMA.EHR.Domain.Models.Discipline;
|
|
using BMA.EHR.Domain.Shared;
|
|
using BMA.EHR.Infrastructure.Persistence;
|
|
// using BMA.EHR.Placement.Service.Requests;
|
|
using Microsoft.AspNetCore.Authorization;
|
|
using Microsoft.AspNetCore.Mvc;
|
|
using Microsoft.EntityFrameworkCore;
|
|
using Swashbuckle.AspNetCore.Annotations;
|
|
using System.Security.Claims;
|
|
|
|
namespace BMA.EHR.DisciplineDirector.Service.Controllers
|
|
{
|
|
[Route("api/v{version:apiVersion}/discipline/director")]
|
|
[ApiVersion("1.0")]
|
|
[ApiController]
|
|
[Produces("application/json")]
|
|
[Authorize]
|
|
[SwaggerTag("กรรมการระบบวินัย")]
|
|
public class DisciplineDirectorController : BaseController
|
|
{
|
|
private readonly DisciplineDbContext _context;
|
|
private readonly MinIOService _documentService;
|
|
private readonly IHttpContextAccessor _httpContextAccessor;
|
|
|
|
public DisciplineDirectorController(DisciplineDbContext context,
|
|
MinIOService documentService,
|
|
IHttpContextAccessor httpContextAccessor)
|
|
{
|
|
// _repository = repository;
|
|
_context = context;
|
|
_documentService = documentService;
|
|
_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>
|
|
/// <returns></returns>
|
|
/// <response code="200"></response>
|
|
/// <response code="400">ค่าตัวแปรที่ส่งมาไม่ถูกต้อง</response>
|
|
/// <response code="401">ไม่ได้ Login เข้าระบบ</response>
|
|
/// <response code="500">เมื่อเกิดข้อผิดพลาดในการทำงาน</response>
|
|
[HttpGet()]
|
|
public async Task<ActionResult<ResponseObject>> GetDiscipline(int page = 1, int pageSize = 25, string keyword = "")
|
|
{
|
|
var data_search = (from x in _context.DisciplineDirectors
|
|
where x.Prefix.Contains(keyword) ||
|
|
x.FirstName.Contains(keyword) ||
|
|
x.LastName.Contains(keyword) ||
|
|
x.Position.Contains(keyword) ||
|
|
x.Email.Contains(keyword) ||
|
|
x.Phone.Contains(keyword)
|
|
select x).ToList();
|
|
var data = data_search
|
|
.Select(x => new
|
|
{
|
|
Id = x.Id,
|
|
Prefix = x.Prefix,
|
|
FirstName = x.FirstName,
|
|
LastName = x.LastName,
|
|
Position = x.Position,
|
|
Email = x.Email,
|
|
Phone = x.Phone,
|
|
})
|
|
.Skip((page - 1) * pageSize)
|
|
.Take(pageSize)
|
|
.ToList();
|
|
return Success(new { data, total = data_search.Count() });
|
|
}
|
|
|
|
/// <summary>
|
|
/// get รายการกรรมการระบบวินัย
|
|
/// </summary>
|
|
/// <returns></returns>
|
|
/// <response code="200"></response>
|
|
/// <response code="400">ค่าตัวแปรที่ส่งมาไม่ถูกต้อง</response>
|
|
/// <response code="401">ไม่ได้ Login เข้าระบบ</response>
|
|
/// <response code="500">เมื่อเกิดข้อผิดพลาดในการทำงาน</response>
|
|
[HttpGet("{id:guid}")]
|
|
public async Task<ActionResult<ResponseObject>> GetByDiscipline(Guid id)
|
|
{
|
|
var data = await _context.DisciplineDirectors
|
|
.Select(x => new
|
|
{
|
|
Id = x.Id,
|
|
Prefix = x.Prefix,
|
|
FirstName = x.FirstName,
|
|
LastName = x.LastName,
|
|
Position = x.Position,
|
|
Email = x.Email,
|
|
Phone = x.Phone,
|
|
})
|
|
.Where(x => x.Id == id)
|
|
.FirstOrDefaultAsync();
|
|
if (data == null)
|
|
return Error(new Exception(GlobalMessages.DataNotFound), (int)StatusCodes.Status404NotFound);
|
|
return Success(data);
|
|
}
|
|
|
|
/// <summary>
|
|
/// สร้างรายการกรรมการระบบวินัย
|
|
/// </summary>
|
|
/// <returns></returns>
|
|
/// <response code="200"></response>
|
|
/// <response code="400">ค่าตัวแปรที่ส่งมาไม่ถูกต้อง</response>
|
|
/// <response code="401">ไม่ได้ Login เข้าระบบ</response>
|
|
/// <response code="500">เมื่อเกิดข้อผิดพลาดในการทำงาน</response>
|
|
[HttpPost()]
|
|
public async Task<ActionResult<ResponseObject>> CreateDiscipline([FromBody] DisciplineDirectorRequest req)
|
|
{
|
|
var disciplineDirector = new Domain.Models.Discipline.DisciplineDirector
|
|
{
|
|
Prefix = req.prefix,
|
|
FirstName = req.firstName,
|
|
LastName = req.lastName,
|
|
Position = req.position,
|
|
Email = req.email,
|
|
Phone = req.phone,
|
|
CreatedFullName = FullName ?? "System Administrator",
|
|
CreatedUserId = UserId ?? "",
|
|
CreatedAt = DateTime.Now,
|
|
LastUpdateFullName = FullName ?? "System Administrator",
|
|
LastUpdateUserId = UserId ?? "",
|
|
LastUpdatedAt = DateTime.Now,
|
|
};
|
|
await _context.DisciplineDirectors.AddAsync(disciplineDirector);
|
|
await _context.SaveChangesAsync();
|
|
return Success();
|
|
}
|
|
|
|
/// <summary>
|
|
/// แก้ไขรายการกรรมการระบบวินัย
|
|
/// </summary>
|
|
/// <returns></returns>
|
|
/// <response code="200"></response>
|
|
/// <response code="400">ค่าตัวแปรที่ส่งมาไม่ถูกต้อง</response>
|
|
/// <response code="401">ไม่ได้ Login เข้าระบบ</response>
|
|
/// <response code="500">เมื่อเกิดข้อผิดพลาดในการทำงาน</response>
|
|
[HttpPut("{id:guid}")]
|
|
public async Task<ActionResult<ResponseObject>> UpdateDiscipline(Guid id, [FromBody] DisciplineDirectorRequest req)
|
|
{
|
|
var data = await _context.DisciplineDirectors.Where(x => x.Id == id).FirstOrDefaultAsync();
|
|
if (data == null)
|
|
return Error(new Exception(GlobalMessages.DataNotFound), (int)StatusCodes.Status404NotFound);
|
|
|
|
data.Prefix = req.prefix;
|
|
data.FirstName = req.firstName;
|
|
data.LastName = req.lastName;
|
|
data.Position = req.position;
|
|
data.Email = req.email;
|
|
data.Phone = req.phone;
|
|
data.LastUpdateFullName = FullName ?? "System Administrator";
|
|
data.LastUpdateUserId = UserId ?? "";
|
|
data.LastUpdatedAt = DateTime.Now;
|
|
await _context.SaveChangesAsync();
|
|
return Success(data);
|
|
}
|
|
|
|
/// <summary>
|
|
/// ลบรายการกรรมการระบบวินัย
|
|
/// </summary>
|
|
/// <returns></returns>
|
|
/// <response code="200"></response>
|
|
/// <response code="400">ค่าตัวแปรที่ส่งมาไม่ถูกต้อง</response>
|
|
/// <response code="401">ไม่ได้ Login เข้าระบบ</response>
|
|
/// <response code="500">เมื่อเกิดข้อผิดพลาดในการทำงาน</response>
|
|
[HttpDelete("{id:guid}")]
|
|
public async Task<ActionResult<ResponseObject>> DeleteDiscipline(Guid id)
|
|
{
|
|
var data = await _context.DisciplineDirectors.Where(x => x.Id == id).FirstOrDefaultAsync();
|
|
if (data == null)
|
|
return Error(new Exception(GlobalMessages.DataNotFound), (int)StatusCodes.Status404NotFound);
|
|
_context.DisciplineDirectors.Remove(data);
|
|
await _context.SaveChangesAsync();
|
|
return Success();
|
|
}
|
|
}
|
|
}
|