แก้ BaseController เพิ่มเวลา Exception สามารถส่งข้อความที่ต้องการให้ user เห็นและแสดง exception message ให้ dev ดูได้

แก้ไขขั้นตอนการ import ให้บันทึกลงตาราง RecruitImportHistories ด้วย
เพิ่ม api การเรียกดูประวัติการนำเข้า
This commit is contained in:
Suphonchai Phoonsawat 2023-04-11 17:15:55 +07:00
parent 56fcf7749a
commit 6d9f252f1a
28 changed files with 1011 additions and 709 deletions

View file

@ -7,67 +7,87 @@ using System.Net;
namespace BMA.EHR.Recruit.Service.Controllers
{
public class BaseController : ControllerBase
{
#region " Methods "
public class BaseController : ControllerBase
{
#region " Methods "
#region " Protected "
#region " Protected "
#region " IActionResult "
#region " IActionResult "
protected virtual ActionResult<ResponseObject> Success(string message, object? result = null)
{
if (result != null)
{
return Ok(new ResponseObject
{
Status = StatusCodes.Status200OK,
Message = message,
Result = result
});
}
else
{
return Ok(new ResponseObject
{
Status = StatusCodes.Status200OK,
Message = message
});
}
protected virtual ActionResult<ResponseObject> Success(string message, object? result = null)
{
if (result != null)
{
return Ok(new ResponseObject
{
Status = StatusCodes.Status200OK,
Message = message,
Result = result
});
}
else
{
return Ok(new ResponseObject
{
Status = StatusCodes.Status200OK,
Message = message
});
}
}
}
protected virtual ActionResult<ResponseObject> Success(object? result = null)
{
return Success(GlobalMessages.Success, result);
}
protected virtual ActionResult<ResponseObject> Success(object? result = null)
{
return Success(GlobalMessages.Success, result);
}
protected virtual ActionResult<ResponseObject> Error(string message, int statusCode = StatusCodes.Status500InternalServerError)
{
return StatusCode((int)statusCode, new ResponseObject
{
Status = statusCode,
Message = message
});
}
protected virtual ActionResult<ResponseObject> Error(string message, string result, int statusCode = StatusCodes.Status500InternalServerError)
{
return StatusCode((int)statusCode, new ResponseObject
{
Status = statusCode,
Message = message,
Result = result
});
}
protected virtual ActionResult<ResponseObject> Error(Exception exception, int statusCode = StatusCodes.Status500InternalServerError)
{
var msg = exception.Message;
var inner = exception.InnerException;
while (inner != null)
{
msg += $" {inner.Message}\r\n";
inner = inner.InnerException;
}
protected virtual ActionResult<ResponseObject> Error(string message, int statusCode = StatusCodes.Status500InternalServerError)
{
return Error(message, message, statusCode);
}
return Error(msg, statusCode);
}
protected virtual ActionResult<ResponseObject> Error(Exception exception, string message, int statusCode = StatusCodes.Status500InternalServerError)
{
var msg = exception.Message;
var inner = exception.InnerException;
while (inner != null)
{
msg += $" {inner.Message}\r\n";
inner = inner.InnerException;
}
#endregion
return Error(message, msg, statusCode);
}
#endregion
protected virtual ActionResult<ResponseObject> Error(Exception exception, int statusCode = StatusCodes.Status500InternalServerError)
{
var msg = exception.Message;
var inner = exception.InnerException;
while (inner != null)
{
msg += $" {inner.Message}\r\n";
inner = inner.InnerException;
}
#endregion
}
return Error(msg, msg, statusCode);
}
#endregion
#endregion
#endregion
}
}

View file

@ -17,6 +17,7 @@ using Sentry;
using Swashbuckle.AspNetCore.Annotations;
using System.Data;
using System.Net;
using System.Security.Claims;
using System.Text;
namespace BMA.EHR.Recruit.Service.Controllers
@ -36,6 +37,9 @@ namespace BMA.EHR.Recruit.Service.Controllers
private readonly IWebHostEnvironment _webHostEnvironment;
private readonly RecruitService _recruitService;
private readonly IHttpContextAccessor _httpContextAccessor;
private readonly ILogger<RecruitController> _logger;
#endregion
#region " Constructor and Destructor "
@ -43,16 +47,28 @@ namespace BMA.EHR.Recruit.Service.Controllers
public RecruitController(ApplicationDbContext context,
MinIOService minioService,
IWebHostEnvironment webHostEnvironment,
RecruitService recruitService)
RecruitService recruitService,
IHttpContextAccessor httpContextAccessor,
ILogger<RecruitController> logger)
{
_context = context;
_minioService = minioService;
_webHostEnvironment = webHostEnvironment;
_recruitService = recruitService;
_httpContextAccessor = httpContextAccessor;
_logger = logger;
}
#endregion
#region " Properties "
private string? UserId => _httpContextAccessor?.HttpContext?.User?.FindFirst(ClaimTypes.NameIdentifier)?.Value;
private string? FullName => _httpContextAccessor?.HttpContext?.User?.FindFirst("name")?.Value;
#endregion
#region " Methods "
#region " Private "
@ -534,7 +550,19 @@ namespace BMA.EHR.Recruit.Service.Controllers
Order = req.Order,
Name = req.Name,
ImportFile = doc,
//Recruits = new List<Models.Recruits.Recruit>(),
CreatedAt = DateTime.Now,
CreatedUserId = UserId ?? "",
CreatedFullName = FullName ?? "System Administrator",
ImportHostories = new List<RecruitImportHistory>
{
new RecruitImportHistory
{
Description = "นำเข้าข้อมูลผู้สมัครสอบแข่งขัน",
CreatedAt = DateTime.Now,
CreatedUserId = UserId ?? "",
CreatedFullName = FullName ?? "System Administrator",
}
}
};
await _context.RecruitImports.AddAsync(imported);
@ -590,7 +618,7 @@ namespace BMA.EHR.Recruit.Service.Controllers
ZipCode = workSheet?.Cells[row, GetColumnIndex(cols, CandidateFileHeader.ZipCode)]?.GetValue<string>() ?? "",
Telephone = workSheet?.Cells[row, GetColumnIndex(cols, CandidateFileHeader.Telephone)]?.GetValue<string>() ?? "",
Mobile = workSheet?.Cells[row, GetColumnIndex(cols, CandidateFileHeader.Mobile)]?.GetValue<string>() ?? "",
Address1 = workSheet?.Cells[row, GetColumnIndex(cols, CandidateFileHeader.Address1)]?.GetValue<string>()??"",
Address1 = workSheet?.Cells[row, GetColumnIndex(cols, CandidateFileHeader.Address1)]?.GetValue<string>() ?? "",
Moo1 = workSheet?.Cells[row, GetColumnIndex(cols, CandidateFileHeader.Moo1)]?.GetValue<string>() ?? "",
Soi1 = workSheet?.Cells[row, GetColumnIndex(cols, CandidateFileHeader.Soi1)]?.GetValue<string>() ?? "",
Road1 = workSheet?.Cells[row, GetColumnIndex(cols, CandidateFileHeader.Road1)]?.GetValue<string>() ?? "",
@ -615,7 +643,7 @@ namespace BMA.EHR.Recruit.Service.Controllers
TermBranch = workSheet?.Cells[row, GetColumnIndex(cols, CandidateFileHeader.TermBranch)]?.GetValue<string>() ?? "",
TellerId = workSheet?.Cells[row, GetColumnIndex(cols, CandidateFileHeader.TellerID)]?.GetValue<string>() ?? "",
CreditDebit = workSheet?.Cells[row, GetColumnIndex(cols, CandidateFileHeader.CreditDebit)]?.GetValue<string>() ?? "",
PaymentType = workSheet?.Cells[row, GetColumnIndex(cols, CandidateFileHeader.Type)]?.GetValue<string>() ,
PaymentType = workSheet?.Cells[row, GetColumnIndex(cols, CandidateFileHeader.Type)]?.GetValue<string>(),
ChequeNo = workSheet?.Cells[row, GetColumnIndex(cols, CandidateFileHeader.ChequeNo)]?.GetValue<string>() ?? "",
Amount = (decimal)workSheet?.Cells[row, GetColumnIndex(cols, CandidateFileHeader.Amount)]?.GetValue<decimal>(),
ChqueBankCode = workSheet?.Cells[row, GetColumnIndex(cols, CandidateFileHeader.ChqBankCode)]?.GetValue<string>() ?? ""
@ -625,7 +653,7 @@ namespace BMA.EHR.Recruit.Service.Controllers
r.Occupations.Add(new RecruitOccupation()
{
Occupation = workSheet?.Cells[row, GetColumnIndex(cols, CandidateFileHeader.Occupation)]?.GetValue<string>() ?? "",
Position = workSheet?.Cells[row, GetColumnIndex(cols, CandidateFileHeader.Position)]?.GetValue<string>()??"",
Position = workSheet?.Cells[row, GetColumnIndex(cols, CandidateFileHeader.Position)]?.GetValue<string>() ?? "",
Workplace = workSheet?.Cells[row, GetColumnIndex(cols, CandidateFileHeader.Workplace)]?.GetValue<string>() ?? "",
Telephone = workSheet?.Cells[row, GetColumnIndex(cols, CandidateFileHeader.WorkplaceTelephone)]?.GetValue<string>() ?? "",
WorkAge = workSheet?.Cells[row, GetColumnIndex(cols, CandidateFileHeader.WorkAge)]?.GetValue<string>() ?? "",
@ -701,6 +729,7 @@ namespace BMA.EHR.Recruit.Service.Controllers
try
{
var data = await _context.RecruitImports.AsQueryable()
.Include(x => x.ImportHostories)
.Include(x => x.ImportFile)
.Include(x => x.Recruits)
.ThenInclude(x => x.Addresses)
@ -745,6 +774,38 @@ namespace BMA.EHR.Recruit.Service.Controllers
}
}
/// <summary>
/// แสดงประวัติการนำเข้าข้อมูลการสอบแข่งขัน
/// </summary>
/// <param name="id">รหัสรอบการสอบแข่งขัน</param>
/// <returns></returns>
/// <response code="200">เมื่อทำนำเข้าข้อมูลสำเร็จ</response>
/// <response code="400">ค่าตัวแปรที่ส่งมาไม่ถูกต้อง</response>
/// <response code="401">ไม่ได้ Login เข้าระบบ</response>
/// <response code="500">เมื่อเกิดข้อผิดพลาดในการทำงาน</response>
[HttpGet("history/{id:length(36)}")]
[ProducesResponseType(StatusCodes.Status200OK)]
[ProducesResponseType(StatusCodes.Status400BadRequest)]
[ProducesResponseType(StatusCodes.Status401Unauthorized)]
[ProducesResponseType(StatusCodes.Status500InternalServerError)]
public async Task<ActionResult<ResponseObject>> GetImportHistoryAsync(Guid id)
{
try
{
var data = await _context.RecruitImportHistories.AsQueryable()
.Include(x => x.RecruitImport)
.Where(x => x.RecruitImport.Id == id)
.OrderByDescending(x => x.CreatedAt)
.ToListAsync();
return Success(data);
}
catch (Exception ex)
{
return Error(ex);
}
}
#endregion
#region " Score File "
@ -768,6 +829,7 @@ namespace BMA.EHR.Recruit.Service.Controllers
var rec_import = await _context.RecruitImports.AsQueryable()
.Include(x => x.ScoreImport)
.Include(x => x.ImportHostories)
.FirstOrDefaultAsync(x => x.Id == id);
if (rec_import == null)
@ -781,6 +843,15 @@ namespace BMA.EHR.Recruit.Service.Controllers
import_doc_id = doc.Id.ToString("D");
var fileContent = (await _minioService.DownloadFileAsync(doc.Id)).FileContent;
// create import history
rec_import.ImportHostories.Add(new RecruitImportHistory
{
Description = "นำเข้าข้อมูลผลคะแนนสอบ",
CreatedAt = DateTime.Now,
CreatedUserId = UserId ?? "",
CreatedFullName = FullName ?? "System Administrator",
});
// create new file import
var imported = new ScoreImport
{