🚑 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

@ -2,9 +2,11 @@ package main
import ( import (
"fmt" "fmt"
"net/http"
"helseveileder/db" "helseveileder/db"
"github.com/gin-contrib/cors"
"github.com/gin-gonic/gin" "github.com/gin-gonic/gin"
) )
@ -17,6 +19,7 @@ type FormData struct {
func main() { func main() {
router := gin.Default() router := gin.Default()
router.Use(cors.Default())
// Info about user // Info about user
router.POST("/submitform", func(c *gin.Context) { router.POST("/submitform", func(c *gin.Context) {
@ -27,11 +30,22 @@ func main() {
fmt.Print(err) 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)
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
}
// 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 // Run the server on port 8080
router.Run(":8080") router.Run(":8080")
//db.SetupDb() //db.SetupDb()

View file

@ -8,7 +8,7 @@ import (
_ "github.com/lib/pq" _ "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 // 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",
@ -30,6 +30,7 @@ func InsertData(age string, education string, healthcarepersonell bool, gender s
insertStatement := ` insertStatement := `
INSERT INTO Respondent (alder, utdanningsgrad, helsepersonell, kjønn) INSERT INTO Respondent (alder, utdanningsgrad, helsepersonell, kjønn)
VALUES ($1, $2, $3, $4) VALUES ($1, $2, $3, $4)
RETURNING respondentID
` `
stmt, err := db.Prepare(insertStatement) stmt, err := db.Prepare(insertStatement)
@ -38,10 +39,12 @@ func InsertData(age string, education string, healthcarepersonell bool, gender s
} }
defer stmt.Close() 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 { if err != nil {
log.Fatalf("Error executing statement: %v\n", err) 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
} }

View file

@ -7,6 +7,7 @@ require (
github.com/chenzhuoyu/base64x v0.0.0-20230717121745-296ad89f973d // indirect github.com/chenzhuoyu/base64x v0.0.0-20230717121745-296ad89f973d // indirect
github.com/chenzhuoyu/iasm v0.9.1 // indirect github.com/chenzhuoyu/iasm v0.9.1 // indirect
github.com/gabriel-vasile/mimetype v1.4.3 // 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-contrib/sse v0.1.0 // indirect
github.com/gin-gonic/gin v1.9.1 // indirect github.com/gin-gonic/gin v1.9.1 // indirect
github.com/go-playground/locales v0.14.1 // indirect github.com/go-playground/locales v0.14.1 // indirect

View file

@ -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/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 h1:in2uUcidCuFcDKtdcBxlR0rJ1+fsokWf+uqxgUFjbI0=
github.com/gabriel-vasile/mimetype v1.4.3/go.mod h1:d8uq/6HKRL6CGdk+aubisF/M5GcPfT7nKyLpA0lbSSk= 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 h1:Y/yl/+YNO8GZSjAhjMsSuLt29uWRFHdHYUb5lYOV9qE=
github.com/gin-contrib/sse v0.1.0/go.mod h1:RHrZQHXnP2xjPF+u1gW/2HnVO7nvIa9PG3Gm+fLHvGI= 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= github.com/gin-gonic/gin v1.9.1 h1:4idEAncQnU5cB7BeOkPtxjfCSye0AAm1R0RVIqJ+Jmg=

View file

@ -15,7 +15,19 @@ export const postFormData = (
healthcare_personnel: personnel, healthcare_personnel: personnel,
gender: gender, gender: gender,
}), }),
}).catch((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); console.log(error);
}); });
return response; return response;