Exemplo n.º 1
0
        public EditQuestionResponse EditQuestion(EditQuestionRequest request)
        {
            EditQuestionResponse response = new EditQuestionResponse();

            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.Name, "Name");

                Common.Helpers.ValidationHelper.ValidateStringLength(request.Name, "Name", Constants.MaxNameLength);
                Common.Helpers.ValidationHelper.ValidateStringLength(request.QuestionBody, "Problem Description", Constants.MaxBlobLength);

                foreach (QuestionTest test in request.Tests)
                {
                    Common.Helpers.ValidationHelper.ValidateRequiredField(test.Name, "Test Name");
                    Common.Helpers.ValidationHelper.ValidateRequiredField(test.Input, "Test Input");
                    Common.Helpers.ValidationHelper.ValidateRequiredField(test.ExpectedOutput, "Test Output");

                    Common.Helpers.ValidationHelper.ValidateStringLength(test.Name, "Test Name", Constants.MaxNameLength);
                    Common.Helpers.ValidationHelper.ValidateStringLength(test.Input, "Test Input", Constants.MaxBlobLength);
                    Common.Helpers.ValidationHelper.ValidateStringLength(test.ExpectedOutput, "Test Output", Constants.MaxBlobLength);
                }

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

                UserController.ValidateAntiForgeryToken(request.AntiForgeryToken, authToken);

                DbContext context = DataController.CreateDbContext();

                Common.Helpers.ValidationHelper.AssertFalse(context.InterviewQuestions
                                                            .Where(iq => iq.QuestionID == request.QuestionID)
                                                            .Where(iq => iq.Interview.StartedDate.HasValue)
                                                            .Any(),
                                                            "This question cannot be edited as it has been used in existing interviews.");

                if (string.IsNullOrEmpty(request.Name))
                {
                    throw new Common.Exceptions.ValidationException("Please fill in the name field.");
                }

                E::Question question = context.Questions
                                       .Where(q => q.ID == request.QuestionID)
                                       .FirstOrDefault();

                question.Name            = request.Name;
                question.LastUpdatedBy   = authToken.Username;
                question.LastUpdatedDate = DateTime.UtcNow;

                DataController.UploadBlob(question.QuestionBody.ToString(), request.QuestionBody);

                if (question.QuestionTypeID == (short)QuestionType.Standard)
                {
                    var tests = TestsController.FromJson(DataController.DownloadBlob(question.Tests.Value.ToString()));

                    IList <TestsController.Test> newTests = new List <TestsController.Test>();

                    foreach (QuestionTest test in request.Tests)
                    {
                        TestsController.Test newTest = new TestsController.Test();

                        newTest.ID             = Guid.NewGuid();
                        newTest.Name           = test.Name;
                        newTest.Input          = test.Input;
                        newTest.ExpectedOutput = test.ExpectedOutput;

                        newTest.ID = Guid.NewGuid();

                        newTests.Add(newTest);
                    }

                    response.TestIDs = tests.Select(t => t.ID).ToArray();

                    DataController.UploadBlob(question.Tests.Value.ToString(), TestsController.ToJson(newTests.ToArray()));
                }

                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);
            }

            return(response);
        }
Exemplo n.º 2
0
        public CreateQuestionResponse CreateQuestion(CreateQuestionRequest request)
        {
            CreateQuestionResponse response = new CreateQuestionResponse();

            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.Name, "Name");

                Common.Helpers.ValidationHelper.ValidateStringLength(request.Name, "Name", Constants.MaxNameLength);
                Common.Helpers.ValidationHelper.ValidateStringLength(request.QuestionBody, "Hidden Code", Constants.MaxBlobLength);

                foreach (QuestionTest test in request.Tests)
                {
                    Common.Helpers.ValidationHelper.ValidateRequiredField(test.Name, "Test Name");
                    Common.Helpers.ValidationHelper.ValidateRequiredField(test.Input, "Test Input");
                    Common.Helpers.ValidationHelper.ValidateRequiredField(test.ExpectedOutput, "Test Output");

                    Common.Helpers.ValidationHelper.ValidateStringLength(test.Name, "Test Name", Constants.MaxNameLength);
                    Common.Helpers.ValidationHelper.ValidateStringLength(test.Input, "Test Input", Constants.MaxBlobLength);
                    Common.Helpers.ValidationHelper.ValidateStringLength(test.ExpectedOutput, "Test Expected Output", Constants.MaxBlobLength);
                }

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

                UserController.ValidateAntiForgeryToken(request.AntiForgeryToken, authToken);

                DbContext context = DataController.CreateDbContext();

                E::Question newQuestion = new E::Question();

                newQuestion.ID              = Guid.NewGuid();
                newQuestion.Name            = request.Name;
                newQuestion.QuestionTypeID  = (short)QuestionType.Standard;
                newQuestion.QuestionBody    = Guid.NewGuid();
                newQuestion.Tests           = Guid.NewGuid();
                newQuestion.CreatedBy       = authToken.Username;
                newQuestion.CreatedDate     = DateTime.UtcNow;
                newQuestion.LastUpdatedBy   = authToken.Username;
                newQuestion.LastUpdatedDate = DateTime.UtcNow;

                DataController.UploadBlob(newQuestion.QuestionBody.ToString(), request.QuestionBody);

                IList <TestsController.Test> tests = new List <TestsController.Test>();

                foreach (QuestionTest test in request.Tests)
                {
                    TestsController.Test newTest = new TestsController.Test();

                    newTest.ID             = Guid.NewGuid();
                    newTest.Name           = test.Name;
                    newTest.Input          = test.Input;
                    newTest.ExpectedOutput = test.ExpectedOutput;

                    newTest.ID = Guid.NewGuid();

                    tests.Add(newTest);
                }

                response.TestIDs = tests.Select(t => t.ID).ToArray();

                DataController.UploadBlob(newQuestion.Tests.Value.ToString(), TestsController.ToJson(tests.ToArray()));

                context.Questions.Add(newQuestion);

                context.SaveChanges();

                response.QuestionID = newQuestion.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);
        }