using System.Security.Claims; using BMA.EHR.Recurit.Exam.Service.Core; 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 { public class CandidateService { #region " Fields " private readonly ApplicationDbContext _context; private readonly MetadataDbContext _contextMetadata; private readonly IHttpContextAccessor _httpContextAccessor; private readonly MinIOService _minioService; private readonly MailService _mailService; #endregion #region " Constructor and Destructor " public CandidateService(ApplicationDbContext context, MetadataDbContext contextMetadata, IHttpContextAccessor httpContextAccessor, MinIOService minioService, MailService mailService) { _context = context; _contextMetadata = contextMetadata; _httpContextAccessor = httpContextAccessor; _minioService = minioService; _mailService = mailService; } #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 " public async Task GetsAsync(string candidateId) { var candidate = await _context.Candidates.AsQueryable() .FirstOrDefaultAsync(x => x.Id == Guid.Parse(candidateId)); if (candidate == null) throw new Exception(GlobalMessages.CandidateNotFound); return candidate; } public async Task GetsAsyncInformation(string examId, string positionId) { var exam = await _context.PeriodExams.AsQueryable() .Where(p => p.CheckDisability == false) .FirstOrDefaultAsync(x => x.Id == Guid.Parse(examId)); if (exam == null) throw new Exception(GlobalMessages.ExamNotFound); if (positionId != "00000000-0000-0000-0000-000000000000") { var position = await _context.PositionExams.AsQueryable() .FirstOrDefaultAsync(x => x.Id == Guid.Parse(positionId) && x.PeriodExam == exam); if (position == null) throw new Exception(GlobalMessages.PositionExamNotFound); return await _context.Candidates.AsQueryable() .Where(x => x.PeriodExam == exam && x.UserId == UserId && x.PositionExam == position) .Select(x => new CandidateInformationResponseItem { Prefix = x.PrefixName, PrefixId = x.PrefixId != null ? x.PrefixId.ToString() : null, FirstName = x.FirstName, LastName = x.LastName, Nationality = x.Nationality, DateOfBirth = x.DateOfBirth, Relationship = x.RelationshipName, RelationshipId = x.RelationshipId != null ? x.RelationshipId.ToString() : null, CitizenProvince = x.CitizenProvinceName, CitizenProvinceId = x.CitizenProvinceId != null ? x.CitizenProvinceId.ToString() : null, CitizenDistrict = x.CitizenDistrictName, CitizenDistrictId = x.CitizenDistrictId != null ? x.CitizenDistrictId.ToString() : null, CitizenDate = x.CitizenDate, Email = x.Email, CitizenId = x.CitizenId, Telephone = x.Telephone, MobilePhone = x.MobilePhone, Knowledge = x.Knowledge, }) .FirstOrDefaultAsync(); } else { return await _context.Candidates.AsQueryable() .Where(x => x.PeriodExam == exam && x.UserId == UserId) .Select(x => new CandidateInformationResponseItem { Prefix = x.PrefixName, PrefixId = x.PrefixId != null ? x.PrefixId.ToString() : null, FirstName = x.FirstName, LastName = x.LastName, Nationality = x.Nationality, DateOfBirth = x.DateOfBirth, Relationship = x.RelationshipName, RelationshipId = x.RelationshipId != null ? x.RelationshipId.ToString() : null, CitizenProvince = x.CitizenProvinceName, CitizenProvinceId = x.CitizenProvinceId != null ? x.CitizenProvinceId.ToString() : null, CitizenDistrict = x.CitizenDistrictName, CitizenDistrictId = x.CitizenDistrictId != null ? x.CitizenDistrictId.ToString() : null, CitizenDate = x.CitizenDate, Email = x.Email, CitizenId = x.CitizenId, Telephone = x.Telephone, MobilePhone = x.MobilePhone, Knowledge = x.Knowledge, }) .FirstOrDefaultAsync(); } } public async Task GetsAsyncAddress(string examId, string positionId) { var exam = await _context.PeriodExams.AsQueryable() .Where(p => p.CheckDisability == false) .FirstOrDefaultAsync(x => x.Id == Guid.Parse(examId)); if (exam == null) throw new Exception(GlobalMessages.ExamNotFound); if (positionId != "00000000-0000-0000-0000-000000000000") { var position = await _context.PositionExams.AsQueryable() .FirstOrDefaultAsync(x => x.Id == Guid.Parse(positionId) && x.PeriodExam == exam); if (position == null) throw new Exception(GlobalMessages.PositionExamNotFound); return await _context.Candidates.AsQueryable() .Where(x => x.PeriodExam == exam && x.UserId == UserId && x.PositionExam == position) .Select(x => new CandidateAddressResponseItem { RegistAddress = x.RegistAddress, RegistProvince = x.RegistProvinceName, RegistProvinceId = x.RegistProvinceId != null ? x.RegistProvinceId.ToString() : null, RegistDistrict = x.RegistDistrictName, RegistDistrictId = x.RegistDistrictId != null ? x.RegistDistrictId.ToString() : null, RegistSubDistrict = x.RegistSubDistrictName, RegistSubDistrictId = x.RegistSubDistrictId != null ? x.RegistSubDistrictId.ToString() : null, RegistZipCode = x.RegistZipCode, RegistSame = x.RegistSame, CurrentAddress = x.CurrentAddress, CurrentProvince = x.CurrentProvinceName, CurrentProvinceId = x.CurrentProvinceId != null ? x.CurrentProvinceId.ToString() : null, CurrentDistrict = x.CurrentDistrictName, CurrentDistrictId = x.CurrentDistrictId != null ? x.CurrentDistrictId.ToString() : null, CurrentSubDistrict = x.CurrentSubDistrictName, CurrentSubDistrictId = x.CurrentSubDistrictId != null ? x.CurrentSubDistrictId.ToString() : null, CurrentZipCode = x.CurrentZipCode, }) .FirstOrDefaultAsync(); } else { return await _context.Candidates.AsQueryable() .Where(x => x.PeriodExam == exam && x.UserId == UserId) .Select(x => new CandidateAddressResponseItem { RegistAddress = x.RegistAddress, RegistProvince = x.RegistProvinceName, RegistProvinceId = x.RegistProvinceId != null ? x.RegistProvinceId.ToString() : null, RegistDistrict = x.RegistDistrictName, RegistDistrictId = x.RegistDistrictId != null ? x.RegistDistrictId.ToString() : null, RegistSubDistrict = x.RegistSubDistrictName, RegistSubDistrictId = x.RegistSubDistrictId != null ? x.RegistSubDistrictId.ToString() : null, RegistZipCode = x.RegistZipCode, RegistSame = x.RegistSame, CurrentAddress = x.CurrentAddress, CurrentProvince = x.CurrentProvinceName, CurrentProvinceId = x.CurrentProvinceId != null ? x.CurrentProvinceId.ToString() : null, CurrentDistrict = x.CurrentDistrictName, CurrentDistrictId = x.CurrentDistrictId != null ? x.CurrentDistrictId.ToString() : null, CurrentSubDistrict = x.CurrentSubDistrictName, CurrentSubDistrictId = x.CurrentSubDistrictId != null ? x.CurrentSubDistrictId.ToString() : null, CurrentZipCode = x.CurrentZipCode, }) .FirstOrDefaultAsync(); } } public async Task GetsAsyncFamily(string examId, string positionId) { var exam = await _context.PeriodExams.AsQueryable() .Where(p => p.CheckDisability == false) .FirstOrDefaultAsync(x => x.Id == Guid.Parse(examId)); if (exam == null) throw new Exception(GlobalMessages.ExamNotFound); if (positionId != "00000000-0000-0000-0000-000000000000") { var position = await _context.PositionExams.AsQueryable() .FirstOrDefaultAsync(x => x.Id == Guid.Parse(positionId) && x.PeriodExam == exam); if (position == null) throw new Exception(GlobalMessages.PositionExamNotFound); return await _context.Candidates.AsQueryable() .Where(x => x.PeriodExam == exam && x.UserId == UserId && x.PositionExam == position) .Select(x => new CandidateFamilyResponseItem { Marry = x.Marry, MarryPrefix = x.MarryPrefixName, MarryPrefixId = x.MarryPrefixId != null ? x.MarryPrefixId.ToString() : null, MarryFirstName = x.MarryFirstName, MarryLastName = x.MarryLastName, MarryOccupation = x.MarryOccupation, MarryNationality = x.MarryNationality, FatherPrefix = x.FatherPrefixName, FatherPrefixId = x.FatherPrefixId != null ? x.FatherPrefixId.ToString() : null, FatherFirstName = x.FatherFirstName, FatherLastName = x.FatherLastName, FatherOccupation = x.FatherOccupation, FatherNationality = x.FatherNationality, MotherPrefix = x.MotherPrefixName, MotherPrefixId = x.MotherPrefixId != null ? x.MotherPrefixId.ToString() : null, MotherFirstName = x.MotherFirstName, MotherLastName = x.MotherLastName, MotherOccupation = x.MotherOccupation, MotherNationality = x.MotherNationality, }) .FirstOrDefaultAsync(); } else { return await _context.Candidates.AsQueryable() .Where(x => x.PeriodExam == exam && x.UserId == UserId) .Select(x => new CandidateFamilyResponseItem { Marry = x.Marry, MarryPrefix = x.MarryPrefixName, MarryPrefixId = x.MarryPrefixId != null ? x.MarryPrefixId.ToString() : null, MarryFirstName = x.MarryFirstName, MarryLastName = x.MarryLastName, MarryOccupation = x.MarryOccupation, MarryNationality = x.MarryNationality, FatherPrefix = x.FatherPrefixName, FatherPrefixId = x.FatherPrefixId != null ? x.FatherPrefixId.ToString() : null, FatherFirstName = x.FatherFirstName, FatherLastName = x.FatherLastName, FatherOccupation = x.FatherOccupation, FatherNationality = x.FatherNationality, MotherPrefix = x.MotherPrefixName, MotherPrefixId = x.MotherPrefixId != null ? x.MotherPrefixId.ToString() : null, MotherFirstName = x.MotherFirstName, MotherLastName = x.MotherLastName, MotherOccupation = x.MotherOccupation, MotherNationality = x.MotherNationality, }) .FirstOrDefaultAsync(); } } public async Task GetsAsyncOccupation(string examId, string positionId) { var exam = await _context.PeriodExams.AsQueryable() .Where(p => p.CheckDisability == false) .FirstOrDefaultAsync(x => x.Id == Guid.Parse(examId)); if (exam == null) throw new Exception(GlobalMessages.ExamNotFound); if (positionId != "00000000-0000-0000-0000-000000000000") { var position = await _context.PositionExams.AsQueryable() .FirstOrDefaultAsync(x => x.Id == Guid.Parse(positionId) && x.PeriodExam == exam); if (position == null) throw new Exception(GlobalMessages.PositionExamNotFound); return await _context.Candidates.AsQueryable() .Where(x => x.PeriodExam == exam && x.UserId == UserId && x.PositionExam == position) .Select(x => new CandidateOccupationResponseItem { OccupationType = x.OccupationType, OccupationCompany = x.OccupationCompany, OccupationDepartment = x.OccupationDepartment, OccupationEmail = x.OccupationEmail, OccupationTelephone = x.OccupationTelephone, OccupationPosition = x.OccupationPosition, }) .FirstOrDefaultAsync(); } else { return await _context.Candidates.AsQueryable() .Where(x => x.PeriodExam == exam && x.UserId == UserId) .Select(x => new CandidateOccupationResponseItem { OccupationType = x.OccupationType, OccupationCompany = x.OccupationCompany, OccupationDepartment = x.OccupationDepartment, OccupationEmail = x.OccupationEmail, OccupationTelephone = x.OccupationTelephone, OccupationPosition = x.OccupationPosition, }) .FirstOrDefaultAsync(); } } public async Task> GetsAsyncCareer(string examId, string positionId) { var exam = await _context.PeriodExams.AsQueryable() .Where(p => p.CheckDisability == false) .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); if (positionId != "00000000-0000-0000-0000-000000000000") { var position = await _context.PositionExams.AsQueryable() .FirstOrDefaultAsync(x => x.Id == Guid.Parse(positionId) && x.PeriodExam == exam); if (position == null) throw new Exception(GlobalMessages.PositionExamNotFound); candidate = await _context.Candidates.AsQueryable() .FirstOrDefaultAsync(x => x.PeriodExam == exam && x.UserId == UserId && x.PositionExam == position); } if (candidate == null) throw new Exception(GlobalMessages.CandidateNotFound); return await _context.Careers.AsQueryable() .Where(x => x.Candidate == candidate) .OrderBy(d => d.DurationStart) .ToListAsync(); } public async Task> GetsAsyncEducation(string examId, string positionId) { var exam = await _context.PeriodExams.AsQueryable() .Where(p => p.CheckDisability == false) .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); if (positionId != "00000000-0000-0000-0000-000000000000") { var position = await _context.PositionExams.AsQueryable() .FirstOrDefaultAsync(x => x.Id == Guid.Parse(positionId) && x.PeriodExam == exam); if (position == null) throw new Exception(GlobalMessages.PositionExamNotFound); candidate = await _context.Candidates.AsQueryable() .FirstOrDefaultAsync(x => x.PeriodExam == exam && x.UserId == UserId && x.PositionExam == position); } if (candidate == null) throw new Exception(GlobalMessages.CandidateNotFound); return await _context.Educations.AsQueryable() .Where(x => x.Candidate == candidate) .OrderBy(d => d.DurationStart) .ToListAsync(); } public async Task> GetsAsyncFileUpload(string examId, string positionId) { var exam = await _context.PeriodExams.AsQueryable() .Where(p => p.CheckDisability == false) .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); if (positionId != "00000000-0000-0000-0000-000000000000") { var position = await _context.PositionExams.AsQueryable() .FirstOrDefaultAsync(x => x.Id == Guid.Parse(positionId) && x.PeriodExam == exam); if (position == null) throw new Exception(GlobalMessages.PositionExamNotFound); candidate = await _context.Candidates.AsQueryable() .FirstOrDefaultAsync(x => x.PeriodExam == exam && x.UserId == UserId && x.PositionExam == position); } if (candidate == null) throw new Exception(GlobalMessages.CandidateNotFound); var document = await _context.CandidateDocuments.AsQueryable() .Where(x => x.Candidate == candidate) .OrderBy(x => x.CreatedAt) .Select(x => new FileListResponse { Id = x.Document == null ? "" : x.Document.Id.ToString(), FileName = x.Document == null ? "" : x.Document.FileName, FileType = x.Document == null ? "" : x.Document.FileType, FileSize = x.Document == null ? 0 : x.Document.FileSize, Detail = x.Document == null ? "" : x.Document.Id.ToString(), }) .ToListAsync(); var i = 0; foreach (var item in document) { if (document[i].Detail != null && document[i].Detail != "") document[i].Detail = _minioService.ImagesPath(Guid.Parse(document[i].Detail)).Result; i++; } return document; } public async Task> GetsAsyncAdminFileUpload(string candidateId) { var candidate = await _context.Candidates.AsQueryable() .FirstOrDefaultAsync(x => x.Id == Guid.Parse(candidateId)); if (candidate == null) throw new Exception(GlobalMessages.CandidateNotFound); var document = await _context.CandidateDocuments.AsQueryable() .Where(x => x.Candidate == candidate) .OrderBy(x => x.CreatedAt) .Select(x => new FileListResponse { Id = x.Document == null ? "" : x.Document.Id.ToString(), FileName = x.Document == null ? "" : x.Document.FileName, FileType = x.Document == null ? "" : x.Document.FileType, FileSize = x.Document == null ? 0 : x.Document.FileSize, Detail = x.Document == null ? "" : x.Document.Id.ToString(), }) .ToListAsync(); var i = 0; foreach (var item in document) { if (document[i].Detail != null && document[i].Detail != "") document[i].Detail = _minioService.ImagesPath(Guid.Parse(document[i].Detail)).Result; i++; } return document; } public async Task GetsAsyncProfileImage(string examId, string positionId) { var exam = await _context.PeriodExams.AsQueryable() .Where(p => p.CheckDisability == false) .FirstOrDefaultAsync(x => x.Id == Guid.Parse(examId)); if (exam == null) throw new Exception(GlobalMessages.ExamNotFound); var candidate = await _context.Candidates.AsQueryable() .Include(x => x.ProfileImg) .Where(x => x.PeriodExam == exam && x.UserId == UserId) .FirstOrDefaultAsync(); if (positionId != "00000000-0000-0000-0000-000000000000") { var position = await _context.PositionExams.AsQueryable() .FirstOrDefaultAsync(x => x.Id == Guid.Parse(positionId) && x.PeriodExam == exam); if (position == null) throw new Exception(GlobalMessages.PositionExamNotFound); candidate = await _context.Candidates.AsQueryable() .Include(x => x.ProfileImg) .Where(x => x.PeriodExam == exam && x.UserId == UserId && x.PositionExam == position) .FirstOrDefaultAsync(); } if (candidate == null) throw new Exception(GlobalMessages.CandidateNotFound); if (candidate.ProfileImg != null) candidate.ProfileImg.Detail = _minioService.ImagesPath(candidate.ProfileImg.Id).Result; return candidate.ProfileImg == null ? "" : candidate.ProfileImg.Detail; } public async Task GetsAsyncAdminProfileImage(string candidateId) { var candidate = await _context.Candidates.AsQueryable() .Include(x => x.ProfileImg) .FirstOrDefaultAsync(x => x.Id == Guid.Parse(candidateId)); if (candidate == null) throw new Exception(GlobalMessages.CandidateNotFound); if (candidate.ProfileImg != null) candidate.ProfileImg.Detail = _minioService.ImagesPath(candidate.ProfileImg.Id).Result; return candidate.ProfileImg == null ? "" : candidate.ProfileImg.Detail; } public async Task GetsAsyncPaymentImg(string examId, string positionId) { var exam = await _context.PeriodExams.AsQueryable() .Where(p => p.CheckDisability == false) .FirstOrDefaultAsync(x => x.Id == Guid.Parse(examId)); if (exam == null) throw new Exception(GlobalMessages.ExamNotFound); var candidate = await _context.Candidates.AsQueryable() .Include(x => x.PaymentImg) .Where(x => x.PeriodExam == exam && x.UserId == UserId) .FirstOrDefaultAsync(); if (positionId != "00000000-0000-0000-0000-000000000000") { var position = await _context.PositionExams.AsQueryable() .FirstOrDefaultAsync(x => x.Id == Guid.Parse(positionId) && x.PeriodExam == exam); if (position == null) throw new Exception(GlobalMessages.PositionExamNotFound); candidate = await _context.Candidates.AsQueryable() .Include(x => x.PaymentImg) .Where(x => x.PeriodExam == exam && x.UserId == UserId && x.PositionExam == position) .FirstOrDefaultAsync(); } if (candidate == null) throw new Exception(GlobalMessages.CandidateNotFound); if (candidate.PaymentImg != null) candidate.PaymentImg.Detail = _minioService.ImagesPath(candidate.PaymentImg.Id).Result; return new PaymentImgResponse { PaymentImg = candidate.PaymentImg == null ? "" : candidate.PaymentImg.Detail, RejectDetail = candidate.RejectDetail }; } public async Task GetsAsyncPaymentImgCandidate(string candidateId) { var candidate = await _context.Candidates.AsQueryable() .Include(x => x.PaymentImg) .Where(x => x.Id == Guid.Parse(candidateId)) .FirstOrDefaultAsync(); if (candidate == null) throw new Exception(GlobalMessages.CandidateNotFound); if (candidate.PaymentImg != null) candidate.PaymentImg.Detail = _minioService.ImagesPath(candidate.PaymentImg.Id).Result; return candidate.PaymentImg == null ? "" : candidate.PaymentImg.Detail.ToString(); } public async Task GetsAsyncRegisterExam(string examId, string positionId) { var exam = await _context.PeriodExams.AsQueryable() .Where(p => p.CheckDisability == false) .Include(x => x.BankExam) .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); var position = await _context.PositionExams.AsQueryable() .FirstOrDefaultAsync(x => x.Id == Guid.Parse(positionId) && x.PeriodExam == exam); if (positionId != "00000000-0000-0000-0000-000000000000") { // var position = await _context.PositionExams.AsQueryable() // .FirstOrDefaultAsync(x => x.Id == Guid.Parse(positionId) && x.PeriodExam == exam); if (position == null) throw new Exception(GlobalMessages.PositionExamNotFound); candidate = await _context.Candidates.AsQueryable() .FirstOrDefaultAsync(x => x.PeriodExam == exam && x.UserId == UserId && x.PositionExam == position); } var candidatePosition = await _context.Candidates.AsQueryable() .Include(x => x.PositionExam) .FirstOrDefaultAsync(x => x.PeriodExam == exam && x.UserId == UserId && x.Status != "register" && x.Status != "rejectRegister"); return new RequestStatusRegistry { Consend = candidate != null, Status = candidate == null ? null : candidate.Status, PositionExam = candidatePosition?.PositionExam, Bank = exam.BankExam.Count() > 0, Payment = exam.Fee > 0, Position = candidatePosition == null ? false : true }; } public async Task CreateAsyncCandidate(string examId, string positionId) { var exam = await _context.PeriodExams.AsQueryable() .Where(p => p.CheckDisability == false) .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); var position = await _context.PositionExams.AsQueryable() .FirstOrDefaultAsync(x => x.Id == Guid.Parse(positionId) && x.PeriodExam == exam); if (positionId != "00000000-0000-0000-0000-000000000000") { if (position == null) throw new Exception(GlobalMessages.PositionExamNotFound); _candidate = await _context.Candidates.AsQueryable() .FirstOrDefaultAsync(x => x.PeriodExam == exam && x.UserId == UserId && x.PositionExam == position); } if (_candidate == null) { var candidate = new Candidate { PeriodExam = exam, PositionExam = position, CreatedAt = DateTime.Now, CreatedUserId = UserId ?? "", CreatedFullName = FullName ?? "", LastUpdatedAt = DateTime.Now, LastUpdateUserId = UserId ?? "", LastUpdateFullName = FullName ?? "", UserId = UserId ?? "", }; await _context.Candidates.AddAsync(candidate); await _context.SaveChangesAsync(); return candidate.Id.ToString(); } else { _candidate.LastUpdatedAt = DateTime.Now; _candidate.LastUpdateUserId = UserId ?? ""; _candidate.LastUpdateFullName = FullName ?? ""; return _candidate.Id.ToString(); } } public async Task UpdateAsync(string examId, string positionId, CandidateResponseItem updated) { var candidateId = await CreateAsyncCandidate(examId, positionId); var candidate = await _context.Candidates.AsQueryable() .FirstOrDefaultAsync(x => x.Id == Guid.Parse(candidateId)); if (candidate == null) throw new Exception(GlobalMessages.ExamNotFound); if (updated.PrefixId != null) { var prefix = await _contextMetadata.Prefixes.AsQueryable() .FirstOrDefaultAsync(x => x.Id == Guid.Parse(updated.PrefixId)); if (prefix == null) throw new Exception(GlobalMessages.PrefixNotFound); candidate.PrefixId = prefix.Id; candidate.PrefixName = prefix.Name; } if (updated.RelationshipId != null) { var relationship = await _contextMetadata.Relationships.AsQueryable() .FirstOrDefaultAsync(x => x.Id == Guid.Parse(updated.RelationshipId)); if (relationship == null) throw new Exception(GlobalMessages.RelationshipNotFound); candidate.RelationshipId = relationship.Id; candidate.RelationshipName = relationship.Name; } if (updated.CitizenProvinceId != null) { var citizenProvince = await _contextMetadata.Provinces.AsQueryable() .FirstOrDefaultAsync(x => x.Id == Guid.Parse(updated.CitizenProvinceId)); if (citizenProvince == null) throw new Exception(GlobalMessages.ProvinceNotFound); candidate.CitizenProvinceId = citizenProvince.Id; candidate.CitizenProvinceName = citizenProvince.Name; } if (updated.CitizenDistrictId != null) { var citizenDistrict = await _contextMetadata.Districts.AsQueryable() .FirstOrDefaultAsync(x => x.Id == Guid.Parse(updated.CitizenDistrictId)); if (citizenDistrict == null) throw new Exception(GlobalMessages.DistrictNotFound); candidate.CitizenDistrictId = citizenDistrict.Id; candidate.CitizenDistrictName = citizenDistrict.Name; } if (updated.RegistProvinceId != null) { var registProvince = await _contextMetadata.Provinces.AsQueryable() .FirstOrDefaultAsync(x => x.Id == Guid.Parse(updated.RegistProvinceId)); if (registProvince == null) throw new Exception(GlobalMessages.ProvinceNotFound); candidate.RegistProvinceId = registProvince.Id; candidate.RegistProvinceName = registProvince.Name; } if (updated.RegistDistrictId != null) { var registDistrict = await _contextMetadata.Districts.AsQueryable() .FirstOrDefaultAsync(x => x.Id == Guid.Parse(updated.RegistDistrictId)); if (registDistrict == null) throw new Exception(GlobalMessages.DistrictNotFound); candidate.RegistDistrictId = registDistrict.Id; candidate.RegistDistrictName = registDistrict.Name; } if (updated.RegistSubDistrictId != null) { var registSubDistrict = await _contextMetadata.SubDistricts.AsQueryable() .FirstOrDefaultAsync(x => x.Id == Guid.Parse(updated.RegistSubDistrictId)); if (registSubDistrict == null) throw new Exception(GlobalMessages.SubDistrictNotFound); candidate.RegistSubDistrictId = registSubDistrict.Id; candidate.RegistSubDistrictName = registSubDistrict.Name; candidate.RegistZipCode = registSubDistrict.ZipCode; } if (updated.CurrentProvinceId != null) { var currentProvince = await _contextMetadata.Provinces.AsQueryable() .FirstOrDefaultAsync(x => x.Id == Guid.Parse(updated.CurrentProvinceId)); if (currentProvince == null) throw new Exception(GlobalMessages.ProvinceNotFound); candidate.CurrentProvinceId = currentProvince.Id; candidate.CurrentProvinceName = currentProvince.Name; } if (updated.CurrentDistrictId != null) { var currentDistrict = await _contextMetadata.Districts.AsQueryable() .FirstOrDefaultAsync(x => x.Id == Guid.Parse(updated.CurrentDistrictId)); if (currentDistrict == null) throw new Exception(GlobalMessages.DistrictNotFound); candidate.CurrentDistrictId = currentDistrict.Id; candidate.CurrentDistrictName = currentDistrict.Name; } if (updated.CurrentSubDistrictId != null) { var currentSubDistrict = await _contextMetadata.SubDistricts.AsQueryable() .FirstOrDefaultAsync(x => x.Id == Guid.Parse(updated.CurrentSubDistrictId)); if (currentSubDistrict == null) throw new Exception(GlobalMessages.SubDistrictNotFound); candidate.CurrentSubDistrictId = currentSubDistrict.Id; candidate.CurrentSubDistrictName = currentSubDistrict.Name; candidate.CurrentZipCode = currentSubDistrict.ZipCode; } if (updated.MarryPrefixId != null) { var prefix = await _contextMetadata.Prefixes.AsQueryable() .FirstOrDefaultAsync(x => x.Id == Guid.Parse(updated.MarryPrefixId)); if (prefix == null) throw new Exception(GlobalMessages.PrefixNotFound); candidate.MarryPrefixId = prefix.Id; candidate.MarryPrefixName = prefix.Name; } if (updated.FatherPrefixId != null) { var prefix = await _contextMetadata.Prefixes.AsQueryable() .FirstOrDefaultAsync(x => x.Id == Guid.Parse(updated.FatherPrefixId)); if (prefix == null) throw new Exception(GlobalMessages.PrefixNotFound); candidate.FatherPrefixId = prefix.Id; candidate.FatherPrefixName = prefix.Name; } if (updated.MotherPrefixId != null) { var prefix = await _contextMetadata.Prefixes.AsQueryable() .FirstOrDefaultAsync(x => x.Id == Guid.Parse(updated.MotherPrefixId)); if (prefix == null) throw new Exception(GlobalMessages.PrefixNotFound); candidate.MotherPrefixId = prefix.Id; candidate.MotherPrefixName = prefix.Name; } candidate.FirstName = updated.FirstName; candidate.LastName = updated.LastName; candidate.Nationality = updated.Nationality; candidate.DateOfBirth = updated.DateOfBirth; candidate.Email = updated.Email; candidate.CitizenId = updated.CitizenId; candidate.CitizenDate = updated.CitizenDate; candidate.Telephone = updated.Telephone; candidate.MobilePhone = updated.MobilePhone; candidate.Knowledge = updated.Knowledge; candidate.RegistAddress = updated.RegistAddress; candidate.RegistSame = updated.RegistSame == null ? null : updated.RegistSame; candidate.CurrentAddress = updated.CurrentAddress; candidate.Marry = updated.Marry == null ? null : updated.Marry; candidate.MarryFirstName = updated.MarryFirstName; candidate.MarryLastName = updated.MarryLastName; candidate.MarryOccupation = updated.MarryOccupation; candidate.MarryNationality = updated.MarryNationality; candidate.FatherFirstName = updated.FatherFirstName; candidate.FatherLastName = updated.FatherLastName; candidate.FatherOccupation = updated.FatherOccupation; candidate.FatherNationality = updated.FatherNationality; candidate.MotherFirstName = updated.MotherFirstName; candidate.MotherLastName = updated.MotherLastName; candidate.MotherOccupation = updated.MotherOccupation; candidate.MotherNationality = updated.MotherNationality; candidate.OccupationType = updated.OccupationType; candidate.OccupationCompany = updated.OccupationCompany; candidate.OccupationDepartment = updated.OccupationDepartment; candidate.OccupationEmail = updated.OccupationEmail; candidate.OccupationTelephone = updated.OccupationTelephone; candidate.OccupationPosition = updated.OccupationPosition; await _context.SaveChangesAsync(); } public async Task UpdateAdminAsync(string candidateId, CandidateResponseItem updated) { var candidate = await _context.Candidates.AsQueryable() .FirstOrDefaultAsync(x => x.Id == Guid.Parse(candidateId)); if (candidate == null) throw new Exception(GlobalMessages.ExamNotFound); if (updated.PrefixId != null) { var prefix = await _contextMetadata.Prefixes.AsQueryable() .FirstOrDefaultAsync(x => x.Id == Guid.Parse(updated.PrefixId)); if (prefix == null) throw new Exception(GlobalMessages.PrefixNotFound); candidate.PrefixId = prefix.Id; candidate.PrefixName = prefix.Name; } if (updated.RelationshipId != null) { var relationship = await _contextMetadata.Relationships.AsQueryable() .FirstOrDefaultAsync(x => x.Id == Guid.Parse(updated.RelationshipId)); if (relationship == null) throw new Exception(GlobalMessages.RelationshipNotFound); candidate.RelationshipId = relationship.Id; candidate.RelationshipName = relationship.Name; } if (updated.CitizenProvinceId != null) { var citizenProvince = await _contextMetadata.Provinces.AsQueryable() .FirstOrDefaultAsync(x => x.Id == Guid.Parse(updated.CitizenProvinceId)); if (citizenProvince == null) throw new Exception(GlobalMessages.ProvinceNotFound); candidate.CitizenProvinceId = citizenProvince.Id; candidate.CitizenProvinceName = citizenProvince.Name; } if (updated.CitizenDistrictId != null) { var citizenDistrict = await _contextMetadata.Districts.AsQueryable() .FirstOrDefaultAsync(x => x.Id == Guid.Parse(updated.CitizenDistrictId)); if (citizenDistrict == null) throw new Exception(GlobalMessages.DistrictNotFound); candidate.CitizenDistrictId = citizenDistrict.Id; candidate.CitizenDistrictName = citizenDistrict.Name; } if (updated.RegistProvinceId != null) { var registProvince = await _contextMetadata.Provinces.AsQueryable() .FirstOrDefaultAsync(x => x.Id == Guid.Parse(updated.RegistProvinceId)); if (registProvince == null) throw new Exception(GlobalMessages.ProvinceNotFound); candidate.RegistProvinceId = registProvince.Id; candidate.RegistProvinceName = registProvince.Name; } if (updated.RegistDistrictId != null) { var registDistrict = await _contextMetadata.Districts.AsQueryable() .FirstOrDefaultAsync(x => x.Id == Guid.Parse(updated.RegistDistrictId)); if (registDistrict == null) throw new Exception(GlobalMessages.DistrictNotFound); candidate.RegistDistrictId = registDistrict.Id; candidate.RegistDistrictName = registDistrict.Name; } if (updated.RegistSubDistrictId != null) { var registSubDistrict = await _contextMetadata.SubDistricts.AsQueryable() .FirstOrDefaultAsync(x => x.Id == Guid.Parse(updated.RegistSubDistrictId)); if (registSubDistrict == null) throw new Exception(GlobalMessages.SubDistrictNotFound); candidate.RegistSubDistrictId = registSubDistrict.Id; candidate.RegistSubDistrictName = registSubDistrict.Name; candidate.RegistZipCode = registSubDistrict.ZipCode; } if (updated.CurrentProvinceId != null) { var currentProvince = await _contextMetadata.Provinces.AsQueryable() .FirstOrDefaultAsync(x => x.Id == Guid.Parse(updated.CurrentProvinceId)); if (currentProvince == null) throw new Exception(GlobalMessages.ProvinceNotFound); candidate.CurrentProvinceId = currentProvince.Id; candidate.CurrentProvinceName = currentProvince.Name; } if (updated.CurrentDistrictId != null) { var currentDistrict = await _contextMetadata.Districts.AsQueryable() .FirstOrDefaultAsync(x => x.Id == Guid.Parse(updated.CurrentDistrictId)); if (currentDistrict == null) throw new Exception(GlobalMessages.DistrictNotFound); candidate.CurrentDistrictId = currentDistrict.Id; candidate.CurrentDistrictName = currentDistrict.Name; } if (updated.CurrentSubDistrictId != null) { var currentSubDistrict = await _contextMetadata.SubDistricts.AsQueryable() .FirstOrDefaultAsync(x => x.Id == Guid.Parse(updated.CurrentSubDistrictId)); if (currentSubDistrict == null) throw new Exception(GlobalMessages.SubDistrictNotFound); candidate.CurrentSubDistrictId = currentSubDistrict.Id; candidate.CurrentSubDistrictName = currentSubDistrict.Name; candidate.CurrentZipCode = currentSubDistrict.ZipCode; } if (updated.MarryPrefixId != null) { var prefix = await _contextMetadata.Prefixes.AsQueryable() .FirstOrDefaultAsync(x => x.Id == Guid.Parse(updated.MarryPrefixId)); if (prefix == null) throw new Exception(GlobalMessages.PrefixNotFound); candidate.MarryPrefixId = prefix.Id; candidate.MarryPrefixName = prefix.Name; } if (updated.FatherPrefixId != null) { var prefix = await _contextMetadata.Prefixes.AsQueryable() .FirstOrDefaultAsync(x => x.Id == Guid.Parse(updated.FatherPrefixId)); if (prefix == null) throw new Exception(GlobalMessages.PrefixNotFound); candidate.FatherPrefixId = prefix.Id; candidate.FatherPrefixName = prefix.Name; } if (updated.MotherPrefixId != null) { var prefix = await _contextMetadata.Prefixes.AsQueryable() .FirstOrDefaultAsync(x => x.Id == Guid.Parse(updated.MotherPrefixId)); if (prefix == null) throw new Exception(GlobalMessages.PrefixNotFound); candidate.MotherPrefixId = prefix.Id; candidate.MotherPrefixName = prefix.Name; } candidate.FirstName = updated.FirstName; candidate.LastName = updated.LastName; candidate.Nationality = updated.Nationality; candidate.DateOfBirth = updated.DateOfBirth; candidate.Email = updated.Email; candidate.CitizenId = updated.CitizenId; candidate.CitizenDate = updated.CitizenDate; candidate.Telephone = updated.Telephone; candidate.MobilePhone = updated.MobilePhone; candidate.Knowledge = updated.Knowledge; candidate.RegistAddress = updated.RegistAddress; candidate.RegistSame = updated.RegistSame == null ? null : updated.RegistSame; candidate.CurrentAddress = updated.CurrentAddress; candidate.Marry = updated.Marry == null ? null : updated.Marry; candidate.MarryFirstName = updated.MarryFirstName; candidate.MarryLastName = updated.MarryLastName; candidate.MarryOccupation = updated.MarryOccupation; candidate.MarryNationality = updated.MarryNationality; candidate.FatherFirstName = updated.FatherFirstName; candidate.FatherLastName = updated.FatherLastName; candidate.FatherOccupation = updated.FatherOccupation; candidate.FatherNationality = updated.FatherNationality; candidate.MotherFirstName = updated.MotherFirstName; candidate.MotherLastName = updated.MotherLastName; candidate.MotherOccupation = updated.MotherOccupation; candidate.MotherNationality = updated.MotherNationality; candidate.OccupationType = updated.OccupationType; candidate.OccupationCompany = updated.OccupationCompany; candidate.OccupationDepartment = updated.OccupationDepartment; candidate.OccupationEmail = updated.OccupationEmail; candidate.OccupationTelephone = updated.OccupationTelephone; candidate.OccupationPosition = updated.OccupationPosition; await _context.SaveChangesAsync(); } public async Task UpdateAsyncInformation(string examId, string positionId, CandidateInformationResponseItem updated) { var candidateId = await CreateAsyncCandidate(examId, positionId); var candidate = await _context.Candidates.AsQueryable() .FirstOrDefaultAsync(x => x.Id == Guid.Parse(candidateId)); if (candidate == null) throw new Exception(GlobalMessages.ExamNotFound); if (updated.PrefixId != null) { var prefix = await _contextMetadata.Prefixes.AsQueryable() .FirstOrDefaultAsync(x => x.Id == Guid.Parse(updated.PrefixId)); if (prefix == null) throw new Exception(GlobalMessages.PrefixNotFound); candidate.PrefixId = prefix.Id; candidate.PrefixName = prefix.Name; } if (updated.RelationshipId != null) { var relationship = await _contextMetadata.Relationships.AsQueryable() .FirstOrDefaultAsync(x => x.Id == Guid.Parse(updated.RelationshipId)); if (relationship == null) throw new Exception(GlobalMessages.RelationshipNotFound); candidate.RelationshipId = relationship.Id; candidate.RelationshipName = relationship.Name; } if (updated.CitizenProvinceId != null) { var citizenProvince = await _contextMetadata.Provinces.AsQueryable() .FirstOrDefaultAsync(x => x.Id == Guid.Parse(updated.CitizenProvinceId)); if (citizenProvince == null) throw new Exception(GlobalMessages.ProvinceNotFound); candidate.CitizenProvinceId = citizenProvince.Id; candidate.CitizenProvinceName = citizenProvince.Name; } if (updated.CitizenDistrictId != null) { var citizenDistrict = await _contextMetadata.Districts.AsQueryable() .FirstOrDefaultAsync(x => x.Id == Guid.Parse(updated.CitizenDistrictId)); if (citizenDistrict == null) throw new Exception(GlobalMessages.DistrictNotFound); candidate.CitizenDistrictId = citizenDistrict.Id; candidate.CitizenDistrictName = citizenDistrict.Name; } candidate.FirstName = updated.FirstName; candidate.LastName = updated.LastName; candidate.Nationality = updated.Nationality; candidate.DateOfBirth = updated.DateOfBirth; candidate.Email = updated.Email; candidate.CitizenId = updated.CitizenId; candidate.CitizenDate = updated.CitizenDate; candidate.Telephone = updated.Telephone; candidate.MobilePhone = updated.MobilePhone; candidate.Knowledge = updated.Knowledge; await _context.SaveChangesAsync(); } public async Task UpdateAsyncProfileImage(string examId, string positionId, IFormFile file) { var candidateId = await CreateAsyncCandidate(examId, positionId); var candidate = await _context.Candidates.AsQueryable() .Include(x => x.ProfileImg) .FirstOrDefaultAsync(x => x.Id == Guid.Parse(candidateId)); if (candidate == null) throw new Exception(GlobalMessages.ExamNotFound); if (candidate.ProfileImg != null) { await DeleteDocument(candidate.ProfileImg.Id.ToString()); } 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 UpdateAsyncAdminProfileImage(string candidateId, IFormFile file) { var candidate = await _context.Candidates.AsQueryable() .Include(x => x.ProfileImg) .FirstOrDefaultAsync(x => x.Id == Guid.Parse(candidateId)); if (candidate == null) throw new Exception(GlobalMessages.ExamNotFound); if (candidate.ProfileImg != null) { await DeleteDocument(candidate.ProfileImg.Id.ToString()); } 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, string positionId, CandidateAddressResponseItem updated) { var candidateId = await CreateAsyncCandidate(examId, positionId); var candidate = await _context.Candidates.AsQueryable() .FirstOrDefaultAsync(x => x.Id == Guid.Parse(candidateId)); if (candidate == null) throw new Exception(GlobalMessages.ExamNotFound); if (updated.RegistProvinceId != null) { var registProvince = await _contextMetadata.Provinces.AsQueryable() .FirstOrDefaultAsync(x => x.Id == Guid.Parse(updated.RegistProvinceId)); if (registProvince == null) throw new Exception(GlobalMessages.ProvinceNotFound); candidate.RegistProvinceId = registProvince.Id; candidate.RegistProvinceName = registProvince.Name; } if (updated.RegistDistrictId != null) { var registDistrict = await _contextMetadata.Districts.AsQueryable() .FirstOrDefaultAsync(x => x.Id == Guid.Parse(updated.RegistDistrictId)); if (registDistrict == null) throw new Exception(GlobalMessages.DistrictNotFound); candidate.RegistDistrictId = registDistrict.Id; candidate.RegistDistrictName = registDistrict.Name; } if (updated.RegistSubDistrictId != null) { var registSubDistrict = await _contextMetadata.SubDistricts.AsQueryable() .FirstOrDefaultAsync(x => x.Id == Guid.Parse(updated.RegistSubDistrictId)); if (registSubDistrict == null) throw new Exception(GlobalMessages.SubDistrictNotFound); candidate.RegistSubDistrictId = registSubDistrict.Id; candidate.RegistSubDistrictName = registSubDistrict.Name; candidate.RegistZipCode = registSubDistrict.ZipCode; } if (updated.CurrentProvinceId != null) { var currentProvince = await _contextMetadata.Provinces.AsQueryable() .FirstOrDefaultAsync(x => x.Id == Guid.Parse(updated.CurrentProvinceId)); if (currentProvince == null) throw new Exception(GlobalMessages.ProvinceNotFound); candidate.CurrentProvinceId = currentProvince.Id; candidate.CurrentProvinceName = currentProvince.Name; } if (updated.CurrentDistrictId != null) { var currentDistrict = await _contextMetadata.Districts.AsQueryable() .FirstOrDefaultAsync(x => x.Id == Guid.Parse(updated.CurrentDistrictId)); if (currentDistrict == null) throw new Exception(GlobalMessages.DistrictNotFound); candidate.CurrentDistrictId = currentDistrict.Id; candidate.CurrentDistrictName = currentDistrict.Name; } if (updated.CurrentSubDistrictId != null) { var currentSubDistrict = await _contextMetadata.SubDistricts.AsQueryable() .FirstOrDefaultAsync(x => x.Id == Guid.Parse(updated.CurrentSubDistrictId)); if (currentSubDistrict == null) throw new Exception(GlobalMessages.SubDistrictNotFound); candidate.CurrentSubDistrictId = currentSubDistrict.Id; candidate.CurrentSubDistrictName = currentSubDistrict.Name; candidate.CurrentZipCode = currentSubDistrict.ZipCode; } candidate.RegistAddress = updated.RegistAddress; candidate.RegistSame = updated.RegistSame == null ? null : updated.RegistSame; candidate.CurrentAddress = updated.CurrentAddress; await _context.SaveChangesAsync(); } public async Task UpdateAsyncFamily(string examId, string positionId, CandidateFamilyResponseItem updated) { var candidateId = await CreateAsyncCandidate(examId, positionId); var candidate = await _context.Candidates.AsQueryable() .FirstOrDefaultAsync(x => x.Id == Guid.Parse(candidateId)); if (candidate == null) throw new Exception(GlobalMessages.ExamNotFound); if (updated.MarryPrefixId != null) { var prefix = await _contextMetadata.Prefixes.AsQueryable() .FirstOrDefaultAsync(x => x.Id == Guid.Parse(updated.MarryPrefixId)); if (prefix == null) throw new Exception(GlobalMessages.PrefixNotFound); candidate.MarryPrefixId = prefix.Id; candidate.MarryPrefixName = prefix.Name; } if (updated.FatherPrefixId != null) { var prefix = await _contextMetadata.Prefixes.AsQueryable() .FirstOrDefaultAsync(x => x.Id == Guid.Parse(updated.FatherPrefixId)); if (prefix == null) throw new Exception(GlobalMessages.PrefixNotFound); candidate.FatherPrefixId = prefix.Id; candidate.FatherPrefixName = prefix.Name; } if (updated.MotherPrefixId != null) { var prefix = await _contextMetadata.Prefixes.AsQueryable() .FirstOrDefaultAsync(x => x.Id == Guid.Parse(updated.MotherPrefixId)); if (prefix == null) throw new Exception(GlobalMessages.PrefixNotFound); candidate.MotherPrefixId = prefix.Id; candidate.MotherPrefixName = prefix.Name; } candidate.Marry = updated.Marry == null ? null : updated.Marry; candidate.MarryFirstName = updated.MarryFirstName; candidate.MarryLastName = updated.MarryLastName; candidate.MarryOccupation = updated.MarryOccupation; candidate.MarryNationality = updated.MarryNationality; candidate.FatherFirstName = updated.FatherFirstName; candidate.FatherLastName = updated.FatherLastName; candidate.FatherOccupation = updated.FatherOccupation; candidate.FatherNationality = updated.FatherNationality; candidate.MotherFirstName = updated.MotherFirstName; candidate.MotherLastName = updated.MotherLastName; candidate.MotherOccupation = updated.MotherOccupation; candidate.MotherNationality = updated.MotherNationality; await _context.SaveChangesAsync(); } public async Task UpdateAsyncOccupation(string examId, string positionId, CandidateOccupationResponseItem updated) { var candidateId = await CreateAsyncCandidate(examId, positionId); var candidate = await _context.Candidates.AsQueryable() .FirstOrDefaultAsync(x => x.Id == Guid.Parse(candidateId)); if (candidate == null) throw new Exception(GlobalMessages.ExamNotFound); candidate.OccupationType = updated.OccupationType; candidate.OccupationCompany = updated.OccupationCompany; candidate.OccupationDepartment = updated.OccupationDepartment; candidate.OccupationEmail = updated.OccupationEmail; candidate.OccupationTelephone = updated.OccupationTelephone; candidate.OccupationPosition = updated.OccupationPosition; await _context.SaveChangesAsync(); } public async Task UpdateAsyncDocument(string examId, string positionId, IFormFile file) { var candidateId = await CreateAsyncCandidate(examId, positionId); 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); if (document == null) throw new Exception(GlobalMessages.NoFileToUpload); var candidateDocument = new CandidateDocument { Candidate = candidate, Document = document, }; await _context.CandidateDocuments.AddAsync(candidateDocument); await _context.SaveChangesAsync(); } public async Task UpdateAsyncAdminDocument(string candidateId, IFormFile file) { 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); if (document == null) throw new Exception(GlobalMessages.NoFileToUpload); var candidateDocument = new CandidateDocument { Candidate = candidate, Document = document, }; await _context.CandidateDocuments.AddAsync(candidateDocument); await _context.SaveChangesAsync(); } public async Task DeleteDocument(string documentId) { await _minioService.DeleteFileAsync(Guid.Parse(documentId)); } public async Task DeleteAsyncDocument(string examId, string positionId, string documentId) { var exam = await _context.PeriodExams.AsQueryable() .Where(p => p.CheckDisability == false) .Include(x => x.PositionExam) .FirstOrDefaultAsync(x => x.Id == Guid.Parse(examId)); if (exam == null) throw new Exception(GlobalMessages.ExamNotFound); var position = exam.PositionExam.Where(x => x.Id == Guid.Parse(positionId)).FirstOrDefault(); if (position == null) throw new Exception(GlobalMessages.PositionExamNotFound); await _minioService.DeleteFileAsync(Guid.Parse(documentId)); } public async Task DeleteAsyncAdminDocument(string candidateId, string documentId) { var candidate = await _context.Candidates.AsQueryable() .FirstOrDefaultAsync(x => x.Id == Guid.Parse(candidateId)); if (candidate == null) throw new Exception(GlobalMessages.ExamNotFound); await _minioService.DeleteFileAsync(Guid.Parse(documentId)); } public async Task CreateAsyncCareer(string examId, string positionId, CandidateCareerResponseItem updated) { var candidateId = await CreateAsyncCandidate(examId, positionId); var candidate = await _context.Candidates.AsQueryable() .FirstOrDefaultAsync(x => x.Id == Guid.Parse(candidateId)); var career = new Career { Candidate = candidate, Name = updated.Name, Position = updated.Position, Salary = updated.Salary, DurationStart = updated.DurationStart, DurationEnd = updated.DurationEnd, Reason = updated.Reason, CreatedAt = DateTime.Now, CreatedUserId = UserId ?? "", LastUpdatedAt = DateTime.Now, LastUpdateUserId = UserId ?? "", CreatedFullName = FullName ?? "", LastUpdateFullName = FullName ?? "", }; await _context.Careers.AddAsync(career); await _context.SaveChangesAsync(); } public async Task CreateAsyncAdminCareer(string candidateId, CandidateCareerResponseItem updated) { var candidate = await _context.Candidates.AsQueryable() .FirstOrDefaultAsync(x => x.Id == Guid.Parse(candidateId)); var career = new Career { Candidate = candidate, Name = updated.Name, Position = updated.Position, Salary = updated.Salary, DurationStart = updated.DurationStart, DurationEnd = updated.DurationEnd, Reason = updated.Reason, CreatedAt = DateTime.Now, CreatedUserId = UserId ?? "", LastUpdatedAt = DateTime.Now, LastUpdateUserId = UserId ?? "", CreatedFullName = FullName ?? "", LastUpdateFullName = FullName ?? "", }; await _context.Careers.AddAsync(career); await _context.SaveChangesAsync(); } public async Task CreateAsyncEducation(string examId, string positionId, CandidateEducationResponseItem updated) { var candidateId = await CreateAsyncCandidate(examId, positionId); var candidate = await _context.Candidates.AsQueryable() .FirstOrDefaultAsync(x => x.Id == Guid.Parse(candidateId)); var educationLevel = await _contextMetadata.EducationLevels.AsQueryable() .FirstOrDefaultAsync(x => x.Id == Guid.Parse(updated.EducationLevelId)); if (educationLevel == null) throw new Exception(GlobalMessages.EducationLevelNotFound); var education = new Education { Candidate = candidate, EducationLevelId = educationLevel.Id, EducationLevelName = educationLevel.Name, Major = updated.Major, Scores = updated.Scores, Name = updated.Name, DurationStart = updated.DurationStart, DurationEnd = updated.DurationEnd, CreatedAt = DateTime.Now, CreatedUserId = UserId ?? "", LastUpdatedAt = DateTime.Now, LastUpdateUserId = UserId ?? "", CreatedFullName = FullName ?? "", LastUpdateFullName = FullName ?? "", }; await _context.Educations.AddAsync(education); await _context.SaveChangesAsync(); } public async Task CreateAsyncAdminEducation(string candidateId, CandidateEducationResponseItem updated) { var candidate = await _context.Candidates.AsQueryable() .FirstOrDefaultAsync(x => x.Id == Guid.Parse(candidateId)); var educationLevel = await _contextMetadata.EducationLevels.AsQueryable() .FirstOrDefaultAsync(x => x.Id == Guid.Parse(updated.EducationLevelId)); if (educationLevel == null) throw new Exception(GlobalMessages.EducationLevelNotFound); var education = new Education { Candidate = candidate, EducationLevelId = educationLevel.Id, EducationLevelName = educationLevel.Name, Major = updated.Major, Scores = updated.Scores, Name = updated.Name, DurationStart = updated.DurationStart, DurationEnd = updated.DurationEnd, CreatedAt = DateTime.Now, CreatedUserId = UserId ?? "", LastUpdatedAt = DateTime.Now, LastUpdateUserId = UserId ?? "", CreatedFullName = FullName ?? "", LastUpdateFullName = FullName ?? "", }; await _context.Educations.AddAsync(education); await _context.SaveChangesAsync(); } public async Task UpdateAsyncCareer(string careerId, CandidateCareerResponseItem updated) { var career = await _context.Careers.AsQueryable() .FirstOrDefaultAsync(x => x.Id == Guid.Parse(careerId)); if (career == null) throw new Exception(GlobalMessages.CareerNotFound); career.Name = updated.Name; career.Position = updated.Position; career.Salary = updated.Salary; career.DurationStart = updated.DurationStart; career.DurationEnd = updated.DurationEnd; career.Reason = updated.Reason; career.LastUpdatedAt = DateTime.Now; career.LastUpdateUserId = UserId ?? ""; career.LastUpdateFullName = FullName ?? ""; await _context.SaveChangesAsync(); } public async Task DeleteAsyncCareer(string careerId) { var career = await _context.Careers.AsQueryable() .FirstOrDefaultAsync(x => x.Id == Guid.Parse(careerId)); if (career == null) throw new Exception(GlobalMessages.CareerNotFound); _context.Careers.Remove(career); await _context.SaveChangesAsync(); } public async Task UpdateAsyncEducation(string educationId, CandidateEducationResponseItem updated) { var education = await _context.Educations.AsQueryable() .FirstOrDefaultAsync(x => x.Id == Guid.Parse(educationId)); if (education == null) throw new Exception(GlobalMessages.EducationNotFound); var educationLevel = await _contextMetadata.EducationLevels.AsQueryable() .FirstOrDefaultAsync(x => x.Id == Guid.Parse(updated.EducationLevelId)); if (educationLevel == null) throw new Exception(GlobalMessages.EducationLevelNotFound); education.EducationLevelId = educationLevel.Id; education.EducationLevelName = educationLevel.Name; education.Major = updated.Major; education.Scores = updated.Scores; education.Name = updated.Name; education.DurationStart = updated.DurationStart; education.DurationEnd = updated.DurationEnd; education.LastUpdatedAt = DateTime.Now; education.LastUpdateUserId = UserId ?? ""; education.LastUpdateFullName = FullName ?? ""; await _context.SaveChangesAsync(); } public async Task DeleteAsyncEducation(string educationId) { var education = await _context.Educations.AsQueryable() .FirstOrDefaultAsync(x => x.Id == Guid.Parse(educationId)); if (education == null) throw new Exception(GlobalMessages.EducationNotFound); _context.Educations.Remove(education); await _context.SaveChangesAsync(); } public async Task GetStatusCandidateService(string examId, string positionId) { var exam = await _context.PeriodExams.AsQueryable() .Where(p => p.CheckDisability == false) .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); if (positionId != "00000000-0000-0000-0000-000000000000") { var position = await _context.PositionExams.AsQueryable() .FirstOrDefaultAsync(x => x.Id == Guid.Parse(positionId) && x.PeriodExam == exam); if (position == null) throw new Exception(GlobalMessages.PositionExamNotFound); candidate = await _context.Candidates.AsQueryable() .FirstOrDefaultAsync(x => x.PeriodExam == exam && x.UserId == UserId && x.PositionExam == position); } if (candidate == null) throw new Exception(GlobalMessages.CandidateNotFound); return new CandidateStatusResponse { Status = candidate.Status, RejectDetail = candidate.RejectDetail }; } public async Task UserCheckCandidateService(string examId, string positionId, string status) { var exam = await _context.PeriodExams.AsQueryable() .Where(p => p.CheckDisability == false) .Include(x => x.PositionExam) .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); if (positionId != "00000000-0000-0000-0000-000000000000") { var position = await _context.PositionExams.AsQueryable() .FirstOrDefaultAsync(x => x.Id == Guid.Parse(positionId) && x.PeriodExam == exam); if (position == null) throw new Exception(GlobalMessages.PositionExamNotFound); var candidateDelete = await _context.Candidates.AsQueryable() .Where(x => x.PeriodExam == exam && x.UserId == UserId && x.PositionExam != position) .ToListAsync(); _context.Candidates.RemoveRange(candidateDelete); candidate = await _context.Candidates.AsQueryable() .FirstOrDefaultAsync(x => x.PeriodExam == exam && x.UserId == UserId && x.PositionExam == position); } if (candidate == null) throw new Exception(GlobalMessages.CandidateNotFound); if (status == "checkRegister") { var subject = "แจ้งผลการสมัครสอบคัดเลือก " + exam.Name; var body = candidate.FirstName + " " + candidate.LastName + " สถานะการสมัครสอบ: รอเจ้าหน้าที่ตรวจสอบข้อมูล"; _mailService.SendMailToUser(subject, body, "ananda@frappet.com"); } if (status == "checkPayment") { var subject = "แจ้งผลการสมัครสอบคัดเลือก " + exam.Name; var body = candidate.FirstName + " " + candidate.LastName + " สถานะการสมัครสอบ: รอเจ้าหน้าที่ตรวจสอบหลักฐานชำระเงิน"; _mailService.SendMailToUser(subject, body, "ananda@frappet.com"); } candidate.Status = status; await _context.SaveChangesAsync(); } public async Task AdminCheckCandidateService(string candidateId, string status, RequestApprove item) { var candidate = await _context.Candidates.AsQueryable() .Include(x => x.PeriodExam) .FirstOrDefaultAsync(x => x.Id == Guid.Parse(candidateId)); if (candidate == null) throw new Exception(GlobalMessages.CandidateNotFound); if (candidate.PeriodExam == null) throw new Exception(GlobalMessages.ExamNotFound); var periodExam = await _context.Candidates.AsQueryable() .Where(x => x.PeriodExam == candidate.PeriodExam && x.ExamIdenNumber != null) .OrderByDescending(d => d.CreatedAt) .ToListAsync(); if (periodExam == null) throw new Exception(GlobalMessages.ExamNotFound); if (status != "rejectDelete") { candidate.Status = status; if (status == "rejectRegister") { var subject = "แจ้งผลการสมัครสอบคัดเลือก " + candidate.PeriodExam.Name; var body = candidate.FirstName + " " + candidate.LastName + " มีคุณสมบัติสมัครสอบไม่ผ่านกรุณาตรวจสอบข้อมูล เนื่องจาก: " + item.Reason; if (candidate.Email != null) _mailService.SendMailToUser(subject, body, candidate.Email); candidate.RejectDetail = item.Reason; } if (status == "rejectPayment") { var subject = "แจ้งผลการสมัครสอบคัดเลือก " + candidate.PeriodExam.Name; var body = candidate.FirstName + " " + candidate.LastName + " มีหลักฐานชำระเงินไม่ถูกต้องกรุณาตรวจสอบข้อมูล เนื่องจาก: " + item.Reason; if (candidate.Email != null) _mailService.SendMailToUser(subject, body, candidate.Email); candidate.RejectDetail = item.Reason; } else if (status == "payment" && candidate.PeriodExam.Fee == 0) { var subject = "แจ้งผลการสมัครสอบคัดเลือก " + candidate.PeriodExam.Name; var body = candidate.FirstName + " " + candidate.LastName + " สถานะการสมัครสอบ: รอเจ้าหน้าที่จัดที่นั่งสอบ"; if (candidate.Email != null) _mailService.SendMailToUser(subject, body, candidate.Email); candidate.Status = "checkSeat"; var num = periodExam.Count() + 1; candidate.ExamIdenNumber = "CDC-" + num; } else if (status == "payment" && candidate.PeriodExam.Fee != 0) { var subject = "แจ้งผลการสมัครสอบคัดเลือก " + candidate.PeriodExam.Name; var body = candidate.FirstName + " " + candidate.LastName + " สถานะการสมัครสอบ: รอชำระค่าสมัครสอบ"; if (candidate.Email != null) _mailService.SendMailToUser(subject, body, candidate.Email); } if (candidate.Status == "checkSeat") { var subject = "แจ้งผลการสมัครสอบคัดเลือก " + candidate.PeriodExam.Name; var body = candidate.FirstName + " " + candidate.LastName + " สถานะการสมัครสอบ: รอเจ้าหน้าที่จัดที่นั่งสอบ"; if (candidate.Email != null) _mailService.SendMailToUser(subject, body, candidate.Email); var num = periodExam.Count() + 1; candidate.ExamIdenNumber = "CDC-" + num; } } else { var subject = "แจ้งผลการสมัครสอบคัดเลือก " + candidate.PeriodExam.Name; var body = candidate.FirstName + " " + candidate.LastName + " มีคุณสมบัติไม่ผ่านตามเงื่อนไข"; if (candidate.Email != null) _mailService.SendMailToUser(subject, body, candidate.Email); _context.Candidates.Remove(candidate); } await _context.SaveChangesAsync(); } public async Task AdminPassCandidateService(string candidateId, string status) { var candidate = await _context.Candidates.AsQueryable() .Include(x => x.PeriodExam) .FirstOrDefaultAsync(x => x.Id == Guid.Parse(candidateId)); if (candidate == null) throw new Exception(GlobalMessages.CandidateNotFound); if (candidate.PeriodExam == null) throw new Exception(GlobalMessages.ExamNotFound); if (status == "done") { var subject = "แจ้งผลการสมัครสอบคัดเลือก " + candidate.PeriodExam.Name; var body = candidate.FirstName + " " + candidate.LastName + " สถานะการสมัครสอบ: สอบคัดเลือกสำเร็จ"; if (candidate.Email != null) _mailService.SendMailToUser(subject, body, candidate.Email); } if (status == "checkPoint") { var subject = "แจ้งผลการสมัครสอบคัดเลือก " + candidate.PeriodExam.Name; var body = candidate.FirstName + " " + candidate.LastName + " สถานะการสมัครสอบ: รอเจ้าหน้าที่สรุปคะแนนสอบ"; if (candidate.Email != null) _mailService.SendMailToUser(subject, body, candidate.Email); } candidate.Status = status; await _context.SaveChangesAsync(); } public async Task UpdateAsyncPaymentImage(string examId, string positionId, IFormFile file) { var candidateId = await CreateAsyncCandidate(examId, positionId); var candidate = await _context.Candidates.AsQueryable() .Include(x => x.PaymentImg) .FirstOrDefaultAsync(x => x.Id == Guid.Parse(candidateId)); if (candidate == null) throw new Exception(GlobalMessages.ExamNotFound); if (candidate.PaymentImg != null) { await DeleteDocument(candidate.PaymentImg.Id.ToString()); } var doc = await _minioService.UploadFileAsync(file); var document = await _context.Documents.AsQueryable() .FirstOrDefaultAsync(x => x.Id == doc.Id); candidate.PaymentImg = document; await _context.SaveChangesAsync(); } public async Task GetsAsyncCardCandidate(string examId, string positionId) { var exam = await _context.PeriodExams.AsQueryable() .Where(p => p.CheckDisability == false) .FirstOrDefaultAsync(x => x.Id == Guid.Parse(examId)); if (exam == null) throw new Exception(GlobalMessages.ExamNotFound); var candidate = await _context.Candidates.AsQueryable() .Include(x => x.ProfileImg) .Where(x => x.PeriodExam == exam && x.UserId == UserId) .FirstOrDefaultAsync(); if (positionId != "00000000-0000-0000-0000-000000000000") { var position = await _context.PositionExams.AsQueryable() .FirstOrDefaultAsync(x => x.Id == Guid.Parse(positionId) && x.PeriodExam == exam); if (position == null) throw new Exception(GlobalMessages.PositionExamNotFound); candidate = await _context.Candidates.AsQueryable() .Where(x => x.PeriodExam == exam && x.UserId == UserId && x.PositionExam == position) .FirstOrDefaultAsync(); } if (candidate == null) throw new Exception(GlobalMessages.CandidateNotFound); return new RequestCardCandidate { Avatar = candidate.ProfileImg == null ? "" : _minioService.ImagesPath(candidate.ProfileImg.Id).Result, FirstName = candidate.FirstName, LastName = candidate.LastName, Prefix = candidate.PrefixName, CitizenId = candidate.CitizenId, ExamIdenNumber = candidate.ExamIdenNumber, SeatNumber = candidate.SeatNumber, PointTotalB = candidate.PointTotalB, PointB = candidate.PointB, ResultB = candidate.ResultB, PointTotalC = candidate.PointTotalC, PointC = candidate.PointC, ResultC = candidate.ResultC, Pass = candidate.Pass, Number = candidate.Number, AnnouncementDate = exam.AnnouncementDate.AddYears(2), Id = candidate.Id, }; } public async Task GetsAsyncAdminCardCandidate(string candidateId) { var candidate = await _context.Candidates.AsQueryable() .Include(x => x.ProfileImg) .Include(x => x.PeriodExam) .FirstOrDefaultAsync(x => x.Id == Guid.Parse(candidateId)); if (candidate == null) throw new Exception(GlobalMessages.CandidateNotFound); return new RequestCardCandidate { Avatar = candidate.ProfileImg == null ? "" : _minioService.ImagesPath(candidate.ProfileImg.Id).Result, FirstName = candidate.FirstName, LastName = candidate.LastName, Prefix = candidate.PrefixName, CitizenId = candidate.CitizenId, ExamIdenNumber = candidate.ExamIdenNumber, SeatNumber = candidate.SeatNumber, PointTotalB = candidate.PointTotalB, PointB = candidate.PointB, ResultB = candidate.ResultB, PointTotalC = candidate.PointTotalC, PointC = candidate.PointC, ResultC = candidate.ResultC, Pass = candidate.Pass, Number = candidate.Number, AnnouncementDate = candidate.PeriodExam?.AnnouncementDate.AddYears(2), Id = candidate.Id, }; } public async Task GetsAsyncBillCandidate(string examId, string positionId) { // 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); // if (positionId != "00000000-0000-0000-0000-000000000000") // { // var position = await _context.PositionExams.AsQueryable() // .FirstOrDefaultAsync(x => x.Id == Guid.Parse(positionId) && x.PeriodExam == exam); // if (position == null) // throw new Exception(GlobalMessages.PositionExamNotFound); // candidate = await _context.Candidates.AsQueryable() // .FirstOrDefaultAsync(x => x.PeriodExam == exam && x.UserId == UserId && x.PositionExam == position); // } // if (candidate == null) // throw new Exception(GlobalMessages.CandidateNotFound); // var document = await _context.CandidateDocuments.AsQueryable() // .Where(x => x.Candidate == candidate) // .OrderBy(x => x.CreatedAt) // .Select(x => new FileListResponse // { // Id = x.Document == null ? "" : x.Document.Id.ToString(), // FileName = x.Document == null ? "" : x.Document.FileName, // FileType = x.Document == null ? "" : x.Document.FileType, // FileSize = x.Document == null ? 0 : x.Document.FileSize, // Detail = x.Document == null ? "" : x.Document.Id.ToString(), // }) // .ToListAsync(); return _minioService.ImagesPath(Guid.Parse("08db463c-9bf7-494f-8b35-e9b777256dae")).Result; } #endregion } }