แก้ api เพิ่มเติมการ upload image and document

This commit is contained in:
Suphonchai Phoonsawat 2023-05-02 22:59:01 +07:00
parent 86ec5de116
commit 6acd497afe
23 changed files with 4081 additions and 53 deletions

View file

@ -5,16 +5,20 @@ using System.Threading.Tasks;
using Microsoft.EntityFrameworkCore;
using BMA.EHR.Recruit.Service.Data;
using BMA.EHR.Recruit.Service.Models.Recruits;
using BMA.EHR.Recruit.Service.Core;
namespace BMA.EHR.Recruit.Service.Services
{
public class RecruitService
{
private readonly ApplicationDbContext _context;
private readonly MinIOService _minIOService;
public RecruitService(ApplicationDbContext context)
public RecruitService(ApplicationDbContext context,
MinIOService minIOService)
{
_context = context;
_minIOService = minIOService;
}
public int GetExamCount(string citizenId)
@ -61,5 +65,87 @@ namespace BMA.EHR.Recruit.Service.Services
return valid;
}
public async Task UpdateDocAsync(Guid ImportId, IFormFileCollection files)
{
var periodExam = await _context.RecruitImports.AsQueryable()
.FirstOrDefaultAsync(x => x.Id == ImportId);
if (periodExam == null)
throw new Exception(GlobalMessages.DataNotFound);
foreach (var file in files)
{
var doc = await _minIOService.UploadFileAsync(file);
var periodExamDocument = new RecruitImportDocument
{
RecruitImportId = ImportId,
DocumentId = doc.Id,
};
await _context.RecruitImportDocuments.AddAsync(periodExamDocument);
}
await _context.SaveChangesAsync();
}
public async Task UpdateImageAsync(Guid ImportId, IFormFileCollection files)
{
var periodExam = await _context.RecruitImports.AsQueryable()
.FirstOrDefaultAsync(x => x.Id == ImportId);
if (periodExam == null)
throw new Exception(GlobalMessages.DataNotFound);
foreach (var file in files)
{
var doc = await _minIOService.UploadFileAsync(file);
var periodExamImage = new RecruitImportImage
{
RecruitImportId = ImportId,
DocumentId = doc.Id,
};
await _context.RecruitImportImages.AddAsync(periodExamImage);
}
await _context.SaveChangesAsync();
}
public async Task DeleteImageAsync(Guid id)
{
var image = await _context.RecruitImportImages.AsQueryable()
.Include(x => x.Document)
.FirstOrDefaultAsync(x => x.Id == id);
if (image == null)
throw new Exception(GlobalMessages.DataNotFound);
var doc_id = image.Document.Id;
_context.RecruitImportImages.Remove(image);
await _context.SaveChangesAsync();
await _minIOService.DeleteFileAsync(doc_id);
}
public async Task DeleteDocAsync(Guid id)
{
var doc = await _context.RecruitImportDocuments.AsQueryable()
.Include(x => x.Document)
.FirstOrDefaultAsync(x => x.Id == id);
if (doc == null)
throw new Exception(GlobalMessages.DataNotFound);
var doc_id = doc.Document.Id;
_context.RecruitImportDocuments.Remove(doc);
await _context.SaveChangesAsync();
await _minIOService.DeleteFileAsync(doc_id);
}
}
}