public async Task<int> UpdateQuestionAsync(IQuestion question, IAnswerChoice[] choices) { if (choices.Sum(c => c.Points) == 0) { throw new ArgumentException("Points"); } var existingQuestion = await questionRepository.GetQuestionWithChoicesAndPicuresAsync(question.Id); var collectionForInsert = choices.Where(choice => existingQuestion.AnswerChoices.SingleOrDefault(c => c.Id == choice.Id) == null).ToArray(); var collectionForUpdate = choices.Where(choice => existingQuestion.AnswerChoices.SingleOrDefault(c => c.Id == choice.Id) != null).ToArray(); var collectionForDelete = existingQuestion.AnswerChoices.Where(choice => choices.SingleOrDefault(c => c.Id == choice.Id) == null).Select(c => c.Id).ToArray(); var unitOfWork = questionRepository.GetUnitOfWork(); await this.AddChoiceRangeForDeleteAsync(unitOfWork, collectionForDelete); await this.AddChoiceRangeForUpdateAsync(unitOfWork, collectionForUpdate); await this.AddChoiceRangeForInsertAsync(unitOfWork, question.Id, collectionForInsert); await questionRepository.AddForUpdateAsync(unitOfWork, question); return await unitOfWork.CommitAsync(); }
public async Task<int> InsertAsync(IQuestion question, IAnswerChoice[] answerChoices, string examId) { var questionId = Guid.NewGuid().ToString(); question.Id = questionId; if (String.IsNullOrEmpty(examId)) { throw new ArgumentException("Exam"); } var correctAnswers = 0; foreach (var choice in answerChoices) { if (choice.Points > 0) { correctAnswers++; } } if (correctAnswers == 0) { throw new ArgumentException("AnswerChoice"); } var examQuestion = new ExamQuestion(); examQuestion.ExamId = examId; examQuestion.QuestionId = questionId; examQuestion.Id = Guid.NewGuid().ToString(); examQuestion.Number = await examRepository.GetNumberOfQuestionsAsync(examQuestion.ExamId) + 1; var unitOfWork = questionRepository.GetUnitOfWork(); await questionRepository.AddForInsertAsync(unitOfWork, question); await examRepository.AddExamQuestionForInsert(unitOfWork, examQuestion); foreach(var choice in answerChoices) { choice.QuestionId = questionId; choice.Id = Guid.NewGuid().ToString(); await answerChoiceRepository.AddForInsertAsync(unitOfWork, choice); } return await unitOfWork.CommitAsync(); }
public async Task<int> AddChoiceRangeForInsertAsync(IUnitOfWork unitOfWork, string questionId, IAnswerChoice[] choices) { var result = 0; foreach (var choice in choices) { choice.QuestionId = questionId; choice.Id = Guid.NewGuid().ToString(); result += await answerChoiceRepository.AddForInsertAsync(unitOfWork, choice); } return result; }
public async Task<int> AddChoiceRangeForUpdateAsync(IUnitOfWork unitOfWork, IAnswerChoice[] choices) { var result = 0; foreach (var choice in choices) { result += await answerChoiceRepository.AddForUpdateAsync(unitOfWork, choice); } return result; }
public Task<int> AddForUpdateAsync(IUnitOfWork unitOfWork, IAnswerChoice answerChoice) { return unitOfWork.AddForUpdateAsync<AnswerChoiceEntity>(Mapper.Map<AnswerChoiceEntity>(answerChoice)); }