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