private int SaveAnswer(Question currentQuestion, int questionNumber = 1, int answer = 0)
        {
            string sessionId = Session["Id"].ToString();
            bool   rez       = false;

            if (currentQuestion.CorrectAnswer == answer)
            {
                rez = true;
            }

            Testing t1 = new Testing
            {
                SessionId       = sessionId,
                QuestionId      = currentQuestion.QuestionId,
                UserAnswer      = answer,
                AnswerCorrectly = rez,
            };

            try
            {
                db.Testings.Add(t1);
                db.SaveChanges();
                return(0);
            }
            catch (Exception)
            {
                db.Entry(t1).State = EntityState.Detached;
                return(-1);
            }
        }
Beispiel #2
0
        public ActionResult <long> CreateCategory(CreateCategoryCommand cmd)
        {
            if (cmd == null)
            {
                return(BadRequest("Incorrect command sent"));
            }

            var parentCategory = _db.TestCategories.Find(cmd.ParentCategoryId);

            if (parentCategory == null)
            {
                return(BadRequest($"Parent test category with id={cmd.ParentCategoryId} doesn't exist"));
            }

            var category = new TestCategory()
            {
                Name     = cmd.Name,
                ParentId = cmd.ParentCategoryId
            };

            _db.TestCategories.Add(category);
            _db.SaveChanges();

            return(category.Id);
        }
Beispiel #3
0
        public ActionResult <MultichoiceTaskAnswerDto> AddTaskAnswer([FromBody] AddTaskAnswerCommand cmd)
        {
            StpTask task = _db.Tasks.Find(cmd.TaskId);

            if (task == null)
            {
                return(NotFound($"Answer with id={cmd.TaskId} doesn't exist"));
            }

            MultichoiceTaskAnswer answer = new MultichoiceTaskAnswer()
            {
                Name      = cmd.Name,
                IsCorrect = cmd.IsCorrect,
                TaskId    = cmd.TaskId
            };

            _db.MultichoiceTaskAnswers.Add(answer);
            _db.SaveChanges();

            var res = new MultichoiceTaskAnswerDto()
            {
                Id        = answer.Id,
                Name      = answer.Name,
                IsCorrect = answer.IsCorrect
            };

            return(CreatedAtAction(nameof(AddTaskAnswer), res));
        }
Beispiel #4
0
        public IActionResult Post(string values)
        {
            var newTopic = new Topic();

            JsonConvert.PopulateObject(values, newTopic);

            _db.Topics.Add(newTopic);
            _db.SaveChanges();

            return(Ok());
        }
Beispiel #5
0
        public IActionResult Post(string values)
        {
            var newSubject = new Subject();

            newSubject.SubjectId = new Guid();

            JsonConvert.PopulateObject(values, newSubject);

            _db.Subjects.Add(newSubject);
            _db.SaveChanges();

            return(Ok());
        }
Beispiel #6
0
        public ActionResult IndexPost(Guid id, Question question)
        {
            var _question = _db.Questions.First(a => a.QuestionId == id);

            _question.QuestionString = question.QuestionString;
            _question.QuestionType   = question.QuestionType;
            _question.QuestionLevel  = question.QuestionLevel;
            _question.Point          = question.Point;
            _question.Hint           = question.Hint;
            _question.TopicId        = question.TopicId;

            _db.SaveChanges();
            return(RedirectToAction("Index", "Questions"));
        }
Beispiel #7
0
        public IActionResult Post(string values)
        {
            var newUser = new User
            {
                UserId = new Guid()
            };

            JsonConvert.PopulateObject(values, newUser);

            _db.Users.Add(newUser);
            _db.SaveChanges();

            return(Ok());
        }
        public IActionResult Post(string values)
        {
            var newQuestion = new Question
            {
                QuestionId = new Guid()
            };

            JsonConvert.PopulateObject(values, newQuestion);

            _db.Questions.Add(newQuestion);
            _db.SaveChanges();

            return(Ok());
        }
Beispiel #9
0
        public IActionResult Post(string values)
        {
            var newExam = new Exam
            {
                ExamId = new Guid()
            };

            JsonConvert.PopulateObject(values, newExam);

            _db.Exams.Add(newExam);
            _db.SaveChanges();

            return(Ok());
        }
Beispiel #10
0
        public IActionResult QuestionAnswer(Guid id)
        {
            var _questAnswer = _db.UserExamQuestions.Where(a => a.UserExamQuestionId == id).ToList();

            foreach (var _questAns in _questAnswer)
            {
                _questAnswer.ForEach(e =>
                {
                    if (e.QuestionId == _questAns.QuestionId)
                    {
                        _db.SaveChanges();
                    }
                });
                return(View(_questAns));
            }
            return(View(_questAnswer));
        }
Beispiel #11
0
        public ActionResult <TaskCategoryDto> CreateCategory([FromBody] CreateCategoryCommand cmd)
        {
            // TODO: Validation
            if (cmd.ParentCategoryId != null)
            {
                var res = _db.TaskCategories.Find(cmd.ParentCategoryId.Value);
                if (res == null)
                {
                    return(BadRequest($"ParentCategoryId={cmd.ParentCategoryId} doesn't exist"));
                }
            }

            var maxPos = _db.TaskCategories.Max(x => (int?)x.Position);

            if (maxPos == null)
            {
                maxPos = 0;
            }
            else
            {
                ++maxPos;
            }
            var newCategory = new TaskCategory()
            {
                Name     = cmd.Name,
                ParentId = cmd.ParentCategoryId,
                Position = maxPos.Value
            };

            _db.TaskCategories.Add(newCategory);
            _db.SaveChanges();

            return(CreatedAtAction(
                       nameof(CreateCategory),
                       new TaskCategoryDto
            {
                Id = newCategory.Id,
                Name = newCategory.Name,
                ParentId = newCategory.ParentId,
                Position = newCategory.Position
            }));
        }
 public bool SaveChanges()
 {
     try
     {
         _db.SaveChanges();
         return(true);
     }
     catch (Exception)
     {
         return(false);
     }
 }
        public ActionResult Create(Test t)
        {
            if (!CheckAuthorizaton())
            {
                return(RedirectToAction("Login", "Account"));
            }


            if (ModelState.IsValid)
            {
                try
                {
                    db.Entry(t).State = EntityState.Added;
                    db.SaveChanges();
                    return(RedirectToAction("Index", "Administration"));
                }
                catch (Exception xcp)
                {
                    db.Entry(t).State = EntityState.Detached;
                    ViewBag.error     = xcp.Message;
                    return(View("Error"));
                }
            }

            return(View(t));
        }
        public ActionResult <TestSectionDto> AddTestSection([FromBody] CreateTestSectionCommand cmd)
        {
            // TODO: Check presences the section in specified test
            TestSection testSection = new TestSection()
            {
                Name     = cmd.Name,
                Position = cmd.Position,
                TestId   = cmd.TestId
            };

            _db.TestSections.Add(testSection);
            _db.SaveChanges();

            var result = new TestSectionDto()
            {
                Id       = testSection.Id,
                Name     = testSection.Name,
                Position = testSection.Position,
            };

            return(Created(nameof(AddTestSection), result));
        }
Beispiel #15
0
        public IActionResult AddExams(string ExamName)
        {
            if (ExamName != null)
            {
                var         _user     = _db.Users.First(a => a.UserId == CurrentUserId);
                List <Exam> examsList = JsonConvert.DeserializeObject <List <Exam> >(ExamName);

                _user.AddExams(examsList);

                _db.SaveChanges();
            }

            return(RedirectToAction("Index", "Exams"));
        }
Beispiel #16
0
        public IActionResult AddQuestions(Guid id, string questionsString)
        {
            if (questionsString != null)
            {
                var             _exam         = _db.Exams.First(a => a.ExamId == id);
                List <Question> questionsList = JsonConvert.DeserializeObject <List <Question> >(questionsString);

                _exam.AddQuestionns(questionsList);

                _db.SaveChanges();
            }

            return(RedirectToAction("Index", "Exam", new { id = id }));
        }
        public ActionResult <SkillDto> CreateSkill([FromBody] string name)
        {
            if (string.IsNullOrWhiteSpace(name))
            {
                return(BadRequest($"Skill's name shouldn't be empty!"));
            }

            bool nameExists = _db.Skills.Any(x => x.Name.ToUpper() == name.ToUpper());

            if (nameExists)
            {
                return(BadRequest($"Skill with name {name} already exists!"));
            }

            var newSkill = new Skill()
            {
                Name = name
            };

            _db.Skills.Add(newSkill);
            _db.SaveChanges();

            return(CreatedAtAction(nameof(CreateSkill), newSkill));
        }
        public bool Create(Products model)
        {
            bool created = false;

            try
            {
                _context.Entry(model).State = EntityState.Added;
                _context.SaveChanges();
                created = true;
            }
            catch (Exception)
            {
                created = false;
            }
            return(created);
        }
Beispiel #19
0
        public IActionResult ProcessRegister(LoginViewModel model)
        {
            if (ModelState.IsValid)
            {
                if (model.CheckPassword)
                {
                    var newUser = new User()
                    {
                        UserId   = new Guid(),
                        Email    = model.Email,
                        UserName = model.UserName,
                        Password = model.Password
                    };
                    _db.Users.Add(newUser);
                    _db.SaveChanges();
                }

                return(RedirectToAction("SignIn"));
            }

            return(RedirectToAction("Register"));
        }
Beispiel #20
0
        public ActionResult <TaskDto> CreateTask([FromBody] CreateTaskCommand cmd)
        {
            var category = _db.TaskCategories.Find(cmd.TaskCategoryId);

            if (category == null)
            {
                return(BadRequest($"Task category with Id={cmd.TaskCategoryId} not found"));
            }

            var skillIds       = cmd.Skills.Select(y => y.Id).ToList();
            var existingSkills = _db.Skills.Where(x => skillIds.Contains(x.Id)).ToList();
            var maxPos         = _db.Tasks.Where(x => x.CategoryId == category.Id).Max(x => (int?)x.Position);

            if (maxPos == null)
            {
                maxPos = 0;
            }
            else
            {
                ++maxPos;
            }
            using var trn = _db.Database.BeginTransaction();

            StpTask newTask = new StpTask()
            {
                CategoryId      = category.Id,
                Name            = cmd.Name,
                Points          = cmd.Points,
                DurationMinutes = cmd.DurationMinutes,
                Type            = cmd.Type,
                Position        = maxPos.Value,
                Complexity      = cmd.Complexity
            };

            _db.Tasks.Add(newTask);

            newTask.TaskAndSkills = new List <TaskAndSkill>();

            foreach (var skill in cmd.Skills)
            {
                if (skill.State != SkillState.Added && skill.State != SkillState.New)
                {
                    return(BadRequest($"Unexpected SkillState: {skill.State}"));
                }

                Skill skillToAdd = null;
                if (skill.State == SkillState.New)
                {
                    skillToAdd = new Skill()
                    {
                        Name = skill.Name
                    };
                    _db.Skills.Add(skillToAdd);
                    _db.SaveChanges();
                }
                else
                {
                    var dbSkill = existingSkills.Find(x => x.Id == skill.Id);
                    if (dbSkill == null)
                    {
                        return(BadRequest($"Skill with Id={skill.Id} not found"));
                    }
                    skillToAdd = dbSkill;
                }


                TaskAndSkill link = new TaskAndSkill()
                {
                    SkillId = skillToAdd.Id,
                    //TaskId = newTask.Id
                };
                newTask.TaskAndSkills.Add(link);

                //_db.TaskAndSkills.Add(link);
            }

            _db.SaveChanges();
            trn.Commit();

            var result = new TaskDto()
            {
                TaskSummary = new TaskSummaryDto()
                {
                    Id              = newTask.Id,
                    Complexity      = newTask.Complexity,
                    DurationMinutes = newTask.DurationMinutes,
                    Name            = newTask.Name,
                    Points          = newTask.Points,
                    Position        = newTask.Position,
                    Type            = newTask.Type,
                    Skills          = newTask.TaskAndSkills.Select(x => new SkillDto()
                    {
                        Id = x.Skill.Id, Name = x.Skill.Name
                    }).ToList(),
                },
                MultichoiceTaskInfo = new MultichoiceTaskInfoDto()
                {
                    Question = "",
                    Answers  = new List <MultichoiceTaskAnswerDto>()
                }
            };

            return(Created(nameof(CreateTask), result));
        }