CRUD Leave Beginning
Some checks failed
release-dev / release-dev (push) Failing after 11s

This commit is contained in:
Suphonchai Phoonsawat 2025-04-23 11:25:12 +07:00
parent b4c169be75
commit 19c30e69df
12 changed files with 3535 additions and 2 deletions

View file

@ -55,6 +55,7 @@ namespace BMA.EHR.Application
services.AddTransient<LeaveTypeRepository>();
services.AddTransient<LeaveRequestRepository>();
services.AddTransient<LeaveBeginningRepository>();
services.AddTransient<MinIOLeaveService>();

View file

@ -0,0 +1,64 @@
using BMA.EHR.Application.Common.Interfaces;
using BMA.EHR.Application.Messaging;
using BMA.EHR.Domain.Models.Leave.Requests;
using Microsoft.AspNetCore.Http;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Configuration;
namespace BMA.EHR.Application.Repositories.Leaves.LeaveRequests
{
public class LeaveBeginningRepository : GenericLeaveRepository<Guid, LeaveBeginning>
{
#region " Fields "
private readonly ILeaveDbContext _dbContext;
private readonly IHttpContextAccessor _httpContextAccessor;
private readonly OrganizationCommonRepository _organizationCommonRepository;
private readonly UserProfileRepository _userProfileRepository;
private readonly IConfiguration _configuration;
private readonly EmailSenderService _emailSenderService;
#endregion
#region " Constructor and Destuctor "
public LeaveBeginningRepository(ILeaveDbContext dbContext,
IHttpContextAccessor httpContextAccessor,
OrganizationCommonRepository organizationCommonRepository,
UserProfileRepository userProfileRepository,
IConfiguration configuration,
EmailSenderService emailSenderService) : base(dbContext, httpContextAccessor)
{
_dbContext = dbContext;
_httpContextAccessor = httpContextAccessor;
_organizationCommonRepository = organizationCommonRepository;
_userProfileRepository = userProfileRepository;
_configuration = configuration;
_emailSenderService = emailSenderService;
}
#endregion
#region " Properties "
protected Guid UserOrganizationId
{
get
{
if (UserId != null || UserId != "")
return _userProfileRepository.GetUserOCId(Guid.Parse(UserId!), AccessToken);
else
return Guid.Empty;
}
}
#endregion
public async Task<List<LeaveBeginning>> GetAllByYearAsync(int year)
{
return await _dbContext.Set<LeaveBeginning>()
.Where(x => x.LeaveYear == year)
.ToListAsync();
}
}
}

View file

@ -10,6 +10,12 @@ namespace BMA.EHR.Domain.Models.Leave.Requests
[Required, Comment("รหัส Profile ในระบบทะเบียนประวัติ")]
public Guid ProfileId { get; set; } = Guid.Empty;
public string? Prefix { get; set; } = string.Empty;
public string? FirstName { get; set; } = string.Empty;
public string? LastName { get; set; } = string.Empty;
[Required, Comment("รหัสประเภทการลา")]
public Guid LeaveTypeId { get; set; } = Guid.Empty;
@ -18,7 +24,10 @@ namespace BMA.EHR.Domain.Models.Leave.Requests
[Required, Comment("ปีงบประมาณ")]
public int LeaveYear { get; set; } = 0;
[Required, Comment("จำนวนวันลา")]
[Required, Comment("จำนวนวันลายกมา")]
public double LeaveDays { get; set; } = 0.0;
[Required, Comment("จำนวนวันลาที่ใช้ไป")]
public double LeaveDaysUsed { get; set; } = 0.0;
}
}

File diff suppressed because it is too large Load diff

View file

@ -0,0 +1,50 @@
using Microsoft.EntityFrameworkCore.Migrations;
#nullable disable
namespace BMA.EHR.Infrastructure.Migrations.LeaveDb
{
/// <inheritdoc />
public partial class AddLeaveDayused : Migration
{
/// <inheritdoc />
protected override void Up(MigrationBuilder migrationBuilder)
{
migrationBuilder.AlterColumn<double>(
name: "LeaveDays",
table: "LeaveBeginnings",
type: "double",
nullable: false,
comment: "จำนวนวันลายกมา",
oldClrType: typeof(double),
oldType: "double",
oldComment: "จำนวนวันลา");
migrationBuilder.AddColumn<double>(
name: "LeaveDaysUsed",
table: "LeaveBeginnings",
type: "double",
nullable: false,
defaultValue: 0.0,
comment: "จำนวนวันลาที่ใช้ไป");
}
/// <inheritdoc />
protected override void Down(MigrationBuilder migrationBuilder)
{
migrationBuilder.DropColumn(
name: "LeaveDaysUsed",
table: "LeaveBeginnings");
migrationBuilder.AlterColumn<double>(
name: "LeaveDays",
table: "LeaveBeginnings",
type: "double",
nullable: false,
comment: "จำนวนวันลา",
oldClrType: typeof(double),
oldType: "double",
oldComment: "จำนวนวันลายกมา");
}
}
}

View file

@ -0,0 +1,51 @@
using Microsoft.EntityFrameworkCore.Migrations;
#nullable disable
namespace BMA.EHR.Infrastructure.Migrations.LeaveDb
{
/// <inheritdoc />
public partial class AddProfileInfotoLeaveBegining : Migration
{
/// <inheritdoc />
protected override void Up(MigrationBuilder migrationBuilder)
{
migrationBuilder.AddColumn<string>(
name: "FirstName",
table: "LeaveBeginnings",
type: "longtext",
nullable: true)
.Annotation("MySql:CharSet", "utf8mb4");
migrationBuilder.AddColumn<string>(
name: "LastName",
table: "LeaveBeginnings",
type: "longtext",
nullable: true)
.Annotation("MySql:CharSet", "utf8mb4");
migrationBuilder.AddColumn<string>(
name: "Prefix",
table: "LeaveBeginnings",
type: "longtext",
nullable: true)
.Annotation("MySql:CharSet", "utf8mb4");
}
/// <inheritdoc />
protected override void Down(MigrationBuilder migrationBuilder)
{
migrationBuilder.DropColumn(
name: "FirstName",
table: "LeaveBeginnings");
migrationBuilder.DropColumn(
name: "LastName",
table: "LeaveBeginnings");
migrationBuilder.DropColumn(
name: "Prefix",
table: "LeaveBeginnings");
}
}
}

View file

@ -147,6 +147,12 @@ namespace BMA.EHR.Infrastructure.Migrations.LeaveDb
.HasColumnOrder(101)
.HasComment("User Id ที่สร้างข้อมูล");
b.Property<string>("FirstName")
.HasColumnType("longtext");
b.Property<string>("LastName")
.HasColumnType("longtext");
b.Property<string>("LastUpdateFullName")
.IsRequired()
.HasMaxLength(200)
@ -168,7 +174,11 @@ namespace BMA.EHR.Infrastructure.Migrations.LeaveDb
b.Property<double>("LeaveDays")
.HasColumnType("double")
.HasComment("จำนวนวันลา");
.HasComment("จำนวนวันลายกมา");
b.Property<double>("LeaveDaysUsed")
.HasColumnType("double")
.HasComment("จำนวนวันลาที่ใช้ไป");
b.Property<Guid>("LeaveTypeId")
.HasColumnType("char(36)")
@ -178,6 +188,9 @@ namespace BMA.EHR.Infrastructure.Migrations.LeaveDb
.HasColumnType("int")
.HasComment("ปีงบประมาณ");
b.Property<string>("Prefix")
.HasColumnType("longtext");
b.Property<Guid>("ProfileId")
.HasColumnType("char(36)")
.HasComment("รหัส Profile ในระบบทะเบียนประวัติ");

View file

@ -0,0 +1,300 @@
using BMA.EHR.Application.Repositories;
using BMA.EHR.Application.Repositories.Leaves.LeaveRequests;
using BMA.EHR.Domain.Common;
using BMA.EHR.Domain.Models.Leave.Requests;
using BMA.EHR.Infrastructure.Persistence;
using BMA.EHR.Leave.Service.DTOs.LeaveBeginnings;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using Swashbuckle.AspNetCore.Annotations;
using System.Security.Claims;
namespace BMA.EHR.Leave.Service.Controllers
{
[Route("api/v{version:apiVersion}/leave-beginning")]
[ApiVersion("1.0")]
[ApiController]
[Produces("application/json")]
[Authorize]
[SwaggerTag("API ระบบลงเวลาและการลา (ข้อมูลวันลายกมาและใช้ไป)")]
public class LeaveBeginningController : BaseController
{
#region " Fields "
private readonly LeaveBeginningRepository _leaveBeginningRepository;
private readonly LeaveDbContext _context;
private readonly IHttpContextAccessor _httpContextAccessor;
private readonly IWebHostEnvironment _hostingEnvironment;
private readonly IConfiguration _configuration;
private readonly UserProfileRepository _userProfileRepository;
private readonly PermissionRepository _permission;
#endregion
#region " Constuctor and Destructor "
public LeaveBeginningController(LeaveBeginningRepository leaveBeginningRepository,
LeaveDbContext context,
IHttpContextAccessor httpContextAccessor,
IWebHostEnvironment hostingEnvironment,
IConfiguration configuration,
UserProfileRepository userProfileRepository,
PermissionRepository permission)
{
_leaveBeginningRepository = leaveBeginningRepository;
_context = context;
_httpContextAccessor = httpContextAccessor;
_hostingEnvironment = hostingEnvironment;
_configuration = configuration;
_userProfileRepository = userProfileRepository;
_permission = permission;
}
#endregion
#region " Properties "
private string? UserId => _httpContextAccessor?.HttpContext?.User?.FindFirst(ClaimTypes.NameIdentifier)?.Value;
private string? FullName => _httpContextAccessor?.HttpContext?.User?.FindFirst("name")?.Value;
private bool? PlacementAdmin => _httpContextAccessor?.HttpContext?.User?.IsInRole("placement1");
private string? AccessToken => _httpContextAccessor?.HttpContext?.Request.Headers["Authorization"];
private Guid OcId
{
get
{
if (UserId != null || UserId != "")
return _userProfileRepository.GetUserOCId(Guid.Parse(UserId!), AccessToken);
else
return Guid.Empty;
}
}
#endregion
#region " Methods "
/// <summary>
/// แสดงรายการ
/// </summary>
/// <returns>
/// </returns>
/// <response code="200">เมื่อทำรายการสำเร็จ</response>
/// <response code="401">ไม่ได้ Login เข้าระบบ</response>
/// <response code="500">เมื่อเกิดข้อผิดพลาดในการทำงาน</response>
[HttpPost("list")]
[ProducesResponseType(StatusCodes.Status200OK)]
[ProducesResponseType(StatusCodes.Status401Unauthorized)]
[ProducesResponseType(StatusCodes.Status500InternalServerError)]
public async Task<ActionResult<ResponseObject>> GetListAsync([FromBody] GetLeaveBeginningDto req)
{
try
{
var getPermission = await _permission.GetPermissionAPIAsync("LIST", "SYS_LEAVE_LIST");
var jsonData = JsonConvert.DeserializeObject<JObject>(getPermission);
if (jsonData["status"]?.ToString() != "200")
{
return Error(jsonData["message"]?.ToString(), StatusCodes.Status403Forbidden);
}
var result = await _leaveBeginningRepository.GetAllByYearAsync(req.Year);
if (req.Type != Guid.Empty)
result = result.Where(x => x.LeaveTypeId == req.Type).ToList();
if (req.Keyword != "")
result = result.Where(x => x.FirstName!.Contains(req.Keyword) || x.LastName!.Contains(req.Keyword)).ToList();
var pageResult = result.Skip((req.Page - 1) * req.PageSize).Take(req.PageSize).ToList();
return Success(new { data = pageResult, total = result.Count });
}
catch (Exception ex)
{
return Error(ex);
}
}
/// <summary>
/// ลบรายการ
/// </summary>
/// <returns>
/// </returns>
/// <response code="200">เมื่อทำรายการสำเร็จ</response>
/// <response code="401">ไม่ได้ Login เข้าระบบ</response>
/// <response code="500">เมื่อเกิดข้อผิดพลาดในการทำงาน</response>
[HttpDelete("{id:guid}")]
[ProducesResponseType(StatusCodes.Status200OK)]
[ProducesResponseType(StatusCodes.Status401Unauthorized)]
[ProducesResponseType(StatusCodes.Status500InternalServerError)]
public async Task<ActionResult<ResponseObject>> DeleteAsync(Guid id)
{
try
{
var getPermission = await _permission.GetPermissionAPIAsync("DELETE", "SYS_LEAVE_LIST");
var jsonData = JsonConvert.DeserializeObject<JObject>(getPermission);
if (jsonData["status"]?.ToString() != "200")
{
return Error(jsonData["message"]?.ToString(), StatusCodes.Status403Forbidden);
}
var leaveBeginning = await _leaveBeginningRepository.GetByIdAsync(id);
if (leaveBeginning == null)
return Error("ไม่พบข้อมูลที่ต้องการลบ", StatusCodes.Status404NotFound);
await _leaveBeginningRepository.DeleteAsync(leaveBeginning);
return Success("ลบข้อมูลสำเร็จ");
}
catch (Exception ex)
{
return Error(ex);
}
}
/// <summary>
/// แสดงรายการจาก Id
/// </summary>
/// <returns>
/// </returns>
/// <response code="200">เมื่อทำรายการสำเร็จ</response>
/// <response code="401">ไม่ได้ Login เข้าระบบ</response>
/// <response code="500">เมื่อเกิดข้อผิดพลาดในการทำงาน</response>
[HttpGet("{id:guid}")]
public async Task<ActionResult<ResponseObject>> GetByIdAsync(Guid id)
{
try
{
var getPermission = await _permission.GetPermissionAPIAsync("GET", "SYS_LEAVE_LIST");
var jsonData = JsonConvert.DeserializeObject<JObject>(getPermission);
if (jsonData["status"]?.ToString() != "200")
{
return Error(jsonData["message"]?.ToString(), StatusCodes.Status403Forbidden);
}
var leaveBeginning = _leaveBeginningRepository.GetByIdAsync(id);
if (leaveBeginning == null)
return Error("ไม่พบข้อมูลที่ต้องการลบ", StatusCodes.Status404NotFound);
return Success(leaveBeginning);
}
catch (Exception ex)
{
return Error(ex);
}
}
// <summary>
/// แก้ไขรายการ
/// </summary>
/// <returns>
/// </returns>
/// <response code="200">เมื่อทำรายการสำเร็จ</response>
/// <response code="401">ไม่ได้ Login เข้าระบบ</response>
/// <response code="500">เมื่อเกิดข้อผิดพลาดในการทำงาน</response>
[HttpPut("{id:guid}")]
public async Task<ActionResult<ResponseObject>> PutAsync(Guid id, [FromBody] EditLeaveBeginningDto req)
{
try
{
var userId = UserId == null ? Guid.Empty : Guid.Parse(UserId);
var getPermission = await _permission.GetPermissionAPIAsync("UPDATE", "SYS_LEAVE_LIST");
var jsonData = JsonConvert.DeserializeObject<JObject>(getPermission);
if (jsonData["status"]?.ToString() != "200")
{
return Error(jsonData["message"]?.ToString(), StatusCodes.Status403Forbidden);
}
var leaveBeginning = await _leaveBeginningRepository.GetByIdAsync(id);
if (leaveBeginning == null)
return Error("ไม่พบข้อมูลที่ต้องการแก้ไข", StatusCodes.Status404NotFound);
var profile = await _userProfileRepository.GetProfileByProfileIdAsync(req.ProfileId, AccessToken);
if(profile == null)
{
return Error("ไม่พบข้อมูลข้าราชการหรือลูกจ้าง", StatusCodes.Status404NotFound);
}
leaveBeginning.LeaveTypeId = req.LeaveTypeId;
leaveBeginning.LeaveYear = req.LeaveYear;
leaveBeginning.LeaveDays = req.LeaveDays;
leaveBeginning.LeaveDaysUsed = req.LeaveDaysUsed;
leaveBeginning.ProfileId = req.ProfileId;
leaveBeginning.Prefix = profile.Prefix;
leaveBeginning.FirstName = profile.FirstName;
leaveBeginning.LastName = profile.LastName;
leaveBeginning.LastUpdateUserId = userId.ToString("D");
leaveBeginning.LastUpdateFullName = FullName ?? "";
leaveBeginning.LastUpdatedAt = DateTime.Now;
await _leaveBeginningRepository.UpdateAsync(leaveBeginning);
return Success("แก้ไขข้อมูลสำเร็จ");
}
catch (Exception ex)
{
return Error(ex);
}
}
/// <summary>
/// สร้างรายการ
/// </summary>
/// <returns>
/// </returns>
/// <response code="200">เมื่อทำรายการสำเร็จ</response>
/// <response code="401">ไม่ได้ Login เข้าระบบ</response>
/// <response code="500">เมื่อเกิดข้อผิดพลาดในการทำงาน</response>
[HttpPost()]
public async Task<ActionResult<ResponseObject>> PostAsync([FromBody] EditLeaveBeginningDto req)
{
try
{
var userId = UserId == null ? Guid.Empty : Guid.Parse(UserId);
var getPermission = await _permission.GetPermissionAPIAsync("CREATE", "SYS_LEAVE_LIST");
var jsonData = JsonConvert.DeserializeObject<JObject>(getPermission);
if (jsonData["status"]?.ToString() != "200")
{
return Error(jsonData["message"]?.ToString(), StatusCodes.Status403Forbidden);
}
var profile = await _userProfileRepository.GetProfileByProfileIdAsync(req.ProfileId, AccessToken);
if (profile == null)
{
return Error("ไม่พบข้อมูลข้าราชการหรือลูกจ้าง", StatusCodes.Status404NotFound);
}
var leaveBeginning = new LeaveBeginning();
leaveBeginning.LeaveTypeId = req.LeaveTypeId;
leaveBeginning.LeaveYear = req.LeaveYear;
leaveBeginning.LeaveDays = req.LeaveDays;
leaveBeginning.LeaveDaysUsed = req.LeaveDaysUsed;
leaveBeginning.ProfileId = req.ProfileId;
leaveBeginning.Prefix = profile.Prefix;
leaveBeginning.FirstName = profile.FirstName;
leaveBeginning.LastName = profile.LastName;
leaveBeginning.CreatedUserId = userId.ToString("D");
leaveBeginning.CreatedFullName = FullName ?? "";
leaveBeginning.CreatedAt = DateTime.Now;
await _leaveBeginningRepository.AddAsync(leaveBeginning);
return Success();
}
catch(Exception ex)
{
return Error(ex);
}
}
#endregion
}
}

View file

@ -0,0 +1,23 @@
using Microsoft.EntityFrameworkCore;
using System.ComponentModel.DataAnnotations;
namespace BMA.EHR.Leave.Service.DTOs.LeaveBeginnings
{
public class CreateLeaveBeginningDto
{
[Required]
public Guid ProfileId { get; set; } = Guid.Empty;
[Required]
public Guid LeaveTypeId { get; set; } = Guid.Empty;
[Required, Comment("ปีงบประมาณ")]
public int LeaveYear { get; set; } = 0;
[Required, Comment("จำนวนวันลายกมา")]
public double LeaveDays { get; set; } = 0.0;
[Required, Comment("จำนวนวันลาที่ใช้ไป")]
public double LeaveDaysUsed { get; set; } = 0.0;
}
}

View file

@ -0,0 +1,23 @@
using Microsoft.EntityFrameworkCore;
using System.ComponentModel.DataAnnotations;
namespace BMA.EHR.Leave.Service.DTOs.LeaveBeginnings
{
public class EditLeaveBeginningDto
{
[Required]
public Guid ProfileId { get; set; } = Guid.Empty;
[Required]
public Guid LeaveTypeId { get; set; } = Guid.Empty;
[Required, Comment("ปีงบประมาณ")]
public int LeaveYear { get; set; } = 0;
[Required, Comment("จำนวนวันลายกมา")]
public double LeaveDays { get; set; } = 0.0;
[Required, Comment("จำนวนวันลาที่ใช้ไป")]
public double LeaveDaysUsed { get; set; } = 0.0;
}
}

View file

@ -0,0 +1,20 @@
using System.ComponentModel.DataAnnotations;
namespace BMA.EHR.Leave.Service.DTOs.LeaveBeginnings
{
public class GetLeaveBeginningDto
{
[Required]
public int Year { get; set; } = 0;
public Guid Type { get; set; } = Guid.Empty;
[Required]
public int Page { get; set; } = 1;
[Required]
public int PageSize { get; set; } = 10;
public string Keyword { get; set; } = string.Empty;
}
}