✨ working endpoint for questions
Co-authored-by: Sindre Kjelsrud <kjelsrudsindre@gmail.com>
This commit is contained in:
parent
702e1d8458
commit
f5a7571c4b
2 changed files with 101 additions and 13 deletions
|
@ -3,6 +3,7 @@ package main
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"net/http"
|
"net/http"
|
||||||
|
"strconv"
|
||||||
|
|
||||||
"helseveileder/db"
|
"helseveileder/db"
|
||||||
|
|
||||||
|
@ -44,7 +45,25 @@ func main() {
|
||||||
})
|
})
|
||||||
|
|
||||||
// Get questions & answers from database
|
// 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
|
// Run the server on port 8080
|
||||||
router.Run(":8080")
|
router.Run(":8080")
|
||||||
|
|
|
@ -22,10 +22,10 @@ type QuestionAnswer struct {
|
||||||
|
|
||||||
type UserQuestions struct {
|
type UserQuestions struct {
|
||||||
Question FormQuestion
|
Question FormQuestion
|
||||||
Answers [2]QuestionAnswer
|
Answers []QuestionAnswer
|
||||||
}
|
}
|
||||||
|
|
||||||
func GetUserQuestions(respondendID int) ([5]UserQuestions, error) {
|
func GetUserQuestions(respondentID int) ([]UserQuestions, error) {
|
||||||
// Connection string
|
// Connection string
|
||||||
psqlInfo := fmt.Sprintf("host=%s port=%d user=%s "+
|
psqlInfo := fmt.Sprintf("host=%s port=%d user=%s "+
|
||||||
"password=%s dbname=%s sslmode=disable",
|
"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)
|
log.Fatalf("Error connecting to the database: %v\n", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
getQuestionsStatement := fmt.Sprintf("SELECT s.SpørsmålID, s.tekst"+
|
getQuestionsStatement := `SELECT s.SpørsmålID, s.tekst
|
||||||
"FROM Spørsmål s"+
|
FROM Spørsmål s
|
||||||
"LEFT JOIN Spørsmålsvar ss ON s.spørsmålID == ss.spørsmålID"+
|
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"+
|
LEFT JOIN SvarVurdering sv ON ss.svarID = sv.svarID AND sv.respondentID = $1
|
||||||
"WHERE sv.vurderingID IS NULL"+
|
WHERE sv.vurderingID IS NULL
|
||||||
"LIMIT 5",
|
LIMIT 5`
|
||||||
respondendID)
|
|
||||||
|
|
||||||
stmt, err := db.Prepare(getQuestionsStatement)
|
stmt, err := db.Prepare(getQuestionsStatement)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
@ -58,10 +57,80 @@ func GetUserQuestions(respondendID int) ([5]UserQuestions, error) {
|
||||||
}
|
}
|
||||||
defer stmt.Close()
|
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
|
||||||
}
|
}
|
Reference in a new issue