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 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> 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 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> 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); return await _context.Candidates.AsQueryable() .Where(x => x.PeriodExam == periodExam && x.Status == status) .ToListAsync(); } #endregion } }