From 9cf8135301c2457666c1ac29ae500c5d16135730 Mon Sep 17 00:00:00 2001 From: AnandaTon <125332905+anandaAiemvong@users.noreply.github.com> Date: Fri, 31 Mar 2023 18:01:42 +0700 Subject: [PATCH] =?UTF-8?q?=E0=B9=81=E0=B8=81=E0=B9=89=20api=20fileImageCa?= =?UTF-8?q?ndidate?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Controllers/CandidateController.cs | 113 ++++++++++------------------- Services/CandidateService.cs | 40 +++++++--- 2 files changed, 68 insertions(+), 85 deletions(-) diff --git a/Controllers/CandidateController.cs b/Controllers/CandidateController.cs index f905341..f4187f4 100644 --- a/Controllers/CandidateController.cs +++ b/Controllers/CandidateController.cs @@ -715,46 +715,33 @@ namespace BMA.EHR.Recurit.Exam.Service.Controllers return Error(ex); } } + /// - /// ตัวอย่างในการเขียน api เพื่อทำการ upload file - /// - /// - /// เมื่อทำการอ่านข้อมูลจาก Relational Database สำเร็จ - /// ค่าตัวแปรที่ส่งมาไม่ถูกต้อง - /// ไม่ได้ Login เข้าระบบ - /// เมื่อเกิดข้อผิดพ ลาดในการทำงาน - // [HttpPost("upload"), DisableRequestSizeLimit] - // [ProducesResponseType(StatusCodes.Status200OK)] - // [ProducesResponseType(StatusCodes.Status400BadRequest)] - // [ProducesResponseType(StatusCodes.Status401Unauthorized)] - // [ProducesResponseType(StatusCodes.Status500InternalServerError)] - // [AllowAnonymous] - // public async Task> 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.UploadFileAsync(file); - - // return Success(doc); - // } - // catch (Exception ex) - // { - // return Error(ex); - // } - // } - [HttpGet("delete/{id:length(36)}")] + /// อัปโหลดรูปถ่ายผู้สมัคร + /// + /// รหัสรอบสมัคร + /// + /// เมื่ออัปโหลดรูปถ่ายผู้สมัครสำเร็จ + /// ค่าตัวแปรที่ส่งมาไม่ถูกต้อง + /// ไม่ได้ Login เข้าระบบ + /// เมื่อเกิดข้อผิดพลาดในการทำงาน + [HttpPut("profile-image/{examId:length(36)}"), DisableRequestSizeLimit] + [ProducesResponseType(StatusCodes.Status200OK)] + [ProducesResponseType(StatusCodes.Status400BadRequest)] + [ProducesResponseType(StatusCodes.Status401Unauthorized)] + [ProducesResponseType(StatusCodes.Status500InternalServerError)] [AllowAnonymous] - public async Task> DeleteFile(Guid id) + public async Task> UpdateProfileImageCandidateService(string examId) { try { - await _minioService.DeleteFileAsync(id); + if (Request.Form.Files == null || Request.Form.Files.Count == 0) + { + return Error(GlobalMessages.NoFileToUpload); + } + + var file = Request.Form.Files[0]; + await _candidateService.UpdateAsyncProfileImage(examId, file); return Success(); } @@ -764,30 +751,6 @@ namespace BMA.EHR.Recurit.Exam.Service.Controllers } } - [HttpGet("download/{id:length(36)}")] - [AllowAnonymous] - public async Task> DownloadFile(Guid id) - { - try - { - var file_data = await _minioService.DownloadFileAsync(id); - - Response.Headers["Content-Disposition"] = $"inline; filename={file_data.FileName}"; - - var ret = new FileContentResult(file_data.FileContent, file_data.FileType) - { - FileDownloadName = file_data.FileName - }; - - return ret; - } - catch (Exception ex) - { - return Error(ex); - } - } - - /// /// อัปเอกสารหลักฐาน /// @@ -828,23 +791,23 @@ namespace BMA.EHR.Recurit.Exam.Service.Controllers /// เมื่อลบเอกสารหลักฐานสำเร็จ /// ไม่ได้ Login เข้าระบบ /// เมื่อเกิดข้อผิดพลาดในการทำงาน - [HttpDelete("upload/{examId:length(36)}"), DisableRequestSizeLimit] - [ProducesResponseType(StatusCodes.Status200OK)] - [ProducesResponseType(StatusCodes.Status401Unauthorized)] - [ProducesResponseType(StatusCodes.Status500InternalServerError)] - public async Task> DeleteFileCandidateService(string documentId) - { - try - { - await _candidateService.DeleteAsyncDocument(documentId); + // [HttpDelete("upload/{examId:length(36)}"), DisableRequestSizeLimit] + // [ProducesResponseType(StatusCodes.Status200OK)] + // [ProducesResponseType(StatusCodes.Status401Unauthorized)] + // [ProducesResponseType(StatusCodes.Status500InternalServerError)] + // public async Task> DeleteFileCandidateService(string documentId) + // { + // try + // { + // await _candidateService.DeleteAsyncDocument(documentId); - return Success(); - } - catch (Exception ex) - { - return Error(ex); - } - } + // return Success(); + // } + // catch (Exception ex) + // { + // return Error(ex); + // } + // } /// /// โหลดไฟล์ diff --git a/Services/CandidateService.cs b/Services/CandidateService.cs index d5e4e8f..9ca3b48 100644 --- a/Services/CandidateService.cs +++ b/Services/CandidateService.cs @@ -550,6 +550,25 @@ namespace BMA.EHR.Recurit.Exam.Service.Services await _context.SaveChangesAsync(); } + public async Task UpdateAsyncProfileImage(string examId, IFormFile file) + { + var candidateId = await CreateAsyncCandidate(examId); + + var candidate = await _context.Candidates.AsQueryable() + .FirstOrDefaultAsync(x => x.Id == Guid.Parse(candidateId)); + + if (candidate == null) + throw new Exception(GlobalMessages.ExamNotFound); + + var doc = await _minioService.UploadFileAsync(file); + + var document = await _context.Documents.AsQueryable() + .FirstOrDefaultAsync(x => x.Id == doc.Id); + + candidate.ProfileImg = document; + await _context.SaveChangesAsync(); + } + public async Task UpdateAsyncAddress(string examId, CandidateAddressResponseItem updated) { var candidateId = await CreateAsyncCandidate(examId); @@ -731,18 +750,19 @@ namespace BMA.EHR.Recurit.Exam.Service.Services await _context.SaveChangesAsync(); } - public async Task DeleteAsyncDocument(string documentId) - { - var document = await _context.Documents.AsQueryable() - .FirstOrDefaultAsync(x => x.Id == Guid.Parse(documentId)); + // public async Task DeleteAsyncDocument(string documentId) + // { + // // var document = await _context.Documents.AsQueryable() + // // .FirstOrDefaultAsync(x => x.Id == Guid.Parse(documentId)); - if (document == null) - throw new Exception(GlobalMessages.FileNotFoundOnServer); + // // if (document == null) + // // throw new Exception(GlobalMessages.FileNotFoundOnServer); - _context.Documents.Remove(document); - - await _context.SaveChangesAsync(); - } + // // _context.Documents.Remove(document); + // var doc = await _minioService.DeleteFileAsync(documentId); + // return "uy"; + // // await _context.SaveChangesAsync(); + // } public async Task CreateAsyncCareer(string examId, CandidateCareerResponseItem updated) {