Skip to content

Commit

Permalink
Merge pull request #603 from bcgov/fixes/refactor-cert-repo-query
Browse files Browse the repository at this point in the history
Refactor Certification Repository Query function
  • Loading branch information
qsl-deepak authored Oct 17, 2024
2 parents 42d078b + cac369c commit e12073a
Showing 1 changed file with 22 additions and 24 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -21,52 +21,50 @@ public async Task<IEnumerable<Certification>> Query(UserCertificationQuery query
var Certifications = context.ecer_CertificateSet.AsQueryable();
var Registrants = context.ContactSet.AsQueryable();

// Initial join without filtering
var queryWithJoin = from cert in Certifications
join reg in Registrants on cert.ecer_Registrantid.Id equals reg.Id
select new { cert, reg };

// Filtering by ID
if (query.ById != null) Certifications = Certifications.Where(r => r.ecer_CertificateId == Guid.Parse(query.ById));
if (query.ById != null) queryWithJoin = queryWithJoin.Where(r => r.cert.ecer_CertificateId == Guid.Parse(query.ById));

// Filtering by applicant ID
if (query.ByApplicantId != null) Certifications = Certifications.Where(r => r.ecer_Registrantid.Id == Guid.Parse(query.ByApplicantId));
if (query.ByApplicantId != null) queryWithJoin = queryWithJoin.Where(r => r.cert.ecer_Registrantid.Id == Guid.Parse(query.ByApplicantId));

// Filtering by First Name
if (!string.IsNullOrEmpty(query.ByFirstName))
{
Certifications = from cert in Certifications
join reg in Registrants on cert.ecer_Registrantid.Id equals reg.Id
where reg.FirstName == (query.ByFirstName)
select cert;
queryWithJoin = queryWithJoin.Where(x => x.reg.FirstName == query.ByFirstName);
}

// Filtering by Last Name
if (!string.IsNullOrEmpty(query.ByLastName))
{
Certifications = from cert in Certifications
join reg in Registrants on cert.ecer_Registrantid.Id equals reg.Id
where reg.LastName == (query.ByLastName)
select cert;
queryWithJoin = queryWithJoin.Where(x => x.reg.LastName == query.ByLastName);
}

// Filtering by certificate number
if (!string.IsNullOrEmpty(query.ByCertificateNumber))
Certifications = Certifications.Where(r => r.ecer_CertificateNumber.Equals(query.ByCertificateNumber));
queryWithJoin = queryWithJoin.Where(r => r.cert.ecer_CertificateNumber.Equals(query.ByCertificateNumber));

//Order by latest first (based on expiry date),
queryWithJoin = queryWithJoin.OrderByDescending(r => r.cert.ecer_ExpiryDate);

//Apply Pagination
if (query.PageSize > 0)
{
queryWithJoin = queryWithJoin.Skip(query.PageNumber).Take(query.PageSize);
}

var results = context.From(Certifications)
var results = context.From(queryWithJoin.Select(c => c.cert))
.Join()
.Include(a => a.ecer_certifiedlevel_CertificateId)
.Include(a => a.ecer_documenturl_CertificateId)
.Include(a => a.ecer_certificate_Registrantid)
.IncludeNested(a => a.ecer_certificateconditions_Registrantid)
.Execute().GroupBy(r => r.ecer_Registrantid.Id) // Group by unique identifier (assuming RegistrantId)
.Select(g => g.OrderByDescending(r => r.ecer_ExpiryDate).FirstOrDefault()); // Select latest by expiry date

// Calculate the correct number of items to skip
int skipAmount = 0;
if (query.PageNumber > 1)
{
skipAmount = (query.PageNumber - 1) * query.PageSize;
}

var paginatedResults = results.Skip(skipAmount).Take(query.PageSize); // Apply Pagination
.Execute().GroupBy(r => r.ecer_Registrantid.Id).Select(g => g.FirstOrDefault()); // Group by unique identifier (assuming RegistrantId)

return mapper.Map<IEnumerable<Certification>>(paginatedResults)!.ToList();
return mapper.Map<IEnumerable<Certification>>(results)!.ToList();
}
}

0 comments on commit e12073a

Please sign in to comment.