/// <summary>
        ///
        /// </summary>
        /// <param name="candidate"></param>
        public void Post(HunterCV.Common.Candidate favorite)
        {
            string userName = Membership.GetUser().UserName;

            using (hunterCVEntities context = new hunterCVEntities())
            {
                var user      = context.Users.Single(u => u.UserName == userName);
                var candidate = context.Candidates.Single(c => c.CandidateID == favorite.CandidateID);

                candidate.Starred = favorite.Starred;

                context.SaveChanges();
            }
        }
        /// <summary>
        ///
        /// </summary>
        /// <param name="candidate"></param>
        public IEnumerable <HunterCV.Common.Candidate> Put(HunterCV.Common.Candidate candidate)
        {
            string userName = Membership.GetUser().UserName;

            using (hunterCVEntities context = new hunterCVEntities())
            {
                var curruser = context.Users.Single(user => user.UserName == userName);

                if (!candidate.SkipDuplicatesCheck)
                {
                    //check duplicates
                    IEnumerable <Candidate> candidates = context.Candidates.Where(c => c.FirstName.ToLower().Trim() == candidate.FirstName.ToLower().Trim() &&
                                                                                  c.LastName.ToLower().Trim() == candidate.LastName.ToLower().Trim() && c.CandidateID != candidate.CandidateID);

                    if (candidates.Any())
                    {
                        return(candidates.Select(p => Mapper.Map <Candidate, HunterCV.Common.Candidate>(p)).ToList());
                    }
                }

                var entity = Mapper.Map <HunterCV.Common.Candidate, Candidate>(candidate);

                //delete old positions
                context.CandidatePositions.Where(c => c.CandidateId == candidate.CandidateID).Each(d =>
                {
                    context.CandidatePositions.DeleteObject(d);
                });

                entity.UserId = curruser.UserId;

                context.Candidates.Attach(entity);
                context.ObjectStateManager.ChangeObjectState(entity, EntityState.Modified);

                foreach (HunterCV.Common.CandidatePosition position in candidate.CandidatePositions)
                {
                    entity.CandidatePositions.Add(new CandidatePosition
                    {
                        CandidateId             = candidate.CandidateID,
                        PositionId              = position.PositionId,
                        CandidatePositionDate   = position.CandidatePositionDate,
                        CandidatePositionStatus = position.CandidatePositionStatus
                    });
                }

                context.SaveChanges();

                return(null);
            }
        }
        /// <summary>
        ///
        /// </summary>
        /// <param name="candidate"></param>
        public HunterCV.Common.PostCandidateApiResponse Post(HunterCV.Common.Candidate candidate)
        {
            string userName = Membership.GetUser().UserName;

            var response = new HunterCV.Common.PostCandidateApiResponse();

            using (hunterCVEntities context = new hunterCVEntities())
            {
                var user = context.Users.Single(u => u.UserName == userName);
                var role = context.Users.Single(u => u.UserName == userName)
                           .Roles.Single();

                if (role.LicenseType == "Free")
                {
                    int count = context.Candidates.Where(c => c.UserId == user.UserId).Count();

                    if (count >= 10)
                    {
                        throw new HttpResponseException(
                                  new HttpResponseMessage
                        {
                            StatusCode   = HttpStatusCode.Forbidden,
                            Content      = new StringContent("License"),
                            ReasonPhrase = "This license type does not allow the operation"
                        });
                    }
                }

                if (!candidate.SkipDuplicatesCheck)
                {
                    //check duplicates
                    IEnumerable <Candidate> candidates = context.Candidates.Where(c => c.FirstName.ToLower().Trim() == candidate.FirstName.ToLower().Trim() &&
                                                                                  c.LastName.ToLower().Trim() == candidate.LastName.ToLower().Trim());

                    if (candidates.Any())
                    {
                        response.Duplicates = candidates.Select(p => Mapper.Map <Candidate, HunterCV.Common.Candidate>(p)).ToList();
                        return(response);
                    }
                }

                int startIndex = 1000;

                //get start index
                var      docSettings       = XDocument.Parse(role.Settings);
                var      elementsSettings  = docSettings.Root.Elements();
                XElement startIndexElement =
                    (from el in docSettings.Root.Elements("setting")
                     where (string)el.Attribute("title") == "CandidatesStartIndex"
                     select el).FirstOrDefault();

                if (startIndexElement != null)
                {
                    startIndex = (int)startIndexElement.Attribute("value");
                }

                if (context.Candidates.Any())
                {
                    startIndex = context.Candidates.Max(c => c.CandidateNumber).Value + 1;
                }

                candidate.CandidateNumber = startIndex;
                response.NewCandidate     = candidate;

                Candidate target = Mapper.Map <HunterCV.Common.Candidate, Candidate>(candidate);
                target.User = user;
                context.Candidates.AddObject(target);

                //check favorite
                //if (candidate.IsFavorite)
                //{
                //    target.FavoriteUsers.Add(user);
                //}

                context.SaveChanges();

                return(response);
            }
        }