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; }
public CreateInterviewResponse CreateInterview(CreateInterviewRequest request) { CreateInterviewResponse response = new CreateInterviewResponse(); 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.QuestionSetID.HasValue || (request.QuestionIDs != null && request.QuestionIDs.Any()), "No questions specified"); 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."); if (!UserController.ValidateSession(request.AuthToken, out authToken)) { throw new AuthenticationException("Authentication failed."); } UserController.ValidateAntiForgeryToken(request.AntiForgeryToken, authToken); DbContext context = DataController.CreateDbContext(); E::Interview newInterview = new E::Interview(); newInterview.ID = Guid.NewGuid(); newInterview.ApplicantID = request.ApplicantID; newInterview.MinutesAllowed = request.MinutesAllowed; newInterview.CreatedBy = authToken.Username; newInterview.CreatedDate = DateTime.UtcNow; newInterview.LastUpdatedBy = authToken.Username; newInterview.LastUpdatedDate = DateTime.UtcNow; context.Interviews.Add(newInterview); AddQuestions(context, newInterview.ID, request.UseQuestionSet, request.QuestionSetID, request.QuestionIDs); context.SaveChanges(); 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; }