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))); } }
public JsonResult CreateQuestion([FromBody] List <QuestionDto> questions) { string functionName = System.Reflection.MethodBase.GetCurrentMethod().Name; try { int examId = 0; if (questions == null) { Log4Net.log.Error(className + "." + functionName + " - " + Log4Net.AddErrorLog(Constants.notInformationQuestion)); return(Json(MessageResult.GetMessage(MessageType.NOT_INFORMATION_QUESTION))); } foreach (var question in questions) { if (!_examRepository.ExamExist(question.examId)) { Log4Net.log.Error(className + "." + functionName + " - " + Log4Net.AddErrorLog(Constants.examNotFound)); return(Json(MessageResult.GetMessage(MessageType.EXAM_NOT_FOUND))); } examId = question.examId; } if (!ModelState.IsValid) { Log4Net.log.Error(className + "." + functionName + " - " + Log4Net.AddErrorLog(Constants.notFound)); return(Json(MessageResult.GetMessage(MessageType.NOT_FOUND))); } foreach (var question in questions) { //Map data enter from the form to question entity var partExam = Mapper.Map <PPT.Database.Entities.QuestionEntity>(question); //This is query insert question _questionRepository.CreatePart(partExam, question.examId); } List <AccountExamEntity> accountExams = _accountExamRepository.GetListAccountExamByExamId(examId); foreach (var item in accountExams) { item.IsStatus = "Do Exam"; _accountExamRepository.Save(); } if (!_questionRepository.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.createdQuestion)); return(Json(MessageResult.GetMessage(MessageType.CREATED_QUESTION))); } catch (Exception ex) { Log4Net.log.Error(className + "." + functionName + " - " + Log4Net.AddErrorLog(ex.Message)); return(Json(MessageResult.ShowServerError(ex.Message))); } }
public JsonResult CreateExam([FromBody] ExamForCreationDto exam) { string functionName = System.Reflection.MethodBase.GetCurrentMethod().Name; try { //Check value enter from the form if (exam == null) { Log4Net.log.Error(className + "." + functionName + " - " + Log4Net.AddErrorLog(Constants.notInformationExam)); return(Json(MessageResult.GetMessage(MessageType.NOT_INFORMATION_EXAM))); } if (!ModelState.IsValid) { Log4Net.log.Error(className + "." + functionName + " - " + Log4Net.AddErrorLog(Constants.notFound)); return(Json(MessageResult.GetMessage(MessageType.NOT_FOUND))); } GroupOwnerEntity groupOwner = _groupOwnerRepository.GetGroupOwnerByGroupId(exam.GroupId); //Map data enter from the form to exam entity var finalExam = Mapper.Map <PPT.Database.Entities.ExamEntity>(exam); //This is query insert exam _examRepository.CreateExam(finalExam); if (!_examRepository.Save()) { Log4Net.log.Error(className + "." + functionName + " - " + Log4Net.AddErrorLog(Constants.badRequest)); return(Json(MessageResult.GetMessage(MessageType.BAD_REQUEST))); } GroupEntity groupEntity = _groupRepository.GetGroupByExam(finalExam); List <GroupMemberEntity> listGroupMembers = _groupMemberRepository.GetGroupMemberByGroupId(groupEntity.GroupId); if (listGroupMembers.Count() > 0) { AccountExamEntity accountExamEntity1 = new AccountExamEntity(); accountExamEntity1.Exam = finalExam; accountExamEntity1.AccountId = groupOwner.AccountId; accountExamEntity1.ExamId = finalExam.ExamId; accountExamEntity1.IsStatus = "Empty"; _accountExamRepository.CreateAccountExam(accountExamEntity1); _accountExamRepository.Save(); foreach (var item in listGroupMembers) { AccountExamEntity accountExamEntity = new AccountExamEntity(); accountExamEntity.Exam = finalExam; accountExamEntity.AccountId = item.AccountId; accountExamEntity.Account = item.Account; accountExamEntity.ExamId = finalExam.ExamId; accountExamEntity.IsStatus = "Empty"; _accountExamRepository.CreateAccountExam(accountExamEntity); _accountExamRepository.Save(); } } else { AccountExamEntity accountExamEntity = new AccountExamEntity(); accountExamEntity.Exam = finalExam; accountExamEntity.AccountId = groupOwner.AccountId; accountExamEntity.ExamId = finalExam.ExamId; accountExamEntity.IsStatus = "Empty"; _accountExamRepository.CreateAccountExam(accountExamEntity); _accountExamRepository.Save(); } Log4Net.log.Error(className + "." + functionName + " - " + Log4Net.AddErrorLog(Constants.createdExam)); return(Json(MessageResult.GetMessage(MessageType.CREATED_EXAM))); } catch (Exception ex) { Log4Net.log.Error(className + "." + functionName + " - " + Log4Net.AddErrorLog(ex.Message)); return(Json(MessageResult.ShowServerError(ex.Message))); } }