private static E.Applicant CreateApplicant(DbContext context, string firstName, string lastName, string emailAddress, string username)
        {
            Common.Helpers.ValidationHelper.ValidateRequiredField(firstName, "First Name");
            Common.Helpers.ValidationHelper.ValidateRequiredField(lastName, "Last Name");
            Common.Helpers.ValidationHelper.ValidateRequiredField(emailAddress, "Email Address");

            Common.Helpers.ValidationHelper.ValidateStringLength(firstName, "First Name", Constants.MaxNameLength);
            Common.Helpers.ValidationHelper.ValidateStringLength(lastName, "Last Name", Constants.MaxNameLength);
            Common.Helpers.ValidationHelper.ValidateStringLength(emailAddress, "Email Address", Constants.MaxEmailAddressLength);

            Common.Helpers.ValidationHelper.ValidateEmailAddress(emailAddress);

            E::Applicant newApplicant = new E::Applicant();

            newApplicant.ID              = Guid.NewGuid();
            newApplicant.FirstName       = firstName;
            newApplicant.LastName        = lastName;
            newApplicant.EmailAddress    = emailAddress;
            newApplicant.CreatedBy       = username;
            newApplicant.CreatedDate     = DateTime.UtcNow;
            newApplicant.LastUpdatedBy   = username;
            newApplicant.LastUpdatedDate = DateTime.UtcNow;

            context.Applicants.Add(newApplicant);

            return(newApplicant);
        }
        public void DeleteApplicant(DeleteApplicantRequest request)
        {
            AuthToken authToken = null;

            try
            {
                Common.Helpers.ValidationHelper.ValidateRequiredField(request.AuthToken, "Auth Token");
                Common.Helpers.ValidationHelper.ValidateRequiredField(request.AntiForgeryToken, "Anti Forgery Token");

                if (!UserController.ValidateSession(request.AuthToken, out authToken))
                {
                    throw new AuthenticationException("Authentication failed.");
                }

                UserController.ValidateAntiForgeryToken(request.AntiForgeryToken, authToken);

                DbContext context = DataController.CreateDbContext();

                E::Applicant applicant = context.Applicants
                                         .Where(q => q.ID == request.ApplicantID)
                                         .FirstOrDefault();

                var attempts = context.Attempts
                               .Where(a => a.InterviewQuestion.Interview.ApplicantID == request.ApplicantID)
                               .Select(t => new
                {
                    Code   = t.Code,
                    Output = t.Output
                });

                foreach (var attempt in attempts.AsEnumerable())
                {
                    DataController.DeleteBlob(attempt.Code.ToString());
                    DataController.DeleteBlob(attempt.Output.ToString());
                }

                context.Applicants.Remove(applicant);

                context.SaveChanges();
            }
            catch (AuthenticationException ex)
            {
                throw new WebFaultException <string>(ex.Message, System.Net.HttpStatusCode.BadRequest);
            }
            catch (Common.Exceptions.ValidationException ex)
            {
                throw new WebFaultException <string>(ex.Message, System.Net.HttpStatusCode.BadRequest);
            }
            catch (Exception ex)
            {
                ExceptionHelper.Log(ex, authToken == null ? null : authToken.Username);
                throw new WebFaultException <string>("An unknown error has occurred.", System.Net.HttpStatusCode.InternalServerError);
            }
        }
        public void EditApplicant(EditApplicantRequest request)
        {
            AuthToken authToken = null;

            try
            {
                Common.Helpers.ValidationHelper.ValidateRequiredField(request.AuthToken, "Auth Token");
                Common.Helpers.ValidationHelper.ValidateRequiredField(request.AntiForgeryToken, "Anti Forgery Token");
                Common.Helpers.ValidationHelper.ValidateRequiredField(request.FirstName, "First Name");
                Common.Helpers.ValidationHelper.ValidateRequiredField(request.LastName, "Last Name");
                Common.Helpers.ValidationHelper.ValidateRequiredField(request.EmailAddress, "Email Address");

                Common.Helpers.ValidationHelper.ValidateStringLength(request.FirstName, "First Name", Constants.MaxNameLength);
                Common.Helpers.ValidationHelper.ValidateStringLength(request.LastName, "Last Name", Constants.MaxNameLength);
                Common.Helpers.ValidationHelper.ValidateStringLength(request.EmailAddress, "Email Address", Constants.MaxEmailAddressLength);

                Common.Helpers.ValidationHelper.ValidateEmailAddress(request.EmailAddress);

                if (!UserController.ValidateSession(request.AuthToken, out authToken))
                {
                    throw new AuthenticationException("Authentication failed.");
                }

                UserController.ValidateAntiForgeryToken(request.AntiForgeryToken, authToken);

                DbContext context = DataController.CreateDbContext();

                E::Applicant applicant = context.Applicants.FirstOrDefault(q => q.ID == request.ApplicantID);

                applicant.FirstName       = request.FirstName;
                applicant.LastName        = request.LastName;
                applicant.EmailAddress    = request.EmailAddress;
                applicant.LastUpdatedBy   = authToken.Username;
                applicant.LastUpdatedDate = DateTime.UtcNow;

                context.SaveChanges();
            }
            catch (AuthenticationException ex)
            {
                throw new WebFaultException <string>(ex.Message, System.Net.HttpStatusCode.BadRequest);
            }
            catch (Common.Exceptions.ValidationException ex)
            {
                throw new WebFaultException <string>(ex.Message, System.Net.HttpStatusCode.BadRequest);
            }
            catch (Exception ex)
            {
                ExceptionHelper.Log(ex, authToken == null ? null : authToken.Username);
                throw new WebFaultException <string>("An unknown error has occurred.", System.Net.HttpStatusCode.InternalServerError);
            }
        }
        public CreateApplicantResponse CreateApplicant(CreateApplicantRequest request)
        {
            CreateApplicantResponse response = new CreateApplicantResponse();

            AuthToken authToken = null;

            try
            {
                Common.Helpers.ValidationHelper.ValidateRequiredField(request.AuthToken, "Auth Token");
                Common.Helpers.ValidationHelper.ValidateRequiredField(request.AntiForgeryToken, "Anti Forgery Token");

                if (!UserController.ValidateSession(request.AuthToken, out authToken))
                {
                    throw new AuthenticationException("Authentication failed.");
                }

                UserController.ValidateAntiForgeryToken(request.AntiForgeryToken, authToken);

                DbContext context = DataController.CreateDbContext();

                E::Applicant newApplicant = CreateApplicant(context, request.FirstName, request.LastName, request.EmailAddress, authToken.Username);

                context.SaveChanges();

                response.ApplicantID = newApplicant.ID;
            }
            catch (AuthenticationException ex)
            {
                throw new WebFaultException <string>(ex.Message, System.Net.HttpStatusCode.BadRequest);
            }
            catch (Common.Exceptions.ValidationException ex)
            {
                throw new WebFaultException <string>(ex.Message, System.Net.HttpStatusCode.BadRequest);
            }
            catch (Exception ex)
            {
                ExceptionHelper.Log(ex, authToken == null ? null : authToken.Username);
                throw new WebFaultException <string>("An unknown error has occurred.", System.Net.HttpStatusCode.InternalServerError);
            }

            return(response);
        }
        private static E.Applicant CreateApplicant(DbContext context, string firstName, string lastName, string emailAddress, string username)
        {
            Common.Helpers.ValidationHelper.ValidateRequiredField(firstName, "First Name");
            Common.Helpers.ValidationHelper.ValidateRequiredField(lastName, "Last Name");
            Common.Helpers.ValidationHelper.ValidateRequiredField(emailAddress, "Email Address");

            Common.Helpers.ValidationHelper.ValidateStringLength(firstName, "First Name", Constants.MaxNameLength);
            Common.Helpers.ValidationHelper.ValidateStringLength(lastName, "Last Name", Constants.MaxNameLength);
            Common.Helpers.ValidationHelper.ValidateStringLength(emailAddress, "Email Address", Constants.MaxEmailAddressLength);

            Common.Helpers.ValidationHelper.ValidateEmailAddress(emailAddress);

            E::Applicant newApplicant = new E::Applicant();

            newApplicant.ID = Guid.NewGuid();
            newApplicant.FirstName = firstName;
            newApplicant.LastName = lastName;
            newApplicant.EmailAddress = emailAddress;
            newApplicant.CreatedBy = username;
            newApplicant.CreatedDate = DateTime.UtcNow;
            newApplicant.LastUpdatedBy = username;
            newApplicant.LastUpdatedDate = DateTime.UtcNow;

            context.Applicants.Add(newApplicant);

            return newApplicant;
        }
        public QuickStartResponse QuickStart(QuickStartRequest request)
        {
            QuickStartResponse response = new QuickStartResponse();

            AuthToken authToken = null;

            try
            {
                Common.Helpers.ValidationHelper.ValidateRequiredField(request.AuthToken, "Auth Token");
                Common.Helpers.ValidationHelper.ValidateRequiredField(request.AntiForgeryToken, "Anti Forgery Token");

                Common.Helpers.ValidationHelper.Assert(request.MinutesAllowed > 0, "Minutes allowed must be positive");
                Common.Helpers.ValidationHelper.Assert(request.MinutesAllowed <= Constants.MaxInterviewDuration, "Time allocated must not be greater than two hours.");

                Common.Helpers.ValidationHelper.ValidateEmailAddress(request.EmailAddress);

                if (!UserController.ValidateSession(request.AuthToken, out authToken))
                {
                    throw new AuthenticationException("Authentication failed.");
                }

                UserController.ValidateAntiForgeryToken(request.AntiForgeryToken, authToken);

                DbContext context =  DataController.CreateDbContext();

                int numberOfQuestions = context.QuestionSetQuestions.Count(q => q.QuestionSetID == request.QuestionSetID);

                if (numberOfQuestions == 0)
                {
                    throw new Common.Exceptions.ValidationException("The question set has no questions.");
                }

                E::Applicant newApplicant = new E::Applicant();

                newApplicant.ID = Guid.NewGuid();
                newApplicant.FirstName = string.Empty;
                newApplicant.LastName = string.Empty;
                newApplicant.EmailAddress = request.EmailAddress;
                newApplicant.CreatedBy = authToken.Username;
                newApplicant.CreatedDate = DateTime.UtcNow;
                newApplicant.LastUpdatedBy = authToken.Username;
                newApplicant.LastUpdatedDate = DateTime.UtcNow;

                context.Applicants.Add(newApplicant);

                E::Interview newInterview = new E::Interview();

                newInterview.ID = Guid.NewGuid();
                newInterview.ApplicantID = newApplicant.ID;
                newInterview.MinutesAllowed = request.MinutesAllowed;
                newInterview.CreatedBy = authToken.Username;
                newInterview.CreatedDate = DateTime.UtcNow;
                newInterview.LastUpdatedBy = authToken.Username;
                newInterview.LastUpdatedDate = DateTime.UtcNow;

                if (request.SendInvitation)
                {
                    newInterview.SentDate = DateTime.UtcNow;
                }

                context.Interviews.Add(newInterview);

                AddQuestions(context, newInterview.ID, true, request.QuestionSetID, null);

                context.SaveChanges();

                if (request.SendInvitation)
                {
                    EmailController.SendInvitationEmail(newInterview.ID, authToken.Username);
                }

                response.InterviewID = newInterview.ID;
            }
            catch (AuthenticationException ex)
            {
                throw new WebFaultException<string>(ex.Message, System.Net.HttpStatusCode.BadRequest);
            }
            catch (Common.Exceptions.ValidationException ex)
            {
                throw new WebFaultException<string>(ex.Message, System.Net.HttpStatusCode.BadRequest);
            }
            catch (Exception ex)
            {
                ExceptionHelper.Log(ex, authToken == null ? null : authToken.Username);
                throw new WebFaultException<string>("An unknown error has occurred.", System.Net.HttpStatusCode.InternalServerError);
            }

            return response;
        }