public async Task <IActionResult> PostCandidate([FromBody] BindCandidate bindCandidate) { if (!ModelState.IsValid) { return(BadRequest(ModelState)); } if (bindCandidate.RecruiterId < 1) { return(BadRequest()); } var newCandidate = new Candidate() { Name = bindCandidate.Name, Email = bindCandidate.Email, Phone = bindCandidate.Phone }; if (bindCandidate.RecruiterId > 0) { newCandidate.Recruiter = await _context.Recruiters.FirstOrDefaultAsync(r => r.RecruiterId == bindCandidate.RecruiterId); } if (bindCandidate.CandidateStatusId > 0) { newCandidate.CandidateStatus = await _context.CandidateStatuses.FirstOrDefaultAsync(s => s.CandidateStatusId == bindCandidate.CandidateStatusId); } _context.Candidates.Add(newCandidate); if (bindCandidate.JobId > 0) { var candiadetSent = new CandidateSentToIntrerview() { JobId = bindCandidate.JobId, CandidateId = newCandidate.CandidateId }; _context.CandidatesSentToInterview.Add(candiadetSent); } await _context.SaveChangesAsync(); return(CreatedAtAction("GetCandidate", new { id = newCandidate.CandidateId }, newCandidate)); }
public async Task <IActionResult> PutCandidate([FromRoute] int id, [FromBody] BindCandidate bindCandidate) { if (!ModelState.IsValid) { return(BadRequest(ModelState)); } if (id != bindCandidate.CandidateId) { return(BadRequest()); } if (!User.HasClaim(claim => (claim.Type == DefinedClaimTypes.RecruiterId && claim.Value == bindCandidate.RecruiterId.ToString()) || (claim.Type == DefinedClaimTypes.Access && claim.Value == DefinedClaimAccessValues.Elevated))) { return(BadRequest()); } var updatedCandidate = await _context.Candidates.FirstOrDefaultAsync(c => c.CandidateId == bindCandidate.CandidateId); updatedCandidate.Name = bindCandidate.Name; updatedCandidate.Email = bindCandidate.Email; updatedCandidate.Phone = bindCandidate.Phone; if (bindCandidate.JobId > 0) { var candidateSent = new CandidateSentToIntrerview() { JobId = bindCandidate.JobId, CandidateId = bindCandidate.CandidateId }; _context.CandidatesSentToInterview.Add(candidateSent); } if (bindCandidate.RecruiterId > 0) { updatedCandidate.Recruiter = await _context.Recruiters.FirstOrDefaultAsync(r => r.RecruiterId == bindCandidate.RecruiterId); } if (bindCandidate.CandidateStatusId > 0) { updatedCandidate.CandidateStatus = await _context.CandidateStatuses.FirstOrDefaultAsync(s => s.CandidateStatusId == bindCandidate.CandidateStatusId); } _context.Entry(updatedCandidate).State = EntityState.Modified; try { await _context.SaveChangesAsync(); } catch (DbUpdateConcurrencyException) { if (!CandidateExists(id)) { return(NotFound()); } else { return(BadRequest()); } }catch (Exception ex) { var debugEx = ex; return(BadRequest()); } return(NoContent()); }