Exemple #1
0
        public long GenerateTest(long testId)
        {
            DB.WorkTest test = null;
            var         t    = testRepository.Test.FirstOrDefault(x => x.Id == testId);

            if (t != null)
            {
                test = new DB.WorkTest()
                {
                    UserId                = accountService.GetCurrentUserId(),
                    TestId                = testId,
                    StartDate             = DateTime.UtcNow,
                    Name                  = t.Name,
                    IsCanShowResultAnswer = t.IsCanShowResultAnswer
                };

                workTestRepository.WorkTest.Add(test);
                workTestRepository.SaveChanges();

                foreach (var q in t.TestQuestions.Where(x => !x.IsHide).OrderBy(x => random.Next()).Take(t.CountQuestionForShow))
                {
                    DB.WorkTestQuestion qu = new DB.WorkTestQuestion
                    {
                        WorkTestId     = test.Id,
                        Name           = q.Name,
                        Text           = q.Text,
                        TestQuestionId = q.Id,
                    };

                    workTestRepository.WorkTestQuestion.Add(qu);
                    workTestRepository.SaveChanges();

                    foreach (var a in q.TestAnswers.Where(x => !x.IsHide).OrderBy(x => random.Next()))
                    {
                        DB.WorkTestAnswer an = new DB.WorkTestAnswer
                        {
                            IsAnswered         = false,
                            IsRight            = a.IsRight,
                            Text               = a.Text,
                            TestAnswerId       = a.Id,
                            WorkTestQuestionId = qu.Id,
                        };
                        workTestRepository.WorkTestAnswer.Add(an);
                        workTestRepository.SaveChanges();
                    }
                }
            }
            if (test != null)
            {
                return(test.Id);
            }
            return(-1);
        }
Exemple #2
0
        /*// GET: api/WorkTestAnswer
         * public IEnumerable<string> Get()
         * {
         *  return new string[] { "value1", "value2" };
         * }
         *
         * // GET: api/WorkTestAnswer/5
         * public string Get(int id)
         * {
         *  return "value";
         * }
         *
         * // POST: api/WorkTestAnswer
         * public void Post([FromBody]string value)
         * {
         * }
         */
        // PUT: api/WorkTestAnswer/5
        public HttpResponseMessage Put(int id, TestAnswer value)
        {
            long userID = accountService.GetCurrentUserId();
            var  answer = workTestRep
                          .WorkTestAnswer
                          .Where(x =>
                                 x.Id == id &&
                                 x.WorkTestQuestion.WorkTest.EndDate == null &&
                                 x.WorkTestQuestion.WorkTest.UserId == userID
                                 )
                          .FirstOrDefault();

            if (answer != null)
            {
                answer.IsAnswered   = value.IsAnswered;
                answer.DateAnswered = DateTime.UtcNow;
                workTestRep.SaveChanges();
            }
            else
            {
                return(new HttpResponseMessage(HttpStatusCode.Forbidden));
            }
            return(new HttpResponseMessage(HttpStatusCode.OK));
        }