Beispiel #1
0
        public JsonResult CreateAnswerUser([FromBody] AnswerUserModel answerUserModel)
        {
            string functionName = System.Reflection.MethodBase.GetCurrentMethod().Name;

            try
            {
                //Check value enter from the form
                if (answerUserModel == null)
                {
                    Log4Net.log.Error(className + "." + functionName + " - " + Log4Net.AddErrorLog(Constants.notInformationQuestion));
                    return(Json(MessageResult.GetMessage(MessageType.NOT_INFORMATION_QUESTION)));
                }

                if (!_accountRepository.AccountExists(answerUserModel.accountId))
                {
                    Log4Net.log.Error(className + "." + functionName + " - " + Log4Net.AddErrorLog(Constants.accountNotFound));
                    return(Json(MessageResult.GetMessage(MessageType.ACCOUNT_NOT_FOUND)));
                }

                if (!_examRepository.ExamExist(answerUserModel.examId))
                {
                    Log4Net.log.Error(className + "." + functionName + " - " + Log4Net.AddErrorLog(Constants.examNotFound));
                    return(Json(MessageResult.GetMessage(MessageType.EXAM_NOT_FOUND)));
                }

                if (!ModelState.IsValid)
                {
                    Log4Net.log.Error(className + "." + functionName + " - " + Log4Net.AddErrorLog(Constants.notFound));
                    return(Json(MessageResult.GetMessage(MessageType.NOT_FOUND)));
                }
                //get list answer user from form
                List <AnswerUserDto> answersFromForm = answerUserModel.listAnswerUser;
                //get all list question from the examId
                List <ExamQuestionEntity> examQuestionEntity = _examQuestionRepository.getListQuestions(answerUserModel.examId);
                //get all answer user of this account
                List <AnswerUserEntity> answerUsersFromDB = _answerUserRepository.GetAnswerUserEntities(answerUserModel.accountId);

                //if this account haven't ever do the exam. newAccountAnswer will store it
                List <AnswerUserDto> newAccountAnswer = new List <AnswerUserDto>();
                //list answer of this account from DB
                List <AnswerUserDto> oldAccountUser = new List <AnswerUserDto>();


                // divide between old and new answer
                foreach (var answer in answersFromForm)
                {
                    foreach (var item in answerUsersFromDB)
                    {
                        if (answer.questionId == item.QuestionId)
                        {
                            oldAccountUser.Add(answer);
                        }
                    }
                }
                newAccountAnswer = answersFromForm;

                if (oldAccountUser.Count() > 0)
                {
                    // remove old answer
                    for (int i = 0; i < answersFromForm.Count; i++)
                    {
                        foreach (var item in oldAccountUser)
                        {
                            if (item == answersFromForm[i])
                            {
                                answersFromForm.Remove(answersFromForm[i]);
                            }
                        }
                    }

                    foreach (var examQuestion in examQuestionEntity)
                    {
                        foreach (var answer in newAccountAnswer)
                        {
                            if (examQuestion.QuestionId == answer.questionId)
                            {
                                //Map data enter from the form to question entity
                                var answerUser = Mapper.Map <PPT.Database.Entities.AnswerUserEntity>(answer);
                                answerUser.AccountId = answerUserModel.accountId;
                                //This is query insert question
                                _answerUserRepository.CreateAnswerUser(answerUser);
                            }
                        }
                    }

                    foreach (var answer in oldAccountUser)
                    {
                        foreach (var answerU in answerUsersFromDB)
                        {
                            if (answerU.QuestionId == answer.questionId)
                            {
                                answerU.AnswerKey = answer.answerKey;
                            }
                        }
                    }
                }
                //this function shows that user haven't ever done this exam. This is the first time they do it.

                else
                {
                    //1 -> 14
                    foreach (var examQuestion in examQuestionEntity)
                    {
                        //1 -> 2
                        foreach (var answer in newAccountAnswer)
                        {
                            if (examQuestion.QuestionId == answer.questionId)
                            {
                                //Map data enter from the form to question entity
                                var answerUser = Mapper.Map <PPT.Database.Entities.AnswerUserEntity>(answer);
                                answerUser.AccountId = answerUserModel.accountId;
                                //This is query insert question
                                _answerUserRepository.CreateAnswerUser(answerUser);
                                AccountExamEntity accountExamEntity = _accountExamRepository.GetByAccountIdAndExamId(answerUser.AccountId, examQuestion.ExamId);

                                accountExamEntity.IsStatus = "Continue Do Exam";
                                _accountExamRepository.Save();
                            }
                        }
                    }
                }
                AccountExamEntity finishExam = _accountExamRepository.GetByAccountIdAndExamId(answerUserModel.accountId, answerUserModel.examId);
                if (answerUserModel.status != null)
                {
                    finishExam.IsStatus = "Finish";
                    _accountExamRepository.Save();
                }

                //get account by Id
                AccountEntity account = _accountRepository.GetAccountById(answerUserModel.accountId);
                //get exam by Id
                ExamEntity exam    = _examRepository.GetExamById(answerUserModel.examId);
                int        groupId = exam.GroupId;
                //get group by Id
                GroupEntity group = _groupRepository.GetGroupById(groupId);

                if (_historyRepository.CheckAccount(account.AccountId, exam.ExamId))
                {
                    HistoryEntity history = new HistoryEntity();
                    history.AccountId = account.AccountId;
                    history.ExamId    = exam.ExamId;
                    history.Group     = group;
                    _historyRepository.CreateHistory(history);
                    _historyRepository.Save();
                }

                if (!_answerUserRepository.Save())
                {
                    Log4Net.log.Error(className + "." + functionName + " - " + Log4Net.AddErrorLog(Constants.badRequest));
                    return(Json(MessageResult.GetMessage(MessageType.BAD_REQUEST)));
                }

                Log4Net.log.Error(className + "." + functionName + " - " + Log4Net.AddErrorLog(Constants.createdAnswerUser));
                return(Json(MessageResult.GetMessage(MessageType.CREATED_ANSWER_USER)));
            }
            catch (Exception ex)
            {
                Log4Net.log.Error(className + "." + functionName + " - " + Log4Net.AddErrorLog(ex.Message));
                return(Json(MessageResult.ShowServerError(ex.Message)));
            }
        }