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")); }