public string EFdeleteQuizQuestion(FarmworkersWebAPI.Entities.QuizQuestion _deletedQuizQuestion) { _context.Configuration.ProxyCreationEnabled = false; try { _context.QuizQuestions.Attach(_deletedQuizQuestion); _context.QuizQuestions.Remove(_deletedQuizQuestion); _context.SaveChanges(); return("Question Deleted Successfully"); } catch (Exception ex) { return(InternalServerError(ex).ToString()); } }
public string EFinsertQuizQuestion(FarmworkersWebAPI.Entities.QuizQuestion _newQuizQuestion) { _context.Configuration.ProxyCreationEnabled = false; FarmworkersWebAPI.Entities.QuizQuestion _quizQuestionContentData = new FarmworkersWebAPI.Entities.QuizQuestion(); try { _quizQuestionContentData = _context.QuizQuestions.Add(_newQuizQuestion); _context.SaveChanges(); return("Question Inserted Successfully"); } catch (Exception ex) { return(InternalServerError(ex).ToString()); } }
public string deleteOldLeftOverQuestions(Entities.Quiz _updatedQuiz) { string _deletionResult = "Questions Deleted Successfully"; try { List <FarmworkersWebAPI.Entities.QuizQuestion> _oldQuestions = _context.QuizQuestions. Where(q => q.QuizID.Equals(_updatedQuiz.QuizID) && q.QuizVersion.Equals(_updatedQuiz.QuizVersion) && q.IsActive.Equals("1")).ToList(); if (_oldQuestions.Count > _updatedQuiz.QuizQuestions.Count && _updatedQuiz.QuizQuestions.Count != 0) { for (int i = _updatedQuiz.QuizQuestions.Count + 1; i <= _oldQuestions.Count; i++) { Entities.QuizQuestion _questionToDelete = new Entities.QuizQuestion(); FarmworkersWebAPI.Entities.QuizQuestion original = _context.QuizQuestions. Where(q => q.QuizID.Equals(_updatedQuiz.QuizID) && q.QuizVersion.Equals(_updatedQuiz.QuizVersion) && q.QuestionID.Equals(i)).FirstOrDefault(); _questionToDelete.QuizID = _updatedQuiz.QuizID; _questionToDelete.QuizVersion = _updatedQuiz.QuizVersion; _questionToDelete.QuestionID = i; _questionToDelete.Question = original.Question; _questionToDelete.IsActive = "0"; _questionToDelete.DateModified = original.DateModified; _context.Entry(original).CurrentValues.SetValues(_questionToDelete); _context.SaveChanges(); } } } catch (Exception ex) { return(InternalServerError(ex).ToString()); } return(_deletionResult); }
public string EFupdateQuizQuestion(FarmworkersWebAPI.Entities.QuizQuestion _updatedQuizQuestion) { if (_updatedQuizQuestion.QuestionID < 0) { return("Quiz Question ID can not be negative"); } if (_updatedQuizQuestion.QuestionID == 0) { return("Quiz Question ID Not Specified"); } _context.Configuration.ProxyCreationEnabled = false; try { FarmworkersWebAPI.Entities.QuizQuestion original = _context.QuizQuestions. Where(q => q.QuizID.Equals(_updatedQuizQuestion.QuizID) && q.QuizVersion.Equals(_updatedQuizQuestion.QuizVersion) && q.QuestionID.Equals(_updatedQuizQuestion.QuestionID)).FirstOrDefault(); if (original != null) { //Update Question Base Information _context.Entry(original).CurrentValues.SetValues(_updatedQuizQuestion); _context.SaveChanges(); //Determine if there are less answers in this quiz question update than in the previous version of the question //If there are less answers in the new update, the ones left over from the previous version will be deleted string _deletionResult = deleteOldLeftOverAnswers(_updatedQuizQuestion); if (_deletionResult != "Answers Deleted Successfully") { return(_deletionResult); } //********* foreach (FarmworkersWebAPI.Entities.QuizAnswer _answer in _updatedQuizQuestion.QuizAnswers) { string _updateResult = EFupdateQuizAnswer(_answer); if (_updateResult != "Answer Updated Successfully") { return(_updateResult); } } return("Question Updated Successfully"); } else //If during the update process, there is a new question that wasn't there before. { //Insert New Question string _insertQuestionResult = EFinsertQuizQuestion(_updatedQuizQuestion); if (_insertQuestionResult != "Question Inserted Successfully") { return(_insertQuestionResult); } //Insert All Answers for that Question foreach (FarmworkersWebAPI.Entities.QuizAnswer _answer in _updatedQuizQuestion.QuizAnswers) { string _insertAnswerResult = EFinsertQuizAnswer(_answer); if (_insertAnswerResult != "Answer Inserted Successfully") { return(_insertAnswerResult); } } return("Question Updated Successfully"); } } catch (Exception ex) { return(InternalServerError(ex).ToString()); } }