From f5a7571c4b3e455d3448034b8d154f2124abb442 Mon Sep 17 00:00:00 2001 From: haraldnilsen Date: Wed, 27 Dec 2023 18:00:18 +0100 Subject: [PATCH] :sparkles: working endpoint for questions Co-authored-by: Sindre Kjelsrud --- backend/cmd/main.go | 21 +++++++- backend/db/get_user_questions.go | 93 +++++++++++++++++++++++++++----- 2 files changed, 101 insertions(+), 13 deletions(-) diff --git a/backend/cmd/main.go b/backend/cmd/main.go index 0bedccd..47e38b1 100644 --- a/backend/cmd/main.go +++ b/backend/cmd/main.go @@ -3,6 +3,7 @@ package main import ( "fmt" "net/http" + "strconv" "helseveileder/db" @@ -44,7 +45,25 @@ func main() { }) // Get questions & answers from database - //router.GET("/") + router.GET("/userquestions", func(c *gin.Context) { + respondentID, err := strconv.Atoi(c.Query("respondentID")) + + if err != nil { + fmt.Print(err) + c.JSON(http.StatusInternalServerError, gin.H{"error": "Wrong respondentID-format (should be int)."}) + return + } + + questions, err := db.GetUserQuestions(respondentID) + + if err != nil { + fmt.Print(err) + c.JSON(http.StatusInternalServerError, gin.H{"error": "Error getting questions for given user."}) + return + } + + c.JSON(http.StatusOK, gin.H{"questions": questions}) + }) // Run the server on port 8080 router.Run(":8080") diff --git a/backend/db/get_user_questions.go b/backend/db/get_user_questions.go index c81d8c5..056f2eb 100644 --- a/backend/db/get_user_questions.go +++ b/backend/db/get_user_questions.go @@ -22,10 +22,10 @@ type QuestionAnswer struct { type UserQuestions struct { Question FormQuestion - Answers [2]QuestionAnswer + Answers []QuestionAnswer } -func GetUserQuestions(respondendID int) ([5]UserQuestions, error) { +func GetUserQuestions(respondentID int) ([]UserQuestions, error) { // Connection string psqlInfo := fmt.Sprintf("host=%s port=%d user=%s "+ "password=%s dbname=%s sslmode=disable", @@ -44,13 +44,12 @@ func GetUserQuestions(respondendID int) ([5]UserQuestions, error) { log.Fatalf("Error connecting to the database: %v\n", err) } - getQuestionsStatement := fmt.Sprintf("SELECT s.SpørsmålID, s.tekst"+ - "FROM Spørsmål s"+ - "LEFT JOIN Spørsmålsvar ss ON s.spørsmålID == ss.spørsmålID"+ - "LEFT JOIN SvarVurdering sv ON ss.svarID == sv.svarID AND sv.respondentID = :$respondendID"+ - "WHERE sv.vurderingID IS NULL"+ - "LIMIT 5", - respondendID) + getQuestionsStatement := `SELECT s.SpørsmålID, s.tekst + FROM Spørsmål s + LEFT JOIN Spørsmålsvar ss ON s.spørsmålID = ss.spørsmålID + LEFT JOIN SvarVurdering sv ON ss.svarID = sv.svarID AND sv.respondentID = $1 + WHERE sv.vurderingID IS NULL + LIMIT 5` stmt, err := db.Prepare(getQuestionsStatement) if err != nil { @@ -58,10 +57,80 @@ func GetUserQuestions(respondendID int) ([5]UserQuestions, error) { } defer stmt.Close() - var questions [5]FormQuestion + var questionAnswers []UserQuestions - rows, err := stmt.Query(getQuestionsStatement) + rows, err := stmt.Query(respondentID) + if err != nil { + return questionAnswers, err + } + defer rows.Close() - + for rows.Next() { + var question FormQuestion + if err := rows.Scan(&question.QuestionID, &question.QuestionText); err != nil { + return questionAnswers, err + } + getAnswersStatement := `WITH TrueAnswer AS ( + SELECT ss.svarID + FROM SpørsmålSvar ss + WHERE ss.spørsmålID = $1 + AND ss.chatgpt = TRUE + AND NOT EXISTS ( + SELECT 1 + FROM SvarVurdering sv + WHERE sv.svarID = ss.svarID + AND sv.respondentID = $2 + ) + LIMIT 1 + ), + FalseAnswer AS ( + SELECT ss.svarID + FROM SpørsmålSvar ss + WHERE ss.spørsmålID = $1 + AND ss.chatgpt = FALSE + AND NOT EXISTS ( + SELECT 1 + FROM SvarVurdering sv + WHERE sv.svarID = ss.svarID + AND sv.respondentID = $2 + ) + LIMIT 1 + ) + SELECT ss.svarID, ss.spørsmålID, ss.chatgpt, ss.svartekst + FROM SpørsmålSvar ss + WHERE ss.svarID IN (SELECT svarID FROM TrueAnswer UNION ALL SELECT svarID FROM FalseAnswer); + ` + + stmt, err := db.Prepare(getAnswersStatement) + if err != nil { + log.Fatalf("Error preparing statement: %v\n", err) + } + defer stmt.Close() + + var answers []QuestionAnswer + // var questionAnswers[5] UserQuestions + + rows, err := stmt.Query(question.QuestionID, respondentID) + if err != nil { + return questionAnswers, err + } + defer rows.Close() + + for rows.Next() { + var answer QuestionAnswer + if err := rows.Scan(&answer.AnswerID, &answer.QuestionID, &answer.IsChatGPT, &answer.AnswerText); err != nil { + return questionAnswers, err + } + answers = append(answers, answer) + } + + questionAnswers = append(questionAnswers, UserQuestions{Question: question, Answers: answers}) + } + + if err = rows.Err(); err != nil { + return questionAnswers, err + } + + return questionAnswers, nil } \ No newline at end of file