//Metode som lagrer spørsmål fra kunden gjort fra QuestionPage.html og lagrer det i databasen. public void savequestion(QandA innQuestion) { var id = innQuestion.Id; try { bool foundQuestion = db.QandAs.Any(p => p.Id == id); if (foundQuestion) { QA updatequestion = db.QandAs.Find(id); updatequestion.Question = innQuestion.Question; updatequestion.Answer = innQuestion.Answer; updatequestion.Category = innQuestion.Category; updatequestion.Votecounter = innQuestion.Votecounter + 1; db.SaveChanges(); } else { QA newQuestion = new QA(); // spørsmål til databasen newQuestion.Question = innQuestion.Question; newQuestion.Answer = "Spørsmål venter på svar fra administrator."; newQuestion.Category = innQuestion.Category; db.QandAs.Add(newQuestion); db.SaveChanges(); } } catch (Exception ex) { //Exception handling } }
//Metode som gjør at avstemming på spørsmål/svar blir lagret i databasen basert på om kunden var fornøyd med svaret eller ikke. public void VoteAnswer(QandA innVote) { var id = innVote.Id; try { bool foundQuestion = db.QandAs.Any(p => p.Id == id); if (foundQuestion && innVote.Votetype == 1) { QA updatequestion = db.QandAs.Find(id); updatequestion.Votecounter = updatequestion.Votecounter + 1; db.SaveChanges(); } else { QA updatequestion = db.QandAs.Find(id); updatequestion.Question = updatequestion.Question; updatequestion.Answer = updatequestion.Answer; updatequestion.Category = updatequestion.Category; updatequestion.Votecounter = updatequestion.Votecounter - 1; db.SaveChanges(); } } catch (Exception ex) { //Feilhåndtering her } }
//Sletter spørsål og svar fra databasen basert på valgt id public void deleteQuestion(int id) { try { QA questionToDelete = db.QandAs.FirstOrDefault(p => p.Id == id); db.QandAs.Remove(questionToDelete); db.SaveChanges(); } catch (Exception ex) { } }