hrms-api-exam/Services/PeriodExamService.cs
2023-03-30 16:47:12 +07:00

267 lines
12 KiB
C#

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.Response;
using Microsoft.EntityFrameworkCore;
namespace BMA.EHR.Recurit.Exam.Service.Services
{
public class PeriodExamService
{
#region " Fields "
private readonly ApplicationDbContext _context;
private readonly IHttpContextAccessor _httpContextAccessor;
#endregion
#region " Constructor and Destructor "
public PeriodExamService(ApplicationDbContext context,
IHttpContextAccessor httpContextAccessor)
{
_context = context;
_httpContextAccessor = httpContextAccessor;
}
#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<IEnumerable<PeriodExam>> GetsAsync(bool showAll = true)
{
if (showAll)
return await _context.PeriodExams.AsQueryable()
.OrderBy(d => d.Name)
.ToListAsync();
else
return await _context.PeriodExams.AsQueryable()
.Where(p => p.IsActive)
.OrderBy(d => d.Name)
.ToListAsync();
}
public async Task<PeriodExam?> GetsExamAndCandidateAsync(string examId, bool showAll = true)
{
var periodExam = await _context.PeriodExams.AsQueryable()
.FirstOrDefaultAsync(x => x.Id == Guid.Parse(examId));
if (periodExam == null)
throw new Exception(GlobalMessages.ExamNotFound);
return periodExam;
}
public async Task CreateAsync(PeriodExam inserted)
{
var periodExam = new PeriodExam
{
Name = inserted.Name,
StartDate = inserted.StartDate,
EndDate = inserted.EndDate,
Round = inserted.Round,
Fee = inserted.Fee,
Year = inserted.Year,
Detail = inserted.Detail,
AnnounceDate = inserted.AnnounceDate,
CreatedAt = DateTime.Now,
CreatedUserId = UserId ?? "",
LastUpdatedAt = DateTime.Now,
LastUpdateUserId = UserId ?? "",
CreatedFullName = FullName ?? "",
LastUpdateFullName = FullName ?? "",
};
await _context.PeriodExams.AddAsync(periodExam);
await _context.SaveChangesAsync();
}
public async Task UpdateAsync(string examId, PeriodExam updated)
{
var periodExam = await _context.PeriodExams.AsQueryable()
.FirstOrDefaultAsync(x => x.Id == Guid.Parse(examId));
if (periodExam == null)
throw new Exception(GlobalMessages.ExamNotFound);
periodExam.Name = updated.Name;
periodExam.StartDate = updated.StartDate;
periodExam.EndDate = updated.EndDate;
periodExam.Round = updated.Round;
periodExam.Fee = updated.Fee;
periodExam.Year = updated.Year;
periodExam.Detail = updated.Detail;
periodExam.AnnounceDate = updated.AnnounceDate;
periodExam.IsActive = updated.IsActive;
periodExam.LastUpdatedAt = DateTime.Now;
periodExam.LastUpdateUserId = UserId ?? "";
periodExam.LastUpdateFullName = FullName ?? "";
await _context.SaveChangesAsync();
}
public async Task DeleteAsync(string examId)
{
var periodExam = await _context.PeriodExams.AsQueryable()
.FirstOrDefaultAsync(x => x.Id == Guid.Parse(examId));
if (periodExam == null)
throw new Exception(GlobalMessages.ExamNotFound);
_context.PeriodExams.Remove(periodExam);
await _context.SaveChangesAsync();
}
public async Task<IEnumerable<Candidate?>> GetsCandidateByStatusAsync(string examId, string status)
{
var periodExam = await _context.PeriodExams.AsQueryable()
.FirstOrDefaultAsync(x => x.Id == Guid.Parse(examId));
if (periodExam == null)
throw new Exception(GlobalMessages.ExamNotFound);
if (status == "all")
{
return await _context.Candidates.AsQueryable()
.Where(x => x.PeriodExam == periodExam && x.Status != "register" && x.Status != "rejectRegister")
.ToListAsync();
}
else
{
return await _context.Candidates.AsQueryable()
.Where(x => x.PeriodExam == periodExam && x.Status == status)
.ToListAsync();
}
}
public async Task<CandidateInformationResponseItem?> GetsAsyncInformation(string candidateId)
{
return await _context.Candidates.AsQueryable()
.Where(x => x.Id == Guid.Parse(candidateId))
.Select(x => new CandidateInformationResponseItem
{
Prefix = x.Prefix,
PrefixId = x.Prefix != null ? x.Prefix.Id.ToString() : null,
FirstName = x.FirstName,
LastName = x.LastName,
Nationality = x.Nationality,
DateOfBirth = x.DateOfBirth,
Relationship = x.Relationship,
RelationshipId = x.Relationship != null ? x.Relationship.Id.ToString() : null,
CitizenProvince = x.CitizenProvince,
CitizenProvinceId = x.CitizenProvince != null ? x.CitizenProvince.Id.ToString() : null,
CitizenDistrict = x.CitizenDistrict,
CitizenDistrictId = x.CitizenDistrict != null ? x.CitizenDistrict.Id.ToString() : null,
CitizenDate = x.CitizenDate,
Email = x.Email,
CitizenId = x.CitizenId,
Telephone = x.Telephone,
MobilePhone = x.MobilePhone,
Knowledge = x.Knowledge,
})
.FirstOrDefaultAsync();
}
public async Task<CandidateOccupationResponseItem?> GetsAsyncOccupation(string candidateId)
{
return await _context.Candidates.AsQueryable()
.Where(x => x.Id == Guid.Parse(candidateId))
.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<CandidateAddressResponseItem?> GetsAsyncAddress(string candidateId)
{
return await _context.Candidates.AsQueryable()
.Where(x => x.Id == Guid.Parse(candidateId))
.Select(x => new CandidateAddressResponseItem
{
RegistAddress = x.RegistAddress,
RegistProvince = x.RegistProvince,
RegistProvinceId = x.RegistProvince != null ? x.RegistProvince.Id.ToString() : null,
RegistDistrict = x.RegistDistrict,
RegistDistrictId = x.RegistDistrict != null ? x.RegistDistrict.Id.ToString() : null,
RegistSubDistrict = x.RegistSubDistrict,
RegistSubDistrictId = x.RegistSubDistrict != null ? x.RegistSubDistrict.Id.ToString() : null,
RegistZipCode = x.RegistZipCode,
RegistSame = x.RegistSame,
CurrentAddress = x.CurrentAddress,
CurrentProvince = x.CurrentProvince,
CurrentProvinceId = x.CurrentProvince != null ? x.CurrentProvince.Id.ToString() : null,
CurrentDistrict = x.CurrentDistrict,
CurrentDistrictId = x.CurrentDistrict != null ? x.CurrentDistrict.Id.ToString() : null,
CurrentSubDistrict = x.CurrentSubDistrict,
CurrentSubDistrictId = x.CurrentSubDistrict != null ? x.CurrentSubDistrict.Id.ToString() : null,
CurrentZipCode = x.CurrentZipCode,
})
.FirstOrDefaultAsync();
}
public async Task<CandidateFamilyResponseItem?> GetsAsyncFamily(string candidateId)
{
return await _context.Candidates.AsQueryable()
.Where(x => x.Id == Guid.Parse(candidateId))
.Select(x => new CandidateFamilyResponseItem
{
Marry = x.Marry,
MarryPrefix = x.MarryPrefix,
MarryPrefixId = x.MarryPrefix != null ? x.MarryPrefix.Id.ToString() : null,
MarryFirstName = x.MarryFirstName,
MarryLastName = x.MarryLastName,
MarryOccupation = x.MarryOccupation,
MarryNationality = x.MarryNationality,
FatherPrefix = x.FatherPrefix,
FatherPrefixId = x.FatherPrefix != null ? x.FatherPrefix.Id.ToString() : null,
FatherFirstName = x.FatherFirstName,
FatherLastName = x.FatherLastName,
FatherOccupation = x.FatherOccupation,
FatherNationality = x.FatherNationality,
MotherPrefix = x.MotherPrefix,
MotherPrefixId = x.MotherPrefix != null ? x.MotherPrefix.Id.ToString() : null,
MotherFirstName = x.MotherFirstName,
MotherLastName = x.MotherLastName,
MotherOccupation = x.MotherOccupation,
MotherNationality = x.MotherNationality,
})
.FirstOrDefaultAsync();
}
public async Task<IEnumerable<Education?>> GetsAsyncEducation(string candidateId)
{
return await _context.Educations.AsQueryable()
.Include(x => x.EducationLevel)
.Where(x => x.Id == Guid.Parse(candidateId))
.OrderBy(d => d.DurationStart)
.ToListAsync();
}
public async Task<IEnumerable<Career?>> GetsAsyncCareer(string candidateId)
{
return await _context.Careers.AsQueryable()
.Where(x => x.Id == Guid.Parse(candidateId))
.OrderBy(d => d.DurationStart)
.ToListAsync();
}
#endregion
}
}