public void EditInterview(EditInterviewRequest 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.Assert(request.MinutesAllowed > 0, "Minutes allowed must be positive"); Common.Helpers.ValidationHelper.Assert(request.QuestionIDs != null, "No questions specified"); Common.Helpers.ValidationHelper.Assert(request.QuestionIDs.Any(), "No questions specified"); if (!UserController.ValidateSession(request.AuthToken, out authToken)) { throw new AuthenticationException("Authentication failed."); } UserController.ValidateAntiForgeryToken(request.AntiForgeryToken, authToken); DbContext context = DataController.CreateDbContext(); E::Interview interview = context.Interviews.FirstOrDefault(q => q.ID == request.InterviewID); InterviewStatus status = StatusHelper.GetInterviewStatus(interview); if (status > InterviewStatus.WaitingForApplicant) { throw new Common.Exceptions.ValidationException("Cannot edit an interview which has already started."); } E::InterviewQuestion[] interviewQuestions = context.InterviewQuestions.Where(iq => iq.InterviewID == request.InterviewID).ToArray(); foreach (E::InterviewQuestion interviewQuestion in interviewQuestions) { if (!request.QuestionIDs.Contains(interviewQuestion.QuestionID)) { context.InterviewQuestions.Remove(interviewQuestion); } } foreach (Guid questionId in request.QuestionIDs.Except(interviewQuestions.Select(iq => iq.QuestionID))) { E::InterviewQuestion newInterviewQuestion = new E.InterviewQuestion(); newInterviewQuestion.ID = Guid.NewGuid(); newInterviewQuestion.InterviewID = request.InterviewID; newInterviewQuestion.QuestionID = questionId; context.InterviewQuestions.Add(newInterviewQuestion); } interview.MinutesAllowed = request.MinutesAllowed; interview.LastUpdatedBy = authToken.Username; interview.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); } }
private static void AddQuestions(DbContext context, Guid interviewId, bool useQuestionSet, Guid? questionSetId, Guid[] questionIds) { if (useQuestionSet) { questionIds = context.QuestionSetQuestions .Where(q => q.QuestionSetID == questionSetId.Value) .Select(q => q.QuestionID) .ToArray(); } foreach (Guid questionId in questionIds) { E::InterviewQuestion newInterviewQuestion = new E.InterviewQuestion(); newInterviewQuestion.ID = Guid.NewGuid(); newInterviewQuestion.InterviewID = interviewId; newInterviewQuestion.QuestionID = questionId; context.InterviewQuestions.Add(newInterviewQuestion); } }