public IHttpActionResult UpdateTestStructure(int AdminId, TestStructure testStructure)
 {
     using (db_OnlineExaminationEntities db = new db_OnlineExaminationEntities())
     {
         TestStructure oldData = db.TestStructures /*.AsNoTracking()*/.Where(ts => ts.Id == testStructure.Id).FirstOrDefault();
         if (oldData == null || !oldData.IsCurrent)
         {
             return(NotFound());
         }
         oldData.SetProperties(AdminId: AdminId, IsCurrent: false /*, SetNumberOfQuestions: false*/);
         db.SaveChanges();
         testStructure.Id = 0;
         if (testStructure.SetProperties(AdminId: AdminId))
         {
             db.TestStructures.Add(testStructure);
             try
             {
                 db.SaveChanges();
             }
             catch (DbUpdateException)
             {
                 if (TestStructureExists(testStructure.Id))
                 {
                     return(Conflict());
                 }
                 else
                 {
                     throw;
                 }
             }
             return(Ok(testStructure));
         }
         return(BadRequest());
     }
 }
        public IHttpActionResult GetUserReports(int UserId)
        {
            List <Test> tests = db.Tests.Where(t => t.UserId == UserId).ToList();

            if (tests == null)
            {
                return(NotFound());
            }
            for (int i = 0; i < tests.Count; i++)
            {
                Test test = tests[i];
                if (test.Score == null)
                {
                    int passingScore = test.TestStructure.PassingScore;
                    int totalScore   = 0;
                    foreach (TestQuestion testQuestion in test.TestQuestions)
                    {
                        if (testQuestion.Question.CorrectOption == testQuestion.UserAnswer)
                        {
                            totalScore++;
                        }
                    }
                    test.Score   = totalScore;
                    test.Result  = totalScore >= passingScore ? true : false;
                    test.EndTime = System.DateTime.Now < test.EndTime ? System.DateTime.Now : test.EndTime;
                    db.SaveChanges();
                }
            }
            return(Ok(tests));
        }
예제 #3
0
        public IHttpActionResult AddFile()
        {
            int            AdminId;
            HttpPostedFile CSV;

            try
            {
                AdminId = Convert.ToInt32(HttpContext.Current.Request.Form.Get("id"));
                CSV     = HttpContext.Current.Request.Files[0];
            }
            catch
            {
                return(BadRequest("No file found"));
            }
            QuestionFile file = new QuestionFile();

            file.SetProperties(AdminId: AdminId, FileName: CSV.FileName);
            db.QuestionFiles.Add(file);
            db.SaveChanges();
            bool        headersRow = true;
            CSVQuestion temp;

            using (CSVReader reader = new CSVReader(CSV.InputStream))
            {
                CSVQuestion row = new CSVQuestion();
                while (reader.ReadRow(row))
                {
                    if (headersRow)
                    {
                        headersRow = false;
                        continue;
                    }
                    temp = new CSVQuestion();
                    temp.AddRange(row);
                    Question question = new Question();
                    if (question.SetProperties(file.Id, temp))
                    {
                        try {
                            db.Questions.Add(question);
                            db.SaveChanges();
                        }
                        catch (DbEntityValidationException e)
                        {
                            foreach (var eve in e.EntityValidationErrors)
                            {
                                System.Diagnostics.Debug.WriteLine("Entity of type \"{0}\" in state \"{1}\" has the following validation errors:",
                                                                   eve.Entry.Entity.GetType().Name, eve.Entry.State);
                                foreach (var ve in eve.ValidationErrors)
                                {
                                    System.Diagnostics.Debug.WriteLine("- Property: \"{0}\", Error: \"{1}\"",
                                                                       ve.PropertyName, ve.ErrorMessage);
                                }
                            }
                            throw;
                        }
                    }
                }
            }
            return(CreatedAtRoute("DefaultApi", new { id = file.Id }, file));
        }
 public IHttpActionResult AddTestStructure(int AdminId, TestStructure testStructure)
 {
     using (db_OnlineExaminationEntities db = new db_OnlineExaminationEntities())
     {
         if (TestStructureExists(testStructure.Technology, testStructure.Level))
         {
             return(Ok(new TestStructure()));
         }
         if (testStructure.SetProperties(AdminId: AdminId))
         {
             db.TestStructures.Add(testStructure);
             try
             {
                 db.SaveChanges();
             }
             catch (DbUpdateException)
             {
                 if (TestStructureExists(testStructure.Id))
                 {
                     return(Conflict());
                 }
                 else
                 {
                     throw;
                 }
             }
             return(CreatedAtRoute("DefaultApi", new { id = testStructure.Id }, testStructure));
         }
         return(BadRequest());
     }
 }
예제 #5
0
        public IHttpActionResult RegisterUser([FromUri] bool Register, User user)
        {
            if (db.Users.Count(u => u.Email == user.Email) > 0)
            {
                return(Conflict());
            }
            db.Users.Add(user);
            try
            {
                db.SaveChanges();
            }
            catch (DbEntityValidationException e)
            {
                foreach (var eve in e.EntityValidationErrors)
                {
                    System.Diagnostics.Debug.WriteLine("Entity of type \"{0}\" in state \"{1}\" has the following validation errors:",
                                                       eve.Entry.Entity.GetType().Name, eve.Entry.State);
                    foreach (var ve in eve.ValidationErrors)
                    {
                        System.Diagnostics.Debug.WriteLine("- Property: \"{0}\", Error: \"{1}\"",
                                                           ve.PropertyName, ve.ErrorMessage);
                    }
                }
                throw;
            }
            catch (DbUpdateException)
            {
                if (db.Users.Count(u => u.Id == user.Id) > 0)
                {
                    return(Conflict());
                }
                else
                {
                    throw;
                }
            }

            return(Ok(user.Id));
        }
 public IHttpActionResult DeleteTestStructure(int AdminId, int TestStructureId)
 {
     using (db_OnlineExaminationEntities db = new db_OnlineExaminationEntities())
     {
         TestStructure testStructure = db.TestStructures.Find(TestStructureId);
         if (testStructure == null || !testStructure.IsCurrent)
         {
             return(NotFound());
         }
         testStructure.SetProperties(AdminId: AdminId, IsCurrent: false /*, SetNumberOfQuestions: false*/);
         db.SaveChanges();
         return(Ok(testStructure));
     }
 }
 public IHttpActionResult SaveUserAnswer(int TestQuestionId, [FromBody] int?UserAnswer)
 {
     if (UserAnswer < 1 || UserAnswer > 4 || UserAnswer == null)
     {
         UserAnswer = 0;
     }
     db.TestQuestions.Find(TestQuestionId).UserAnswer = UserAnswer;
     db.SaveChanges();
     return(StatusCode(HttpStatusCode.NoContent));
 }