2023-12-21 19:59:52 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2023-12-23 15:26:00 +00:00
|
|
|
"net/http"
|
2023-12-27 17:00:18 +00:00
|
|
|
"strconv"
|
2023-12-21 19:59:52 +00:00
|
|
|
|
|
|
|
"helseveileder/db"
|
|
|
|
|
2023-12-23 15:26:00 +00:00
|
|
|
"github.com/gin-contrib/cors"
|
2023-12-21 19:59:52 +00:00
|
|
|
"github.com/gin-gonic/gin"
|
|
|
|
)
|
|
|
|
|
2024-01-07 19:48:38 +00:00
|
|
|
type UserformData struct {
|
2023-12-21 19:59:52 +00:00
|
|
|
Age string `json:"age"`
|
|
|
|
Education string `json:"education"`
|
2023-12-23 17:27:07 +00:00
|
|
|
HealthcarePersonnel bool `json:"healthcare_personnel"`
|
2023-12-21 19:59:52 +00:00
|
|
|
Gender string `json:"gender"`
|
|
|
|
}
|
|
|
|
|
2024-01-07 19:48:38 +00:00
|
|
|
type FormData struct {
|
|
|
|
FormAnswers string `json:"allFormAnswers"`
|
2024-01-07 20:33:25 +00:00
|
|
|
RespondentId int `json:"respondentID"`
|
2024-01-07 19:48:38 +00:00
|
|
|
}
|
|
|
|
|
2023-12-21 19:59:52 +00:00
|
|
|
func main() {
|
|
|
|
router := gin.Default()
|
2023-12-23 15:26:00 +00:00
|
|
|
router.Use(cors.Default())
|
|
|
|
|
2023-12-21 19:59:52 +00:00
|
|
|
// Info about user
|
2024-01-07 12:59:07 +00:00
|
|
|
router.POST("/submituserform", func(c *gin.Context) {
|
2023-12-21 19:59:52 +00:00
|
|
|
|
2024-01-07 19:48:38 +00:00
|
|
|
var requestBody UserformData
|
2023-12-21 19:59:52 +00:00
|
|
|
|
|
|
|
if err := c.BindJSON(&requestBody); err != nil {
|
|
|
|
fmt.Print(err)
|
|
|
|
}
|
|
|
|
|
2023-12-23 15:26:00 +00:00
|
|
|
// Capture both the ID and error returned from InsertData
|
2023-12-23 17:27:07 +00:00
|
|
|
respondentId, err := db.InsertUserData(requestBody.Age, requestBody.Education, requestBody.HealthcarePersonnel, requestBody.Gender)
|
2023-12-23 15:26:00 +00:00
|
|
|
|
|
|
|
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})
|
2023-12-21 19:59:52 +00:00
|
|
|
})
|
|
|
|
|
2023-12-23 15:26:00 +00:00
|
|
|
// Get questions & answers from database
|
2023-12-27 17:00:18 +00:00
|
|
|
router.GET("/userquestions", func(c *gin.Context) {
|
|
|
|
respondentID, err := strconv.Atoi(c.Query("respondentID"))
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
fmt.Print(err)
|
|
|
|
c.JSON(http.StatusInternalServerError, gin.H{"error": "Wrong respondentID-format (should be int)."})
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
questions, err := db.GetUserQuestions(respondentID)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
fmt.Print(err)
|
|
|
|
c.JSON(http.StatusInternalServerError, gin.H{"error": "Error getting questions for given user."})
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
c.JSON(http.StatusOK, gin.H{"questions": questions})
|
|
|
|
})
|
2023-12-23 15:26:00 +00:00
|
|
|
|
2024-01-07 12:59:07 +00:00
|
|
|
router.POST("/submitanswers", func(c *gin.Context) {
|
|
|
|
var requestBody FormData
|
|
|
|
|
|
|
|
if err := c.BindJSON(&requestBody); err != nil {
|
|
|
|
fmt.Print(err)
|
|
|
|
}
|
2024-01-07 19:48:38 +00:00
|
|
|
|
2024-01-07 20:33:25 +00:00
|
|
|
err := db.InsertUserAnswers(requestBody.RespondentId, requestBody.FormAnswers)
|
2024-01-07 19:48:38 +00:00
|
|
|
|
|
|
|
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
|
2024-01-07 20:33:25 +00:00
|
|
|
c.JSON(http.StatusOK, "Successfully inserted formdata!")
|
2024-01-07 12:59:07 +00:00
|
|
|
})
|
|
|
|
|
2023-12-21 19:59:52 +00:00
|
|
|
// Run the server on port 8080
|
|
|
|
router.Run(":8080")
|
2023-12-23 14:54:23 +00:00
|
|
|
//db.SetupDb()
|
2023-12-21 19:59:52 +00:00
|
|
|
}
|