public IHttpActionResult InsertQuiz(ViewModels.QuizForm _quizInformation) { Quiz _quizReceived = new Quiz(); try { //The incoming format coming from the Front-End is changed so it is adjusted to the Format in the data base. _quizReceived = handleIncomingQuizFormat(_quizInformation); } catch (Exception ex) { return(InternalServerError(ex)); } string _insertionStatus = _quizReceived.insertQuiz(_quizReceived); if (_insertionStatus != "Quiz Question Inserted Successfully") { return(Ok(GetQuizByIDAndVersionController(_quizReceived.QuizID.ToString(), _quizReceived.QuizVersion.ToString()))); } else { return(Ok(_insertionStatus)); } }
public IHttpActionResult EFInsertQuiz(ViewModels.QuizForm _quizInformation) { _context.Configuration.ProxyCreationEnabled = false; Entities.Quiz _quizReceived = new Entities.Quiz(); Entities.Quiz _quiz = new Entities.Quiz(); try { //The incoming format coming from the Front-End is changed so it is adjusted to the Format in the data base. _quizReceived = EFhandleIncomingQuizFormat(_quizInformation); _quiz = _context.Quizs.Add(_quizReceived); _context.SaveChanges(); if (_quizInformation.RelatedSource != null) { Entities.QuizEducationalContent _educationalContentLinkToQuiz = new Entities.QuizEducationalContent(); _educationalContentLinkToQuiz.QuizID = _quiz.QuizID; _educationalContentLinkToQuiz.QuizVersion = _quiz.QuizVersion; _educationalContentLinkToQuiz.EducationalContentID = int.Parse(_quizInformation.RelatedSource); int _educationalContentID = int.Parse(_quizInformation.RelatedSource); _context.QuizEducationalContents.Add(_educationalContentLinkToQuiz); _context.SaveChanges(); _quiz.EducationalContents = _context.EducationalContents.Where(ec => ec.IDEducationalContent.Equals(_educationalContentID)).ToList(); } return(Ok(_quiz)); } catch (Exception ex) { return(InternalServerError(ex)); } }
public IHttpActionResult EFupdateQuiz(ViewModels.QuizForm _quizInformation) { //The incoming format coming from the Front-End is changed so it is adjusted to the Format in the data base. Entities.Quiz _updatedQuiz = EFhandleIncomingQuizFormat(_quizInformation); if (_updatedQuiz.QuizID < 0) { return(Ok("Quiz ID can not be negative")); } if (_updatedQuiz.QuizID == 0) { return(Ok("Quiz ID Not Specified")); } _context.Configuration.ProxyCreationEnabled = false; try { FarmworkersWebAPI.Entities.Quiz original = _context.Quizs. Where(q => q.QuizID.Equals(_updatedQuiz.QuizID) && q.QuizVersion.Equals(_updatedQuiz.QuizVersion)).FirstOrDefault(); //***NOT Entity Framework Quiz _updated = new Quiz(); ///*********************** if (original != null) { //Update Quiz Base Information _context.Entry(original).CurrentValues.SetValues(_updatedQuiz); _context.SaveChanges(); //Determine if there are less questions in this quiz update than in the previous version //If there are less questions in the new update, the ones left over from the previous version //will be deleted string _deletionResult = deleteOldLeftOverQuestions(_updatedQuiz); if (_deletionResult != "Questions Deleted Successfully") { return(Ok(_deletionResult)); } foreach (FarmworkersWebAPI.Entities.QuizQuestion _question in _updatedQuiz.QuizQuestions) { string _updateResult = EFupdateQuizQuestion(_question); if (_updateResult != "Question Updated Successfully") { return(Ok(_updateResult)); } } _context.SaveChanges(); if (_quizInformation.RelatedSource != null) { Entities.QuizEducationalContent _updatedEducationalContentLinkToQuiz = new Entities.QuizEducationalContent(); Entities.QuizEducationalContent _originalEducationalContentLinkToQuiz = new Entities.QuizEducationalContent(); _originalEducationalContentLinkToQuiz = _context.QuizEducationalContents.Where(qec => qec.QuizID.Equals(_quizInformation.QuizID) && qec.QuizVersion.Equals(_quizInformation.Quizversion)).FirstOrDefault(); _updatedEducationalContentLinkToQuiz.QuizID = _quizInformation.QuizID; _updatedEducationalContentLinkToQuiz.QuizVersion = _quizInformation.Quizversion; _updatedEducationalContentLinkToQuiz.EducationalContentID = int.Parse(_quizInformation.RelatedSource); if (_originalEducationalContentLinkToQuiz != null) { _context.Entry(_originalEducationalContentLinkToQuiz).CurrentValues.SetValues(_updatedEducationalContentLinkToQuiz); } else { _context.QuizEducationalContents.Add(_updatedEducationalContentLinkToQuiz); _context.SaveChanges(); } } //***NOT Entity Framework (Because Only Need Questions Marked as Activo, not InActive). _updated = _updated.readQuizByIDAndVersion(_updatedQuiz.QuizID.ToString(), _updatedQuiz.QuizVersion.ToString()); ///*********************** return(Ok(_updated)); } } catch (Exception ex) { return(InternalServerError(ex)); } return(Ok("Quiz Not Found")); }
//Utility Function public Quiz handleIncomingQuizFormat(ViewModels.QuizForm _quiz) { Quiz _quizCorrectFormat = new Quiz(); QuizQuestion _quizQuestionCorrectFormat = new QuizQuestion(); QuizAnswer _quizAnswerCorrectFormat = new QuizAnswer(); List <QuizQuestion> _quizQuestionsLISTCorrectFormat = new List <QuizQuestion>(); List <QuizAnswer> _questionAnswersLISTCorrectFormat = new List <QuizAnswer>(); try { _quizCorrectFormat.QuizID = _quiz.QuizID; _quizCorrectFormat.QuizVersion = _quiz.Quizversion; _quizCorrectFormat.QuizName = _quiz.Name; _quizCorrectFormat.QuizDescription = _quiz.QuizDescription; _quizCorrectFormat.Scale = _quiz.Scale != null?long.Parse(_quiz.Scale) : _quiz.QuizQuestions.Count; _quizCorrectFormat.IsActive = _quiz.Status; _quizCorrectFormat.DateModified = _quiz.LastModifiedDate.ToString(); foreach (ViewModels.QuizQuestionForm _question in _quiz.QuizQuestions) { _quizQuestionCorrectFormat = new QuizQuestion(); _quizQuestionCorrectFormat.QuizID = _quiz.QuizID; _quizQuestionCorrectFormat.QuizVersion = _quiz.Quizversion; _quizQuestionCorrectFormat.QuestionID = _question.QuestionID; _quizQuestionCorrectFormat.Question = _question.Question; _quizQuestionCorrectFormat.IsActive = "1"; _quizQuestionCorrectFormat.DateModified = _quiz.LastModifiedDate.ToString(); string _correctAnswer = _question.CorrectA; _questionAnswersLISTCorrectFormat = new List <QuizAnswer>(); foreach (ViewModels.QuizAnswerForm _answer in _question.Answers) { _quizAnswerCorrectFormat = new QuizAnswer(); _quizAnswerCorrectFormat.QuizID = _quiz.QuizID; _quizAnswerCorrectFormat.QuizVersion = _quiz.Quizversion; _quizAnswerCorrectFormat.QuestionID = _question.QuestionID; _quizAnswerCorrectFormat.AnswerID = _answer.AnswerID; _quizAnswerCorrectFormat.Answer = _answer.Answer; _quizAnswerCorrectFormat.IsCorrect = _answer.AnswerID.ToString() == _question.CorrectA ? "1" : "0"; _quizAnswerCorrectFormat.IsActive = "1"; _quizAnswerCorrectFormat.DateModified = _quiz.LastModifiedDate.ToString(); //Add Answer to Answer List _questionAnswersLISTCorrectFormat.Add(_quizAnswerCorrectFormat); } //Add List of Answers to a Single Question _quizQuestionCorrectFormat.QuizAnswers = _questionAnswersLISTCorrectFormat; //Add Complete Question with Answers, to the Question List _quizQuestionsLISTCorrectFormat.Add(_quizQuestionCorrectFormat); } _quizCorrectFormat.QuizQuestions = _quizQuestionsLISTCorrectFormat; } catch (Exception ex) { throw ex; } return(_quizCorrectFormat); }