Exemple #1
0
        public async Task <OperationDetails> UpdateAsync(AnswerDTO item, string userId)
        {
            try
            {
                // Checking for: Does the current user has a role - "admin"?
                bool isAdmin = BLLRepository.IsAdmin(Database, userId);
                // Checking for: Does the current user have permission for updating answers from the test creator?
                int courseId = (await Database.Question.GetAsync(item.QuestionId)).Topic.CourseId;
                IEnumerable <CourseAssignment> assignments = Database.CourseAssignment.Find(obj => obj.CourseId == courseId);
                bool isCourseAssigned = (from assign in assignments
                                         where assign.UserProfileId == userId
                                         select assign).Count() > 0;
                Course course = await Database.Course.GetAsync(courseId);

                if (course.UserProfileId != userId && !isAdmin && !isCourseAssigned)
                {
                    return(new OperationDetails(false, "You can't update this answer. This course has been created by other user so apply to the course creator for the permission.", "Answer"));
                }
                // Get the updated answer from DB by AnswerId.
                Answer answer = await Database.Answer.GetAsync(item.AnswerId);

                if (answer != null)
                {
                    // Check the updated answer.
                    string answerType = (await Database.Question.GetAsync(item.QuestionId)).AnswerType.AnswerTypeDescription;
                    var    answers    = Database.Answer.Find(obj => obj.QuestionId == item.QuestionId);
                    int    countOfExistedProperAnswersInQuestion = (from row in answers
                                                                    where row.IsProper == true
                                                                    select row).Count();
                    if (answer.IsProper)
                    {
                        countOfExistedProperAnswersInQuestion -= countOfExistedProperAnswersInQuestion;
                    }
                    if (!BLLRepository.CheckCountOfProperAnswers(answerType, item.IsProper,
                                                                 countOfExistedProperAnswersInQuestion,
                                                                 out string result))
                    {
                        return(new OperationDetails(false, result, "Answer"));
                    }
                    // Update the answer.
                    answer.AnswerText           = item.AnswerText?.Trim();
                    answer.IsProper             = item.IsProper;
                    answer.LastModifiedDateTime = DateTime.Now;
                    Database.Answer.Update(answer);
                    await Database.SaveAsync();

                    return(new OperationDetails(true, "Answer updating completed successfully.", "Answer"));
                }
                return(new OperationDetails(false, "Answer with this Id doesn't exists.", "Answer"));
            }
            catch (Exception ex)
            {
                return(new OperationDetails(false, ex.Message, "Answer"));
            }
        }