functioning form submission

Co-authored-by: Sindre Kjelsrud <kjelsrudsindre@gmail.com>
This commit is contained in:
haraldnilsen 2024-01-07 21:33:25 +01:00
parent 9f113c1258
commit 3fefb35db0
2 changed files with 75 additions and 23 deletions

View file

@ -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

View file

@ -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
}