🚑 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 (
"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()