Exemple #1
0
        public GetQuestionResponse GetQuestion(GetQuestionRequest request)
        {
            GetQuestionResponse response = new GetQuestionResponse();

            AuthToken authToken = null;

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

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

                DbContext context = DataController.CreateDbContext();

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

                response.QuestionBody = DataController.DownloadBlob(question.QuestionBody.ToString());

                response.Name            = question.Name;
                response.LastUpdatedBy   = question.LastUpdatedBy;
                response.LastUpdatedDate = question.LastUpdatedDate.ToShortDateString();
                response.CreatedBy       = question.CreatedBy;
                response.CreatedDate     = question.CreatedDate.ToShortDateString();

                response.CanEdit = !context.InterviewQuestions
                                   .Where(q => q.QuestionID == request.QuestionID)
                                   .Where(q => q.Interview.StartedDate.HasValue)
                                   .Any();

                response.IsCodedTest = question.QuestionTypeID == (short)QuestionType.Coded;

                if (!response.IsCodedTest)
                {
                    var tests = TestsController.FromJson(DataController.DownloadBlob(question.Tests.Value.ToString()));

                    IList <QuestionTest> questionTests = new List <QuestionTest>();

                    foreach (var test in tests.AsEnumerable())
                    {
                        QuestionTest questionTest = new QuestionTest();

                        questionTest.ID             = test.ID;
                        questionTest.Name           = test.Name;
                        questionTest.Input          = test.Input;
                        questionTest.ExpectedOutput = test.ExpectedOutput;

                        questionTests.Add(questionTest);
                    }

                    response.Tests = questionTests.ToArray();
                }
                else
                {
                    response.Tests = new QuestionTest[0];
                }
            }
            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);
        }
Exemple #2
0
        public DownloadInputResponse DownloadInput(DownloadInputRequest request)
        {
            DownloadInputResponse response = new DownloadInputResponse();

            AuthToken authToken = null;

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

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

                DbContext context = DataController.CreateDbContext();

                var question = context.Questions
                               .Where(q => q.ID == request.QuestionID)
                               .Select(q => new
                {
                    QuestionTypeID = q.QuestionTypeID,
                    CodedTestID    = q.CodedTestID,
                    TestsID        = q.Tests
                })
                               .FirstOrDefault();

                if (question.QuestionTypeID == (short)QuestionType.Standard)
                {
                    var testsJson = DataController.DownloadBlob(question.TestsID.Value.ToString());
                    var tests     = TestsController.FromJson(testsJson).OrderBy(t => t.ID);

                    int numberOfTests = Settings.Default.TestsExecutedPerQuestion;
                    var randomTests   = tests.Shuffle(0).Take(numberOfTests).ToList();

                    var selectedTests =
                        from test in randomTests
                        orderby test.Name
                        select test.Input;

                    response.Input = string.Join("\n", selectedTests);
                }
                else
                {
                    response.Input = CodedQuestionController.GetInput(question.CodedTestID.Value, 0);
                }
            }
            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);
        }
Exemple #3
0
        public RunTestsResponse RunTests(RunTestsRequest request)
        {
            RunTestsResponse response = new RunTestsResponse();

            AuthToken authToken = null;

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

                Common.Helpers.ValidationHelper.ValidateStringLength(request.Output, "Output", Constants.MaxBlobLength);

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

                UserController.ValidateAntiForgeryToken(request.AntiForgeryToken, authToken);

                DbContext context = DataController.CreateDbContext();

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

                if (question.LastTestRunOn.HasValue && DateTime.UtcNow - question.LastTestRunOn.Value < TimeSpan.FromSeconds(5))
                {
                    throw new Common.Exceptions.ValidationException("Please wait at least 5 seconds between test runs.");
                }

                question.LastTestRunOn = DateTime.UtcNow;

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

                    int numberOfTests = Settings.Default.TestsExecutedPerQuestion;
                    var randomTests   = tests.Shuffle(0).Take(numberOfTests).ToList();

                    var parsedOutput = request.Output.Lines();

                    if (randomTests.Count > parsedOutput.Length)
                    {
                        throw new Common.Exceptions.ValidationException(string.Format("Invalid output. Expected {0} lines, found {1}.", randomTests.Count, parsedOutput.Length));
                    }
                    else
                    {
                        response.Tests =
                            (from pair in randomTests.OrderBy(t => t.Name).Zip(parsedOutput, Tuple.Create)
                             select new RunTestsResponseItem
                        {
                            TestName = pair.Item1.Name,
                            TestID = pair.Item1.ID,
                            Success = pair.Item1.ExpectedOutput.EqualsLenient(pair.Item2),
                            Input = pair.Item1.Input.Truncate(100),
                            ExpectedOutput = pair.Item1.ExpectedOutput.Truncate(100),
                            Output = pair.Item2.Truncate(100)
                        })
                            .ToArray();
                    }
                }
                else
                {
                    response.Tests = CodedQuestionController.RunTests(question.CodedTestID.Value, request.Output, 0)
                                     .Select(test => new RunTestsResponseItem
                    {
                        TestName       = test.TestName,
                        Success        = test.Success,
                        Input          = test.Input.Truncate(100),
                        ExpectedOutput = test.ExpectedOutput.Truncate(100),
                        Output         = test.Output.Truncate(100)
                    })
                                     .OrderBy(test => test.TestName)
                                     .ToArray();
                }
            }
            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);
        }
Exemple #4
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);
        }