From 3fefb35db06240c5ab045510f3e5f8ce208bf736 Mon Sep 17 00:00:00 2001 From: haraldnilsen Date: Sun, 7 Jan 2024 21:33:25 +0100 Subject: [PATCH] :sparkles: functioning form submission Co-authored-by: Sindre Kjelsrud --- backend/cmd/main.go | 6 +- backend/db/insert_user_answers.go | 92 ++++++++++++++++++++++++------- 2 files changed, 75 insertions(+), 23 deletions(-) diff --git a/backend/cmd/main.go b/backend/cmd/main.go index 2e98fbd..631d3cf 100644 --- a/backend/cmd/main.go +++ b/backend/cmd/main.go @@ -20,7 +20,7 @@ type UserformData struct { type FormData struct { FormAnswers string `json:"allFormAnswers"` - RespondentId int `json:"respondentID"` + RespondentId int `json:"respondentID"` } func main() { @@ -77,7 +77,7 @@ func main() { fmt.Print(err) } - respondentID, err := db.InsertUserAnswers(requestBody.RespondentId, requestBody.FormAnswers) + err := db.InsertUserAnswers(requestBody.RespondentId, requestBody.FormAnswers) if err != nil { fmt.Print(err) @@ -86,7 +86,7 @@ func main() { } // Respond with the ID of the newly inserted respondent - c.JSON(http.StatusOK, gin.H{"respondentID": respondentID}) + c.JSON(http.StatusOK, "Successfully inserted formdata!") }) // Run the server on port 8080 diff --git a/backend/db/insert_user_answers.go b/backend/db/insert_user_answers.go index 8abff23..ba69c93 100644 --- a/backend/db/insert_user_answers.go +++ b/backend/db/insert_user_answers.go @@ -9,13 +9,14 @@ import ( _ "github.com/lib/pq" ) -func InsertUserAnswers(respondentId int, allAnswers string) (int, error) { - // Parse allAnswers from JSON string to map - var allAnswersArray []string - err := json.Unmarshal([]byte(allAnswers), &allAnswersArray) - if err != nil { - return 0, fmt.Errorf("invalid JSON format for allAnswers: %v", err) - } +type FormAnswer struct { + AnswerID int + Ratings []int +} + +type AllFormAnswers []FormAnswer + +func InsertUserAnswers(respondentId int, allAnswers string) (error) { // Connection string psqlInfo := fmt.Sprintf("host=%s port=%d user=%s "+ @@ -35,21 +36,72 @@ func InsertUserAnswers(respondentId int, allAnswers string) (int, error) { log.Fatalf("Error connecting to the database: %v\n", err) } - insertStatement := ` - INSERT INTO SvarVurdering (svarID, respondentID, kunnskap, empati, hjelpsomhet) - VALUES ($1, $2, $3, $4, $5) - ` + var convertedFormAnswers = convertAnswerFormat(allAnswers) - tx, err := db.Begin() - if err != nil { - log.Fatalf("Error creating transaction: %v\n", err) + for _,formanswer := range convertedFormAnswers { + insertStatement := ` + INSERT INTO SvarVurdering (svarID, respondentID, kunnskap, empati, hjelpsomhet) + VALUES ($1, $2, $3, $4, $5) + ` + stmt, err := db.Prepare(insertStatement) + if err != nil { + log.Fatalf("Error preparing statement: %v\n", err) + } + defer stmt.Close() + + kunnskap := formanswer.Ratings[0] + empati := formanswer.Ratings[1] + hjelpsomhet := formanswer.Ratings[2] + + _, err = stmt.Exec(formanswer.AnswerID, respondentId, kunnskap, empati, hjelpsomhet) + if err != nil { + log.Fatalf("Error executing statement: %v\n", err) + } } - err = tx.Commit() - if err != nil { - log.Fatalf("Transaction commit failed: %v\n", err) - } + fmt.Print("Inserted data successfully") - fmt.Printf("Data inserted successfully for respondentID: %d\n", respondentId) - return respondentId, nil + return nil +} + +func convertAnswerFormat(allAnswers string) (AllFormAnswers) { + // Define an intermediate structure that matches the input format + var intermediate [][]interface{} + + // Parse the JSON string into the intermediate structure + err := json.Unmarshal([]byte(allAnswers), &intermediate) + if err != nil { + log.Fatalf("Error parsing JSON: %v", err) + } + + // Convert the intermediate structure to OuterSlice + var result AllFormAnswers + for _, pair := range intermediate { + if len(pair) != 2 { + log.Fatalf("Invalid pair length: %v", pair) + } + + number, ok := pair[0].(float64) // JSON numbers are parsed as float64 + if !ok { + log.Fatalf("Expected a number, got: %v", pair[0]) + } + + arrayInterface, ok := pair[1].([]interface{}) + if !ok { + log.Fatalf("Expected an array, got: %v", pair[1]) + } + + var array []int + for _, item := range arrayInterface { + num, ok := item.(float64) + if !ok { + log.Fatalf("Expected a number in array, got: %v", item) + } + array = append(array, int(num)) + } + + result = append(result, FormAnswer{AnswerID: int(number), Ratings: array}) + } + + return result } \ No newline at end of file