/// <summary> /// This method attaches an answer to a question using the entity that represents the question answers table. /// Logging and Exception handling is present /// </summary> /// <param name="QuestionID">The Subquestion ID of the question you would like to add an answer to</param> /// <param name="Answer">The Actual Answer</param> /// <param name="IC">The boolean value that represents if the answer is the correct answer for the specified question</param> public void AddAnswer(int QuestionID, string Answer, bool IC) { EAD.Answer ans = new EAD.Answer(); ans.Answer1 = Answer; NLogConfig.logger.Log(new LogEventInfo(LogLevel.Info, "WFCLogger", "The answer value has been added")); try { NLogConfig.logger.Log(new LogEventInfo(LogLevel.Info, "WFCLogger", "before db.Answer.Add")); db.Answer.Add(ans); //add the answer object to the database Answer Table NLogConfig.logger.Log(new LogEventInfo(LogLevel.Info, "WFCLogger", "After db.Answer.Add")); db.SaveChanges(); var tempPKID = db.Answer.OrderByDescending(item => item.PKID).First(); //Get the PKID of the answer that was just inserted int NewAnswerID = tempPKID.PKID; if (tempPKID.PKID == null) { NLogConfig.logger.Log(new LogEventInfo(LogLevel.Info, "WFCLogger", "The PKID of new answer has never been recieved")); } else { NLogConfig.logger.Log(new LogEventInfo(LogLevel.Info, "WFCLogger", "temp PKID")); NLogConfig.logger.Log(new LogEventInfo(LogLevel.Info, "WFCLogger", "The answer PKID has been received")); } spAddQuestionToAnswer(QuestionID, NewAnswerID, IC); //Call a method that uses a stored procedure call to update the junction table in the database according to the question Answer combination NLogConfig.logger.Log(new LogEventInfo(LogLevel.Info, "WFCLogger", "Stored Procedure fired")); } catch (Exception ex) { // TODO } }
/// <summary> /// Returns a list of answers for the question id passed in /// </summary> /// <param name="Questid"></param> /// <returns></returns> public List <Answers> GetAnswersQuestion(int Questid) { if (db == null) { NLogConfig.logger.Log(new LogEventInfo(LogLevel.Info, "WFCLogger", "The Database is null")); } else if (db.Database == null) { NLogConfig.logger.Log(new LogEventInfo(LogLevel.Info, "WFCLogger", "The Database is null")); } else { NLogConfig.logger.Log(new LogEventInfo(LogLevel.Info, "WFCLogger", "Nothing is Null")); } NLogConfig.logger.Log(new LogEventInfo(LogLevel.Info, "WFCLogger", "GetAnswersQuestion Started")); try { AutoMapperConfiguration.Configure(); List <int> AnswerID = db.QuestionAnswers.Where(c => c.QuestionID == Questid).Select(x => x.AnswerID).ToList(); NLogConfig.logger.Log(new LogEventInfo(LogLevel.Info, "WFCLogger", "Got AnswerID's")); List <EAD.Answer> AnswerDB = db.Answer.ToList(); List <EAD.QuestionAnswers> dbQuestionAns = db.QuestionAnswers.ToList(); List <Answers> ListOfAnswers = new List <Answers>(); if (AnswerID.Count > 0) { for (int k = 0; k < AnswerID.Count; k++) { EAD.Answer ans = (from tempanswer in AnswerDB where tempanswer.PKID == AnswerID.ElementAt(k) select tempanswer).First(); NLogConfig.logger.Log(new LogEventInfo(LogLevel.Info, "WFCLogger", $"Got Answers for Specified Question {Questid}")); Answers answer = Mapper.Map <Answers>(ans); if (dbQuestionAns.Where(s => s.QuestionID == Questid && s.AnswerID == answer.PKID).Select(s => s.IsCorrect).First() == true) { NLogConfig.logger.Log(new LogEventInfo(LogLevel.Info, "WFCLogger", $"Set correct answer for question with id {Questid}")); answer.correct.isCorrect = true; } else { NLogConfig.logger.Log(new LogEventInfo(LogLevel.Info, "WFCLogger", $"Assigned answer {answer.PKID} to false for question {Questid}")); answer.correct.isCorrect = false; } ListOfAnswers.Add(answer); } } NLogConfig.logger.Log(new LogEventInfo(LogLevel.Info, "WFCLogger", $"returned a list of answes for question {Questid}")); return(ListOfAnswers); } catch (Exception e) { NLogConfig.logger.Log(new LogEventInfo(LogLevel.Info, "WFCLogger", e.StackTrace)); return(null); } }
public void AddExamQuestion(ExamQuestion examQuestion) // Adds an exam question using a whole exam question object/model/EAD { if (examQuestion == null) //Exam Question does it exist { throw new ArgumentNullException("Exam Question"); //You passed in a empty exam Question Object } AutoMapperConfiguration.Configure(); EAD.ExamQuestion DALExamQuestion = new EAD.ExamQuestion(); DALExamQuestion.ExamQuestionID = examQuestion.ExamQuestionID; DALExamQuestion.ExamQuestionName = examQuestion.ExamQuestionName; DALExamQuestion.QuestionTypeID = examQuestion.QuestionType.PKID; db.ExamQuestion.Add(DALExamQuestion); db.SaveChanges(); foreach (var subquestion in examQuestion.quest) { EAD.Question questiontoAdd = new EAD.Question(); EAD.ExamQuestionList questioncombination = new EAD.ExamQuestionList(); questiontoAdd.Description = subquestion.Description; db.Question.Add(questiontoAdd); db.SaveChanges(); questioncombination.ExamQuestionID = DALExamQuestion.ExamQuestionID; questioncombination.QuestionID = questiontoAdd.PKID; //adds to subquestion table db.ExamQuestionList.Add(questioncombination); //adds to subquestion/examquestion junction table foreach (var answer in subquestion.Answers) { EAD.Answer answertoAdd = new EAD.Answer(); answertoAdd.Answer1 = answer.Answer1; answertoAdd.AddLanguageTypeID = 1; //Change the Language type to 1 EAD.QuestionAnswers answercombination = new EAD.QuestionAnswers(); db.Answer.Add(answertoAdd); // adds answer to answer Table db.SaveChanges(); answercombination.AnswerID = answertoAdd.PKID; answercombination.QuestionID = questiontoAdd.PKID; answercombination.IsCorrect = answer.correct.isCorrect; db.QuestionAnswers.Add(answercombination); // Add the QuestionAnswers junction table row db.SaveChanges(); } } }
/// <summary> /// Delete /// </summary> /// Might want to change this Method's parameters and functionality to use the PKID because to delete you have to type in the whole answer instead of easily using PKID and also the whole string has to match which is more work /// <param name="Answerdesc">The actual answer string</param> public void DeleteAnswer(string Answerdesc) { int answerID = 0; EAD.Answer removedAnswer = new EAD.Answer(); try { foreach (var item in db.Answer) //Gets the Answer which will be needed so it can be removed { if (item.Answer1 == Answerdesc) { answerID = item.PKID; removedAnswer = item; } } } catch (Exception ex) { // TODO } try { foreach (var item in db.QuestionAnswers) //Removes all references to the Answer in the database { if (item.AnswerID == answerID) { db.QuestionAnswers.Remove(item); //Remove the row from the database } } db.Answer.Remove(removedAnswer); // removes the Answer from the Answer table. db.SaveChanges(); } catch (Exception ex) { //TODO } }