Business Logic:
I have three tables. Referral, Referrer, ReferralInstance with many to many relationship in Referral and Referer tables. A candidate is provided with a Referral form where he fills a Job Request. The Referral form is posted by the user is shown to all the Referrer (aka Employee) tied to the requested company. Also the Employee in request company can choose to "Accept" or "Reject" that profile (this detail is captured in the instance table).
Behaviour Needed:
Now when candidate adds a Referral Form, based on certain condition (mentioned below) I want to check whether this referral request already exists. If it already exists then I show a warning/pop up.
Condition 1:
When candidate just now posted a referral request. Then ReferralInstance table will be EMPTY, so I need to check if CompanyId, CandidateId and SkillId matches, if all these three matches to an already existing DB Record I want to set hasPreviousRequest to true
Condition 2:
When candidate posted a request and SkillId, CompanyId and CandidateId matches. And many Employees (belonging to that requested company" Rejected him, but no one has accepted then I want to set hasPreviousRequest to true.
Condition 3:
When candidate posted a referral request, and SkillId, CompanyId, CandidateId matches but one of the Referrer has Accepted him for the Job, In that case I want to set set hasPreviousRequest to false.
Below is My Attempt:
[HttpPost]
public JsonResult CheckForExistingReferral(ReferralViewModel viewModel)
{
bool hasPreviousRequest = false;
var candidateId = User.Identity.GetUserId();
// CHCEKING CONDITION ONE
if (_context.Referrals
.Any(r => ((r.CandidateId == candidateId)
&& (r.CompanyId == viewModel.CompanyId)
&& (r.SkillId == viewModel.SkillId))))
{
// NOW CHECKING CONDITION TWO
if (_context.Referrals
.Any(r => ((r.CandidateId == candidateId)
&& (r.CompanyId == viewModel.CompanyId)
&& (r.SkillId == viewModel.SkillId))
&& r.ReferralInstances
.Any(e => (e.ReferrerId != null) && (e.ReferralStatus == "Accepted"))))
{
hasPreviousRequest = false;
}
else
hasPreviousRequest = true;
}
return Json(new { hasPreviousRequest = hasPreviousRequest });
}
In above attempt, I made same partial LINQ Query twice. Either I want a better LINQ to check all the three condition at once. If not then I want to store LINQ Query used in Condition 1 and use it while checking Condition 2.
Something like below:
[HttpPost]
public JsonResult CheckForExistingReferral(ReferralViewModel viewModel)
{
bool hasPreviousRequest = false;
var candidateId = User.Identity.GetUserId();
var PartialLINQ = _context.Referrals
.Any(r => ((r.CandidateId == candidateId)
&& (r.CompanyId == viewModel.CompanyId)
&& (r.SkillId == viewModel.SkillId)));
if (PartialLiinq)
{
// SOMETHING LIKE THIS
if (PartialLinq
&& r.ReferralInstances
.Any(e => (e.ReferrerId != null) && (e.ReferralStatus == "Accepted"))))
{
hasPreviousRequest = false;
}
else
hasPreviousRequest = true;
}
return Json(new { hasPreviousRequest = hasPreviousRequest });
}
EDIT
ReferralInstances has 4 properties:
- Id PK 2. ReferrerId FK 3. ReferralID FK 4. ReferralStatus
CoverLetterId- is that supposed to beSkillId`?ReferralInstancestable for your 2nd condition. What are the properties of yourReferralInstancestable?