🚧 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:
parent
7ffea02700
commit
9f113c1258
5 changed files with 107 additions and 4 deletions
|
@ -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
|
||||
|
|
Reference in a new issue