public async Task Save(KVRRQuestion question)
        {
            try
            {
                if (question == null)
                {
                    throw new InvalidParameterException();
                }

                if (question.KVRRAnswers != null && question.KVRRAnswers.Any(i => !string.IsNullOrWhiteSpace(i.Content) && i.Mark != null))
                {
                    var listAnswers = question.KVRRAnswers.Where(i => !string.IsNullOrWhiteSpace(i.Content) && i.Mark != null);
                    if (listAnswers.Count() >= 2)
                    {
                        //update No before insert record
                        var questions = await _unitOfWork.KVRRQuestionRepository.GetAllAsync();

                        var no = (questions.Count() + 1);
                        question.No = no;

                        var addQuestion = _unitOfWork.KVRRQuestionRepository.Add(question);
                        if (addQuestion != null)
                        {
                            _unitOfWork.KVRRAnswerRepository.BulkInsert(question.KVRRAnswers);
                        }
                        await _unitOfWork.SaveChangesAsync();
                    }
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
        public async Task Update(KVRRQuestion question)
        {
            try
            {
                if (question == null)
                {
                    throw new InvalidParameterException();
                }

                var oldQuestion = await GetKVRRQuestionNoAnswerById(question.Id);

                question.ImageDesktop = oldQuestion.ImageDesktop;
                question.ImageMobile  = oldQuestion.ImageMobile;

                _unitOfWork.KVRRQuestionRepository.Update(question);

                var answerOld = await _unitOfWork.KVRRAnswerRepository.FindByAsync(x => x.KVRRQuestion.Id == question.Id);

                if (answerOld != null && answerOld.Any())
                {
                    _unitOfWork.KVRRAnswerRepository.BulkDelete(answerOld.ToList());
                }

                _unitOfWork.KVRRAnswerRepository.BulkUpdate(question.KVRRAnswers);
                await _unitOfWork.SaveChangesAsync();
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
Пример #3
0
        public async Task <IActionResult> NewQuestionDefindKVRR([FromBody] KVRRQuestion data)
        {
            try
            {
                await _kvrrQuestionAnswerService.Save(data);

                return(Json(new { Success = true }));
            }
            catch (System.Exception ex)
            {
                throw ex;
            }
        }
        public async Task UpdateOnlyQuestion(KVRRQuestion question)
        {
            try
            {
                if (question == null)
                {
                    throw new InvalidParameterException();
                }
                _unitOfWork.KVRRQuestionRepository.Update(question);

                await _unitOfWork.SaveChangesAsync();
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
        public async Task UpdateQuestionOrder(KVRRQuestion question)
        {
            try
            {
                if (question == null)
                {
                    throw new InvalidParameterException();
                }

                var oldQuestion = await GetKVRRQuestionNoAnswerById(question.Id);

                question.ImageDesktop = oldQuestion.ImageDesktop;
                question.ImageMobile  = oldQuestion.ImageMobile;

                _unitOfWork.KVRRQuestionRepository.Update(question);
                await _unitOfWork.SaveChangesAsync();
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
        public async Task <bool> ImportListQuestions(IFormFile file)
        {
            string fileExtension = Path.GetExtension(file.FileName);

            var listQuestions = new List <KVRRQuestion>();

            using (var stream = new MemoryStream())
            {
                await file.CopyToAsync(stream);

                using (var package = new ExcelPackage(stream))
                {
                    ExcelWorksheet worksheet = package.Workbook.Worksheets[1];
                    var            rowCount  = worksheet.Dimension.Rows;
                    var            questions = await _unitOfWork.KVRRQuestionRepository.GetAllAsync();

                    var no = questions.Count();
                    if (no == 0)
                    {
                        no = 1;
                    }

                    for (int row = 2; row <= rowCount; row++)
                    {
                        var listAnswers  = new List <KVRRAnswer>();
                        var categoryCell = "";
                        categoryCell = worksheet.Cells[row, 2].Value.ToString().Trim();

                        //update "No" Column in database
                        no += 1;
                        try
                        {
                            KVRRQuestion newQuestion = new KVRRQuestion();
                            newQuestion.Content = worksheet.Cells[row, 3].Value.ToString().Trim();
                            newQuestion.No      = no;
                            newQuestion.KVRRQuestionCategories = (KVRRQuestionCategories)CheckCategoryExisted(categoryCell);

                            for (int col = 4; col <= 22; col += 2)
                            {
                                KVRRAnswer newAnswer = new KVRRAnswer();
                                if (worksheet.Cells[row, col].Value != null)
                                {
                                    newAnswer.Content = worksheet.Cells[row, col].Value.ToString().Trim();
                                }
                                if (worksheet.Cells[row, col + 1].Value != null)
                                {
                                    newAnswer.Mark = int.Parse(worksheet.Cells[row, col + 1].Value.ToString().Trim());
                                }
                                if (newAnswer.Content != null && newAnswer.Mark != null)
                                {
                                    newAnswer.KVRRQuestion = newQuestion;
                                    listAnswers.Add(newAnswer);
                                }
                            }
                            KVRRQuestion oldQuestion = IsQuestionExisted(newQuestion.Content);
                            //check if question is not existed
                            if (oldQuestion != null)
                            {
                                listQuestions.Remove(newQuestion);

                                oldQuestion.KVRRAnswers            = listAnswers;
                                oldQuestion.KVRRQuestionCategories = newQuestion.KVRRQuestionCategories;
                                await Update(oldQuestion);
                            }
                            else
                            {
                                newQuestion.KVRRAnswers = listAnswers;
                                listQuestions.Add(newQuestion);
                            }
                        }
                        catch (Exception ex)
                        {
                            throw ex;
                        }
                    }
                    _unitOfWork.KVRRQuestionRepository.BulkInsert(listQuestions);
                    await _unitOfWork.SaveChangesAsync();
                }
            }
            return(true);
        }