diff --git a/backend/cmd/main.go b/backend/cmd/main.go index 24a0cb3..2a893a7 100644 --- a/backend/cmd/main.go +++ b/backend/cmd/main.go @@ -2,9 +2,11 @@ package main import ( "fmt" + "net/http" "helseveileder/db" + "github.com/gin-contrib/cors" "github.com/gin-gonic/gin" ) @@ -17,7 +19,8 @@ type FormData struct { func main() { router := gin.Default() - + router.Use(cors.Default()) + // Info about user router.POST("/submitform", func(c *gin.Context) { @@ -27,11 +30,22 @@ func main() { fmt.Print(err) } - fmt.Print(requestBody) + // Capture both the ID and error returned from InsertData + respondentId, err := db.InsertData(requestBody.Age, requestBody.Education, requestBody.HealthcarePersonnel, requestBody.Gender) + + if err != nil { + fmt.Print(err) + c.JSON(http.StatusInternalServerError, gin.H{"error": "Unable to insert data"}) + return + } - db.InsertData(requestBody.Age, requestBody.Education, requestBody.HealthcarePersonnel, requestBody.Gender) + // Respond with the ID of the newly inserted respondent + c.JSON(http.StatusOK, gin.H{"respondentID": respondentId}) }) + // Get questions & answers from database + //router.GET("/") + // Run the server on port 8080 router.Run(":8080") //db.SetupDb() diff --git a/backend/db/insert_data.go b/backend/db/insert_data.go index e08eb01..2c57bad 100644 --- a/backend/db/insert_data.go +++ b/backend/db/insert_data.go @@ -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 } \ No newline at end of file diff --git a/backend/go.mod b/backend/go.mod index c9c2fc6..137c929 100644 --- a/backend/go.mod +++ b/backend/go.mod @@ -7,6 +7,7 @@ require ( github.com/chenzhuoyu/base64x v0.0.0-20230717121745-296ad89f973d // indirect github.com/chenzhuoyu/iasm v0.9.1 // indirect github.com/gabriel-vasile/mimetype v1.4.3 // indirect + github.com/gin-contrib/cors v1.5.0 // indirect github.com/gin-contrib/sse v0.1.0 // indirect github.com/gin-gonic/gin v1.9.1 // indirect github.com/go-playground/locales v0.14.1 // indirect diff --git a/backend/go.sum b/backend/go.sum index 3b6f8e7..f10eca7 100644 --- a/backend/go.sum +++ b/backend/go.sum @@ -13,6 +13,8 @@ github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSs github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= github.com/gabriel-vasile/mimetype v1.4.3 h1:in2uUcidCuFcDKtdcBxlR0rJ1+fsokWf+uqxgUFjbI0= github.com/gabriel-vasile/mimetype v1.4.3/go.mod h1:d8uq/6HKRL6CGdk+aubisF/M5GcPfT7nKyLpA0lbSSk= +github.com/gin-contrib/cors v1.5.0 h1:DgGKV7DDoOn36DFkNtbHrjoRiT5ExCe+PC9/xp7aKvk= +github.com/gin-contrib/cors v1.5.0/go.mod h1:TvU7MAZ3EwrPLI2ztzTt3tqgvBCq+wn8WpZmfADjupI= github.com/gin-contrib/sse v0.1.0 h1:Y/yl/+YNO8GZSjAhjMsSuLt29uWRFHdHYUb5lYOV9qE= github.com/gin-contrib/sse v0.1.0/go.mod h1:RHrZQHXnP2xjPF+u1gW/2HnVO7nvIa9PG3Gm+fLHvGI= github.com/gin-gonic/gin v1.9.1 h1:4idEAncQnU5cB7BeOkPtxjfCSye0AAm1R0RVIqJ+Jmg= diff --git a/frontend/src/api/postFormData.ts b/frontend/src/api/postFormData.ts index df37694..0eb818e 100644 --- a/frontend/src/api/postFormData.ts +++ b/frontend/src/api/postFormData.ts @@ -15,8 +15,20 @@ export const postFormData = ( healthcare_personnel: personnel, gender: gender, }), - }).catch((error) => { - console.log(error); - }); + }) + .then((response) => { + if (!response.ok) { + throw new Error(`HTTP error! Status: ${response.status}`); + } + return response.json(); + }) + .then((data) => { + console.log(data); + localStorage.setItem("RespondentId", data); + return data; + }) + .catch((error) => { + console.log(error); + }); return response; };