Пример #1
0
        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)));
            }
        }