🚧 working on endpoint for useranswer submit

Co-authored-by: haraldnilsen <harald_998@hotmail.com>
Signed-off-by: Sindre Kjelsrud <kjelsrudsindre@gmail.com>
This commit is contained in:
Sindre Kjelsrud 2024-01-07 20:48:38 +01:00
parent 7ffea02700
commit 9f113c1258
Signed by untrusted user who does not match committer: sidski
GPG key ID: D2BBDF3EDE6BA9A6
5 changed files with 107 additions and 4 deletions

View file

@ -11,13 +11,18 @@ import (
"github.com/gin-gonic/gin"
)
type FormData struct {
type UserformData struct {
Age string `json:"age"`
Education string `json:"education"`
HealthcarePersonnel bool `json:"healthcare_personnel"`
Gender string `json:"gender"`
}
type FormData struct {
FormAnswers string `json:"allFormAnswers"`
RespondentId int `json:"respondentID"`
}
func main() {
router := gin.Default()
router.Use(cors.Default())
@ -25,7 +30,7 @@ func main() {
// Info about user
router.POST("/submituserform", func(c *gin.Context) {
var requestBody FormData
var requestBody UserformData
if err := c.BindJSON(&requestBody); err != nil {
fmt.Print(err)
@ -71,6 +76,17 @@ func main() {
if err := c.BindJSON(&requestBody); err != nil {
fmt.Print(err)
}
respondentID, err := db.InsertUserAnswers(requestBody.RespondentId, requestBody.FormAnswers)
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})
})
// Run the server on port 8080