This repository has been archived on 2024-12-07. You can view files and clone it, but cannot push or open issues or pull requests.
helseveileder/backend/cmd/main.go
Sindre Kjelsrud db0f1f8de0
🚑 added respondentId to POST-endpoint
Co-authored-by: haraldnilsen <harald_998@hotmail.com>
Signed-off-by: Sindre Kjelsrud <kjelsrudsindre@gmail.com>
2023-12-23 16:26:00 +01:00

52 lines
1.2 KiB
Go

package main
import (
"fmt"
"net/http"
"helseveileder/db"
"github.com/gin-contrib/cors"
"github.com/gin-gonic/gin"
)
type FormData struct {
Age string `json:"age"`
Education string `json:"education"`
HealthcarePersonnel bool `json:"healthcare_personnel"`
Gender string `json:"gender"`
}
func main() {
router := gin.Default()
router.Use(cors.Default())
// Info about user
router.POST("/submitform", func(c *gin.Context) {
var requestBody FormData
if err := c.BindJSON(&requestBody); err != nil {
fmt.Print(err)
}
// 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
}
// 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()
}