🚑 added respondentId to POST-endpoint

Co-authored-by: haraldnilsen <harald_998@hotmail.com>
Signed-off-by: Sindre Kjelsrud <kjelsrudsindre@gmail.com>
This commit is contained in:
Sindre Kjelsrud 2023-12-23 16:26:00 +01:00
parent d56893e9dd
commit db0f1f8de0
Signed by untrusted user who does not match committer: sidski
GPG key ID: D2BBDF3EDE6BA9A6
5 changed files with 41 additions and 9 deletions

View file

@ -8,7 +8,7 @@ import (
_ "github.com/lib/pq"
)
func InsertData(age string, education string, healthcarepersonell bool, gender string) {
func InsertData(age string, education string, healthcarepersonell bool, gender string) (int, error) {
// Connection string
psqlInfo := fmt.Sprintf("host=%s port=%d user=%s "+
"password=%s dbname=%s sslmode=disable",
@ -30,6 +30,7 @@ func InsertData(age string, education string, healthcarepersonell bool, gender s
insertStatement := `
INSERT INTO Respondent (alder, utdanningsgrad, helsepersonell, kjønn)
VALUES ($1, $2, $3, $4)
RETURNING respondentID
`
stmt, err := db.Prepare(insertStatement)
@ -38,10 +39,12 @@ func InsertData(age string, education string, healthcarepersonell bool, gender s
}
defer stmt.Close()
_, err = stmt.Exec(age, education, healthcarepersonell, gender)
var respondentID int
err = stmt.QueryRow(age, education, healthcarepersonell, gender).Scan(&respondentID)
if err != nil {
log.Fatalf("Error executing statement: %v\n", err)
}
fmt.Println("Data inserted successfully")
fmt.Printf("Data inserted successfully with respondentID: %d\n", respondentID)
return respondentID, nil
}