API Upload, Download and Delete Complete
This commit is contained in:
parent
bdd327cce2
commit
27a3b1e771
34 changed files with 1120 additions and 780 deletions
|
|
@ -13,267 +13,319 @@ using System.Text;
|
|||
|
||||
namespace BMA.EHR.Recruit.Service.Controllers
|
||||
{
|
||||
[Route("api/v{version:apiVersion}/recruit")]
|
||||
[ApiVersion("1.0")]
|
||||
[ApiController]
|
||||
[Produces("application/json")]
|
||||
[Authorize]
|
||||
[SwaggerTag("จัดการข้อมูลการสอบแข่งขัน")]
|
||||
public class RecruitController : BaseController
|
||||
{
|
||||
#region " Fields "
|
||||
[Route("api/v{version:apiVersion}/recruit")]
|
||||
[ApiVersion("1.0")]
|
||||
[ApiController]
|
||||
[Produces("application/json")]
|
||||
[Authorize]
|
||||
[SwaggerTag("จัดการข้อมูลการสอบแข่งขัน")]
|
||||
public class RecruitController : BaseController
|
||||
{
|
||||
#region " Fields "
|
||||
|
||||
private readonly ApplicationDbContext _context;
|
||||
private readonly MinIOService _minioService;
|
||||
private readonly IWebHostEnvironment _webHostEnvironment;
|
||||
private readonly RecruitService _recruitService;
|
||||
private readonly ApplicationDbContext _context;
|
||||
private readonly MinIOService _minioService;
|
||||
private readonly IWebHostEnvironment _webHostEnvironment;
|
||||
private readonly RecruitService _recruitService;
|
||||
|
||||
#endregion
|
||||
#endregion
|
||||
|
||||
#region " Constructor and Destructor "
|
||||
#region " Constructor and Destructor "
|
||||
|
||||
public RecruitController(ApplicationDbContext context,
|
||||
MinIOService minioService,
|
||||
IWebHostEnvironment webHostEnvironment,
|
||||
RecruitService recruitService)
|
||||
{
|
||||
_context = context;
|
||||
_minioService = minioService;
|
||||
_webHostEnvironment = webHostEnvironment;
|
||||
_recruitService = recruitService;
|
||||
}
|
||||
public RecruitController(ApplicationDbContext context,
|
||||
MinIOService minioService,
|
||||
IWebHostEnvironment webHostEnvironment,
|
||||
RecruitService recruitService)
|
||||
{
|
||||
_context = context;
|
||||
_minioService = minioService;
|
||||
_webHostEnvironment = webHostEnvironment;
|
||||
_recruitService = recruitService;
|
||||
}
|
||||
|
||||
#endregion
|
||||
#endregion
|
||||
|
||||
#region " Methods "
|
||||
#region " Methods "
|
||||
|
||||
#region " Private "
|
||||
#region " Private "
|
||||
|
||||
private int GetColumnIndex(string[] columns, string name, bool partial = false)
|
||||
{
|
||||
try
|
||||
{
|
||||
if (partial)
|
||||
return Array.FindIndex(columns, x => x.Contains(name)) + 1;
|
||||
else
|
||||
return Array.FindIndex(columns, x => x == name) + 1;
|
||||
}
|
||||
catch
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
private int GetColumnIndex(string[] columns, string name, bool partial = false)
|
||||
{
|
||||
try
|
||||
{
|
||||
if (partial)
|
||||
return Array.FindIndex(columns, x => x.Contains(name)) + 1;
|
||||
else
|
||||
return Array.FindIndex(columns, x => x == name) + 1;
|
||||
}
|
||||
catch
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
private string CalculateDiff(DateTime d1, DateTime d2)
|
||||
{
|
||||
if (d1 > d2) return "ข้อมูลไม่ถูกต้อง";
|
||||
private string CalculateDiff(DateTime d1, DateTime d2)
|
||||
{
|
||||
if (d1 > d2) return "ข้อมูลไม่ถูกต้อง";
|
||||
|
||||
TimeSpan sp = d2.Subtract(d1);
|
||||
int yy = sp.Days / 365;
|
||||
int mm = (sp.Days - (yy * 365)) / 30;
|
||||
int dd = (sp.Days - (yy * 365) - (mm * 30));
|
||||
TimeSpan sp = d2.Subtract(d1);
|
||||
int yy = sp.Days / 365;
|
||||
int mm = (sp.Days - (yy * 365)) / 30;
|
||||
int dd = (sp.Days - (yy * 365) - (mm * 30));
|
||||
|
||||
var sb = new StringBuilder();
|
||||
sb.Clear();
|
||||
sb.Append(yy == 0 ? "" : $"{yy} ปี ");
|
||||
sb.Append(mm == 0 ? "" : $"{mm} เดือน ");
|
||||
//sb.Append(dd == 0 ? "" : $"{dd} วัน ");
|
||||
var sb = new StringBuilder();
|
||||
sb.Clear();
|
||||
sb.Append(yy == 0 ? "" : $"{yy} ปี ");
|
||||
sb.Append(mm == 0 ? "" : $"{mm} เดือน ");
|
||||
//sb.Append(dd == 0 ? "" : $"{dd} วัน ");
|
||||
|
||||
return sb.ToString();
|
||||
}
|
||||
return sb.ToString();
|
||||
}
|
||||
|
||||
private async Task<int> GetExamCount(Guid exam)
|
||||
{
|
||||
try
|
||||
{
|
||||
return await _context.RecruitPayments.AsQueryable()
|
||||
.Include(x => x.Recruit)
|
||||
.ThenInclude(x => x.RecruitImport)
|
||||
.Where(x => x.Recruit.RecruitImport.Id == exam)
|
||||
.CountAsync();
|
||||
}
|
||||
catch
|
||||
{
|
||||
throw;
|
||||
}
|
||||
}
|
||||
private async Task<int> GetExamCount(Guid exam)
|
||||
{
|
||||
try
|
||||
{
|
||||
return await _context.RecruitPayments.AsQueryable()
|
||||
.Include(x => x.Recruit)
|
||||
.ThenInclude(x => x.RecruitImport)
|
||||
.Where(x => x.Recruit.RecruitImport.Id == exam)
|
||||
.CountAsync();
|
||||
}
|
||||
catch
|
||||
{
|
||||
throw;
|
||||
}
|
||||
}
|
||||
|
||||
private async Task<int> GetPassExamCount(Guid exam)
|
||||
{
|
||||
try
|
||||
{
|
||||
return await _context.RecruitScores.AsQueryable()
|
||||
.Include(x => x.ScoreImport)
|
||||
.Where(x => x.ScoreImport.Id == exam)
|
||||
.Where(x => x.ExamStatus == "ผ่าน")
|
||||
.CountAsync();
|
||||
}
|
||||
catch
|
||||
{
|
||||
throw;
|
||||
}
|
||||
}
|
||||
private async Task<int> GetPassExamCount(Guid exam)
|
||||
{
|
||||
try
|
||||
{
|
||||
return await _context.RecruitScores.AsQueryable()
|
||||
.Include(x => x.ScoreImport)
|
||||
.Where(x => x.ScoreImport.Id == exam)
|
||||
.Where(x => x.ExamStatus == "ผ่าน")
|
||||
.CountAsync();
|
||||
}
|
||||
catch
|
||||
{
|
||||
throw;
|
||||
}
|
||||
}
|
||||
|
||||
private async Task<int> GetScoreCount(Guid exam)
|
||||
{
|
||||
try
|
||||
{
|
||||
return await _context.RecruitScores.AsQueryable()
|
||||
.Include(x => x.ScoreImport)
|
||||
.Where(x => x.ScoreImport.Id == exam)
|
||||
.CountAsync();
|
||||
}
|
||||
catch
|
||||
{
|
||||
throw;
|
||||
}
|
||||
}
|
||||
private async Task<int> GetScoreCount(Guid exam)
|
||||
{
|
||||
try
|
||||
{
|
||||
return await _context.RecruitScores.AsQueryable()
|
||||
.Include(x => x.ScoreImport)
|
||||
.Where(x => x.ScoreImport.Id == exam)
|
||||
.CountAsync();
|
||||
}
|
||||
catch
|
||||
{
|
||||
throw;
|
||||
}
|
||||
}
|
||||
|
||||
#endregion
|
||||
#endregion
|
||||
|
||||
#region " Ex. Upload and Download "
|
||||
#region " Ex. Upload, Download and Delete file "
|
||||
|
||||
[HttpPost("upload"), DisableRequestSizeLimit]
|
||||
[AllowAnonymous]
|
||||
public async Task<ActionResult<ResponseObject>> UploadFile()
|
||||
{
|
||||
try
|
||||
{
|
||||
if (Request.Form.Files == null || Request.Form.Files.Count == 0)
|
||||
{
|
||||
return Error(GlobalMessages.NoFileToUpload);
|
||||
}
|
||||
/// <summary>
|
||||
/// ตัวอย่างในการเขียน api เพื่อทำการ upload file
|
||||
/// </summary>
|
||||
/// <returns></returns>
|
||||
/// <response code="200">เมื่อทำการอ่านข้อมูลจาก Relational Database สำเร็จ</response>
|
||||
/// <response code="400">ค่าตัวแปรที่ส่งมาไม่ถูกต้อง</response>
|
||||
/// <response code="401">ไม่ได้ Login เข้าระบบ</response>
|
||||
/// <response code="500">เมื่อเกิดข้อผิดพลาดในการทำงาน</response>
|
||||
[HttpPost("upload"), DisableRequestSizeLimit]
|
||||
[ProducesResponseType(StatusCodes.Status200OK)]
|
||||
[ProducesResponseType(StatusCodes.Status400BadRequest)]
|
||||
[ProducesResponseType(StatusCodes.Status401Unauthorized)]
|
||||
[ProducesResponseType(StatusCodes.Status500InternalServerError)]
|
||||
[AllowAnonymous]
|
||||
public async Task<ActionResult<ResponseObject>> UploadFile()
|
||||
{
|
||||
try
|
||||
{
|
||||
if (Request.Form.Files == null || Request.Form.Files.Count == 0)
|
||||
{
|
||||
return Error(GlobalMessages.NoFileToUpload);
|
||||
}
|
||||
|
||||
var file = Request.Form.Files[0];
|
||||
var doc = await _minioService.UploadFile(file);
|
||||
var file = Request.Form.Files[0];
|
||||
var doc = await _minioService.UploadFileAsync(file);
|
||||
|
||||
return Success(doc);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
return Error(ex);
|
||||
}
|
||||
}
|
||||
return Success(doc);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
return Error(ex);
|
||||
}
|
||||
}
|
||||
|
||||
#endregion
|
||||
[HttpGet("delete/{id:length(36)}")]
|
||||
[AllowAnonymous]
|
||||
public async Task<ActionResult<ResponseObject>> DeleteFile(Guid id)
|
||||
{
|
||||
try
|
||||
{
|
||||
await _minioService.DeleteFileAsync(id);
|
||||
|
||||
#region " จัดการรอบการสมัครสอบแข่งขัน "
|
||||
return Success();
|
||||
}
|
||||
catch(Exception ex)
|
||||
{
|
||||
return Error(ex);
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// แสดงข้อมูลรอบการสอบแข่งขัน
|
||||
/// </summary>
|
||||
/// <returns></returns>
|
||||
/// <response code="200">เมื่อทำการอ่านข้อมูลจาก Relational Database สำเร็จ</response>
|
||||
/// <response code="400">ค่าตัวแปรที่ส่งมาไม่ถูกต้อง</response>
|
||||
/// <response code="401">ไม่ได้ Login เข้าระบบ</response>
|
||||
/// <response code="500">เมื่อเกิดข้อผิดพลาดในการทำงาน</response>
|
||||
[HttpGet]
|
||||
[ProducesResponseType(StatusCodes.Status200OK)]
|
||||
[ProducesResponseType(StatusCodes.Status400BadRequest)]
|
||||
[ProducesResponseType(StatusCodes.Status401Unauthorized)]
|
||||
[ProducesResponseType(StatusCodes.Status500InternalServerError)]
|
||||
public async Task<ActionResult<ResponseObject>> GetsAsync()
|
||||
{
|
||||
try
|
||||
{
|
||||
var data = await _context.RecruitImports.AsQueryable()
|
||||
.OrderByDescending(x => x.Year)
|
||||
.ThenByDescending(x => x.Order)
|
||||
.ToListAsync();
|
||||
[HttpGet("download/{id:length(36)}")]
|
||||
[AllowAnonymous]
|
||||
public async Task<ActionResult<ResponseObject>> DownloadFile(Guid id)
|
||||
{
|
||||
try
|
||||
{
|
||||
var file_data = await _minioService.DownloadFileAsync(id);
|
||||
|
||||
return Success(data);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
return Error(ex);
|
||||
}
|
||||
}
|
||||
Response.Headers["Content-Disposition"] = $"inline; filename={file_data.FileName}";
|
||||
|
||||
/// <summary>
|
||||
/// แสดงข้อมูลรอบการสอบแข่งขัน
|
||||
/// </summary>
|
||||
/// <param name="id">รหัสรอบการสอบแข่งขัน</param>
|
||||
/// <returns></returns>
|
||||
/// <response code="200">เมื่อทำการอ่านข้อมูลจาก Relational Database สำเร็จ</response>
|
||||
/// <response code="400">ค่าตัวแปรที่ส่งมาไม่ถูกต้อง</response>
|
||||
/// <response code="401">ไม่ได้ Login เข้าระบบ</response>
|
||||
/// <response code="500">เมื่อเกิดข้อผิดพลาดในการทำงาน</response>
|
||||
[HttpGet("{id:length(24)}")]
|
||||
[ProducesResponseType(StatusCodes.Status200OK)]
|
||||
[ProducesResponseType(StatusCodes.Status400BadRequest)]
|
||||
[ProducesResponseType(StatusCodes.Status401Unauthorized)]
|
||||
[ProducesResponseType(StatusCodes.Status500InternalServerError)]
|
||||
public async Task<ActionResult<ResponseObject>> GetByIdAsync(Guid id)
|
||||
{
|
||||
try
|
||||
{
|
||||
var data = await _context.RecruitImports.AsQueryable()
|
||||
.Include(x => x.ImportFile)
|
||||
.Include(x => x.Recruits)
|
||||
.ThenInclude(x => x.Addresses)
|
||||
.Include(x => x.Recruits)
|
||||
.ThenInclude(x => x.Occupations)
|
||||
.Include(x => x.Recruits)
|
||||
.ThenInclude(x => x.Certificates)
|
||||
.Include(x => x.Recruits)
|
||||
.ThenInclude(x => x.Educations)
|
||||
.Include(x => x.Recruits)
|
||||
.ThenInclude(x => x.Payments)
|
||||
.Include(x => x.Recruits)
|
||||
.ThenInclude(x => x.Documents)
|
||||
.ThenInclude(x => x.DocumentFile)
|
||||
.FirstOrDefaultAsync(x => x.Id == id);
|
||||
var ret = new FileContentResult(file_data.FileContent, file_data.FileType)
|
||||
{
|
||||
FileDownloadName = file_data.FileName
|
||||
};
|
||||
|
||||
return Success(data);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
return Error(ex);
|
||||
}
|
||||
}
|
||||
return ret;
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
return Error(ex);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// เพิ่มข้อมูลรอบการจัดสอบแข่งขัน
|
||||
/// </summary>
|
||||
/// <param name="req">Request parameters</param>
|
||||
/// <returns></returns>
|
||||
/// <response code="200">เมื่อทำการอ่านข้อมูลจาก Relational Database สำเร็จ</response>
|
||||
/// <response code="400">ค่าตัวแปรที่ส่งมาไม่ถูกต้อง</response>
|
||||
/// <response code="401">ไม่ได้ Login เข้าระบบ</response>
|
||||
/// <response code="500">เมื่อเกิดข้อผิดพลาดในการทำงาน</response>
|
||||
[HttpPost("period")]
|
||||
[ProducesResponseType(StatusCodes.Status200OK)]
|
||||
[ProducesResponseType(StatusCodes.Status400BadRequest)]
|
||||
[ProducesResponseType(StatusCodes.Status401Unauthorized)]
|
||||
[ProducesResponseType(StatusCodes.Status500InternalServerError)]
|
||||
public async Task<ActionResult<ResponseObject>> PostPeriodAsync([FromBody] PostRecruitImportRequest req)
|
||||
{
|
||||
try
|
||||
{
|
||||
if (req == null)
|
||||
return Error(GlobalMessages.InvalidRequestParam, (int)HttpStatusCode.BadRequest);
|
||||
#endregion
|
||||
|
||||
await _context.RecruitImports.AddAsync(new RecruitImport
|
||||
{
|
||||
Year = req.Year,
|
||||
Name = req.Name,
|
||||
Order = req.Order,
|
||||
});
|
||||
#region " จัดการรอบการสมัครสอบแข่งขัน "
|
||||
|
||||
await _context.SaveChangesAsync();
|
||||
/// <summary>
|
||||
/// แสดงข้อมูลรอบการสอบแข่งขัน
|
||||
/// </summary>
|
||||
/// <returns></returns>
|
||||
/// <response code="200">เมื่อทำการอ่านข้อมูลจาก Relational Database สำเร็จ</response>
|
||||
/// <response code="400">ค่าตัวแปรที่ส่งมาไม่ถูกต้อง</response>
|
||||
/// <response code="401">ไม่ได้ Login เข้าระบบ</response>
|
||||
/// <response code="500">เมื่อเกิดข้อผิดพลาดในการทำงาน</response>
|
||||
[HttpGet]
|
||||
[ProducesResponseType(StatusCodes.Status200OK)]
|
||||
[ProducesResponseType(StatusCodes.Status400BadRequest)]
|
||||
[ProducesResponseType(StatusCodes.Status401Unauthorized)]
|
||||
[ProducesResponseType(StatusCodes.Status500InternalServerError)]
|
||||
public async Task<ActionResult<ResponseObject>> GetsAsync()
|
||||
{
|
||||
try
|
||||
{
|
||||
var data = await _context.RecruitImports.AsQueryable()
|
||||
.OrderByDescending(x => x.Year)
|
||||
.ThenByDescending(x => x.Order)
|
||||
.ToListAsync();
|
||||
|
||||
return Success();
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
return Error(ex);
|
||||
}
|
||||
}
|
||||
return Success(data);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
return Error(ex);
|
||||
}
|
||||
}
|
||||
|
||||
#endregion
|
||||
/// <summary>
|
||||
/// แสดงข้อมูลรอบการสอบแข่งขัน
|
||||
/// </summary>
|
||||
/// <param name="id">รหัสรอบการสอบแข่งขัน</param>
|
||||
/// <returns></returns>
|
||||
/// <response code="200">เมื่อทำการอ่านข้อมูลจาก Relational Database สำเร็จ</response>
|
||||
/// <response code="400">ค่าตัวแปรที่ส่งมาไม่ถูกต้อง</response>
|
||||
/// <response code="401">ไม่ได้ Login เข้าระบบ</response>
|
||||
/// <response code="500">เมื่อเกิดข้อผิดพลาดในการทำงาน</response>
|
||||
[HttpGet("{id:length(36)}")]
|
||||
[ProducesResponseType(StatusCodes.Status200OK)]
|
||||
[ProducesResponseType(StatusCodes.Status400BadRequest)]
|
||||
[ProducesResponseType(StatusCodes.Status401Unauthorized)]
|
||||
[ProducesResponseType(StatusCodes.Status500InternalServerError)]
|
||||
public async Task<ActionResult<ResponseObject>> GetByIdAsync(Guid id)
|
||||
{
|
||||
try
|
||||
{
|
||||
var data = await _context.RecruitImports.AsQueryable()
|
||||
.Include(x => x.ImportFile)
|
||||
.Include(x => x.Recruits)
|
||||
.ThenInclude(x => x.Addresses)
|
||||
.Include(x => x.Recruits)
|
||||
.ThenInclude(x => x.Occupations)
|
||||
.Include(x => x.Recruits)
|
||||
.ThenInclude(x => x.Certificates)
|
||||
.Include(x => x.Recruits)
|
||||
.ThenInclude(x => x.Educations)
|
||||
.Include(x => x.Recruits)
|
||||
.ThenInclude(x => x.Payments)
|
||||
.Include(x => x.Recruits)
|
||||
.ThenInclude(x => x.Documents)
|
||||
.ThenInclude(x => x.DocumentFile)
|
||||
.FirstOrDefaultAsync(x => x.Id == id);
|
||||
|
||||
#endregion
|
||||
}
|
||||
return Success(data);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
return Error(ex);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// เพิ่มข้อมูลรอบการจัดสอบแข่งขัน
|
||||
/// </summary>
|
||||
/// <param name="req">Request parameters</param>
|
||||
/// <returns></returns>
|
||||
/// <response code="200">เมื่อทำการอ่านข้อมูลจาก Relational Database สำเร็จ</response>
|
||||
/// <response code="400">ค่าตัวแปรที่ส่งมาไม่ถูกต้อง</response>
|
||||
/// <response code="401">ไม่ได้ Login เข้าระบบ</response>
|
||||
/// <response code="500">เมื่อเกิดข้อผิดพลาดในการทำงาน</response>
|
||||
[HttpPost("period")]
|
||||
[ProducesResponseType(StatusCodes.Status200OK)]
|
||||
[ProducesResponseType(StatusCodes.Status400BadRequest)]
|
||||
[ProducesResponseType(StatusCodes.Status401Unauthorized)]
|
||||
[ProducesResponseType(StatusCodes.Status500InternalServerError)]
|
||||
public async Task<ActionResult<ResponseObject>> PostPeriodAsync([FromBody] PostRecruitImportRequest req)
|
||||
{
|
||||
try
|
||||
{
|
||||
if (req == null)
|
||||
return Error(GlobalMessages.InvalidRequestParam, (int)HttpStatusCode.BadRequest);
|
||||
|
||||
await _context.RecruitImports.AddAsync(new RecruitImport
|
||||
{
|
||||
Year = req.Year,
|
||||
Name = req.Name,
|
||||
Order = req.Order,
|
||||
});
|
||||
|
||||
await _context.SaveChangesAsync();
|
||||
|
||||
return Success();
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
return Error(ex);
|
||||
}
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#endregion
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue