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));
        }
示例#2
0
        public async Task <IActionResult> PostCandidateSentToInterview([FromBody] CandidateSentToIntrerviewBindModel bindCandidate)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }
            var job = await _context.Jobs.Include(j => j.Recruiter).FirstOrDefaultAsync(j => j.JobId == bindCandidate.JobId);

            if (!User.HasClaim(claim => (claim.Type == DefinedClaimTypes.RecruiterId && claim.Value == job.RecruiterId.ToString()) ||
                               (claim.Type == DefinedClaimTypes.Access && claim.Value == DefinedClaimAccessValues.Elevated)))
            {
                return(BadRequest());
            }

            var candidateSent = new CandidateSentToIntrerview();

            if (bindCandidate.JobId > 0 && bindCandidate.CandidateId > 0)
            {
                var checkForExistingRecord = await _context.CandidatesSentToInterview.FirstOrDefaultAsync(cs => cs.CandidateId == bindCandidate.CandidateId && cs.JobId == bindCandidate.JobId);

                if (checkForExistingRecord == null)
                {
                    candidateSent.JobId       = bindCandidate.JobId;
                    candidateSent.CandidateId = bindCandidate.CandidateId;
                }
            }
            else
            {
                return(BadRequest());
            }


            _context.CandidatesSentToInterview.Add(candidateSent);
            await _context.SaveChangesAsync();

            return(Ok());
        }
        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());
        }