Exemplo n.º 1
0
        //public Task CheckLastTimeStatusAsync()
        //{
        //    throw new NotImplementedException();
        //}


        public LogicResult <EssayAnswer> CreateEssayAnswer(EssayAnswer essayExercise)
        {
            try
            {
                if (essayExercise is null)
                {
                    return(new LogicResult <EssayAnswer>
                    {
                        IsSuccess = false,
                        Errors = new List <string> {
                            "Annotation can not be empty."
                        }
                    });
                }
                _unitOfWork.EssayAnswerRepository.AddAsync(essayExercise);
                _unitOfWork.SaveChangesAsync();
                return(new LogicResult <EssayAnswer>
                {
                    IsSuccess = true,
                    Data = essayExercise
                });
            }
            catch (Exception ex)
            {
                return(new LogicResult <EssayAnswer>
                {
                    IsSuccess = false,
                    Errors = new List <string>
                    {
                        "Unable to save changes.",
                        ex.Message
                    }
                });
            }
        }
Exemplo n.º 2
0
        public async Task <EssayAnswer> UpdateAsync(EssayAnswer entity)
        {
            await _unitOfWork.EssayAnswerRepository.UpdateAsync(entity);

            await _unitOfWork.SaveChangesAsync();

            return(entity);
        }
Exemplo n.º 3
0
        public async Task <IActionResult> CreateEssayAnswer([FromBody] EssayAnswerModel model)
        {
            var essayAnswer = new EssayAnswer();

            try
            {
                if (!ModelState.IsValid)
                {
                    ValidModel();
                }

                //check if this question has been done yet
                var existingEssayAnswer = await _service.EssayAnswerService.CheckExistingBasedOnAnswerIdAndExerciseId(model.AnswerId, model.EssayExerciseId);

                if (existingEssayAnswer == true)
                {
                    return(ErrorResult("You has already done this question."));
                }

                //check if multiple choices answer is existing
                var essayExercise = await _service.EssayExerciseService.GetByIdAsync(model.EssayExerciseId);

                if (essayExercise == null)
                {
                    return(ErrorResult($"The Exercise with Id :{model.EssayExerciseId} does not exist."));
                }

                essayAnswer = new EssayAnswer()
                {
                    AnswerId        = model.AnswerId,
                    EssayExerciseId = model.EssayExerciseId,
                    Result          = model.Result
                };
                await _service.EssayAnswerService.AddAsync(essayAnswer);
            }
            catch (Exception e)
            {
                return(ErrorResult(e.Message));
            }
            return(SuccessResult(essayAnswer, "Created Essay Answer successfully."));
        }
Exemplo n.º 4
0
        public async Task <IActionResult> CreateAnswerContainer([FromBody] AnswerContainerModel model)
        {
            var newAnswer = new Answer();

            try
            {
                if (!ModelState.IsValid)
                {
                    ValidModel();
                }
                //check if isExisting ->studentId match TestId
                var existingAnswer = await _service.AnswerService.CheckExistingBasedOnTestId(model.StudentId, model.TestId);

                if (existingAnswer == true)
                {
                    return(ErrorResult("You has already done this test."));
                }

                int score = 0;
                //check answer is right
                foreach (var multipleChoicesAnswer in model.MultipleChoicesAnswerModels)
                {
                    //get right result
                    var result = await _service.MultipleChoicesExerciseService.GetByIdAsync(multipleChoicesAnswer.MultipleChoicesExerciseId);

                    //compare result
                    if (multipleChoicesAnswer.Result.ToString() == result.RightResult.ToString())
                    {
                        multipleChoicesAnswer.IsBingo = true;
                        //update result
                        score++;
                    }
                    else
                    {
                        multipleChoicesAnswer.IsBingo = false;
                    }
                }

                //else -> add answer
                newAnswer = new Answer()
                {
                    StudentId = model.StudentId,
                    TestId    = model.TestId,
                    Score     = model.Score
                };
                await _service.AnswerService.AddAsync(newAnswer);

                //add new multiple answer
                foreach (var multipleChoicesAnswer in model.MultipleChoicesAnswerModels)
                {
                    var newMultitpleChoicesAnswer = new MultipleChoicesAnswer()
                    {
                        AnswerId = multipleChoicesAnswer.AnswerId,
                        IsBingo  = multipleChoicesAnswer.IsBingo,
                        Result   = multipleChoicesAnswer.Result,
                        MultipleChoicesExerciseId = multipleChoicesAnswer.MultipleChoicesExerciseId
                    };
                    await _service.MultipleChoicesAnswerService.AddAsync(newMultitpleChoicesAnswer);

                    //add log
                    var multipleChoicesLog = new Log()
                    {
                        Status = true,
                        Action = "Add Multiple Choices Answer id : " + newMultitpleChoicesAnswer.Id.ToString() + " complete at " + newMultitpleChoicesAnswer.CreatedOnUtc.ToString()
                    };
                    await _service.LogService.AddAsync(multipleChoicesLog);
                }

                //add new essay answer
                foreach (var essayAnswer in model.EssayAnswerModels)
                {
                    var newEssayAnswer = new EssayAnswer()
                    {
                        AnswerId        = essayAnswer.AnswerId,
                        IsBingo         = false,
                        Result          = essayAnswer.Result,
                        EssayExerciseId = essayAnswer.EssayExerciseId
                    };
                    await _service.EssayAnswerService.AddAsync(newEssayAnswer);

                    //add log
                    var essayLog = new Log()
                    {
                        Status = true,
                        Action = "Add Essay Answer id : " + newEssayAnswer.Id + " complete at " + newEssayAnswer.CreatedOnUtc
                    };
                    await _service.LogService.AddAsync(essayLog);
                }

                var newLog = new Log()
                {
                    Status = true,
                    Action = "Add Answer id : " + newAnswer.Id + " complete at " + newAnswer.CreatedOnUtc
                };
                await _service.LogService.AddAsync(newLog);
            }
            catch (Exception e)
            {
                return(ErrorResult(e.Message));
            }
            return(SuccessResult(newAnswer, "Created Answer successfully."));
        }