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
2024-01-13 12:00:08 +01:00
"helseveileder/cmd/db"
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-10 20:14:14 +01:00
AnsweredBefore bool ` json:"answered_before" `
County string ` json:"county" `
SubmitDate string ` json:"submit_date" `
2023-12-21 20:59:52 +01:00
}
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
}
2024-01-10 13:05:58 +01:00
type BugReport struct {
BugText string ` json:"bugText" `
}
2024-01-10 13:31:02 +01:00
type Evaluation struct {
EvaluationText string ` json:"evaluationText" `
}
2023-12-21 20:59:52 +01:00
func main ( ) {
router := gin . Default ( )
2024-01-13 20:52:21 +01:00
//router.Use(cors.Default())
2024-01-12 22:23:33 +01:00
router . GET ( "/hello" , func ( c * gin . Context ) {
c . JSON ( http . StatusOK , gin . H { "hello" : "world" } )
} )
2023-12-23 16:26:00 +01:00
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
2024-01-10 20:14:14 +01:00
respondentId , err := db . InsertUserData ( requestBody . Age , requestBody . Education , requestBody . HealthcarePersonnel , requestBody . Gender , requestBody . AnsweredBefore , requestBody . County , requestBody . SubmitDate )
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
} )
2024-01-10 13:05:58 +01:00
router . POST ( "/submitbug" , func ( c * gin . Context ) {
var requestBody BugReport
// Bind JSON to the requestBody struct
if err := c . BindJSON ( & requestBody ) ; err != nil {
fmt . Print ( err )
c . JSON ( http . StatusBadRequest , gin . H { "error" : "Invalid request" } )
return
}
// Here, you'd insert the bug text into your database
err := db . InsertBugReport ( requestBody . BugText )
if err != nil {
fmt . Print ( err )
c . JSON ( http . StatusInternalServerError , gin . H { "error" : "Unable to insert bug report" } )
return
}
// Respond with a success message
c . JSON ( http . StatusOK , "Successfully submitted bug report!" )
} )
2024-01-10 13:31:02 +01:00
router . POST ( "/submiteval" , func ( c * gin . Context ) {
var requestBody Evaluation
// Bind JSON to the requestBody struct
if err := c . BindJSON ( & requestBody ) ; err != nil {
fmt . Print ( err )
c . JSON ( http . StatusBadRequest , gin . H { "error" : "Invalid request" } )
return
}
// Here, you'd insert the bug text into your database
err := db . InsertEvaluation ( requestBody . EvaluationText )
if err != nil {
fmt . Print ( err )
c . JSON ( http . StatusInternalServerError , gin . H { "error" : "Unable to insert evaluation" } )
return
}
// Respond with a success message
c . JSON ( http . StatusOK , "Successfully submitted evaluation!" )
} )
2023-12-21 20:59:52 +01:00
// Run the server on port 8080
2024-01-13 20:52:21 +01:00
//db.SetupDb()
2023-12-21 20:59:52 +01:00
router . Run ( ":8080" )
}