สร้าง api อัพเอกสาร

This commit is contained in:
Kittapath 2023-03-31 15:01:40 +07:00
parent 3ff5a4fa46
commit 15e4d21a6f
13 changed files with 3037 additions and 5 deletions

View file

@ -4,6 +4,8 @@ using BMA.EHR.Recurit.Exam.Service.Data;
using BMA.EHR.Recurit.Exam.Service.Models;
using BMA.EHR.Recurit.Exam.Service.Request;
using BMA.EHR.Recurit.Exam.Service.Response;
using BMA.EHR.Recurit.Exam.Service.Responses.Document;
using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore;
namespace BMA.EHR.Recurit.Exam.Service.Services
@ -14,16 +16,19 @@ namespace BMA.EHR.Recurit.Exam.Service.Services
private readonly ApplicationDbContext _context;
private readonly IHttpContextAccessor _httpContextAccessor;
private readonly MinIOService _minioService;
#endregion
#region " Constructor and Destructor "
public CandidateService(ApplicationDbContext context,
IHttpContextAccessor httpContextAccessor)
IHttpContextAccessor httpContextAccessor,
MinIOService minioService)
{
_context = context;
_httpContextAccessor = httpContextAccessor;
_minioService = minioService;
}
#endregion
@ -221,6 +226,28 @@ namespace BMA.EHR.Recurit.Exam.Service.Services
.ToListAsync();
}
public async Task<IEnumerable<FileListResponse?>> GetsAsyncFileUpload(string examId)
{
var exam = await _context.PeriodExams.AsQueryable()
.FirstOrDefaultAsync(x => x.Id == Guid.Parse(examId));
if (exam == null)
throw new Exception(GlobalMessages.ExamNotFound);
var candidate = await _context.Candidates.AsQueryable()
.FirstOrDefaultAsync(x => x.PeriodExam == exam && x.UserId == UserId);
return await _context.CandidateDocuments.AsQueryable()
.Where(x => x.Candidate == candidate)
.Select(x => new FileListResponse
{
FileName = x.Document == null ? "" : x.Document.FileName,
FileType = x.Document == null ? "" : x.Document.FileType,
FileSize = x.Document == null ? 0 : x.Document.FileSize,
})
.ToListAsync(); ;
}
public async Task<bool> GetsAsyncRegisterExam(string examId)
{
var exam = await _context.PeriodExams.AsQueryable()
@ -679,6 +706,44 @@ namespace BMA.EHR.Recurit.Exam.Service.Services
await _context.SaveChangesAsync();
}
public async Task UpdateAsyncDocument(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);
var candidateDocument = new CandidateDocument
{
Candidate = candidate,
Document = document,
};
await _context.CandidateDocuments.AddAsync(candidateDocument);
await _context.SaveChangesAsync();
}
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);
_context.Documents.Remove(document);
await _context.SaveChangesAsync();
}
public async Task CreateAsyncCareer(string examId, CandidateCareerResponseItem updated)
{
var candidateId = await CreateAsyncCandidate(examId);