diff --git a/backend/cmd/main.go b/backend/cmd/main.go new file mode 100644 index 0000000..96a0f12 --- /dev/null +++ b/backend/cmd/main.go @@ -0,0 +1,46 @@ +package main + +import ( + "fmt" + "net/http" + + "helseveileder/db" + + "github.com/gin-gonic/gin" +) + +type FormData struct { + Age string `json:"age"` + Education string `json:"education"` + HealthcarePersonnel bool `json:"healthcare_personnel"` + Gender string `json:"gender"` +} + +func main() { + router := gin.Default() + + // Define a basic GET request handler + router.GET("/ping", func(c *gin.Context) { + c.JSON(http.StatusOK, gin.H{ + "message": "pong", + }) + }) + + // Info about user + router.POST("/submitform", func(c *gin.Context) { + + var requestBody FormData + + if err := c.BindJSON(&requestBody); err != nil { + fmt.Print(err) + } + + fmt.Print(requestBody) + + db.InsertData(requestBody.Age, requestBody.Education, requestBody.HealthcarePersonnel, requestBody.Gender) + }) + + // Run the server on port 8080 + router.Run(":8080") + // db.SetupDb() +} diff --git a/backend/db/db.go b/backend/db/db.go new file mode 100644 index 0000000..4c5f41d --- /dev/null +++ b/backend/db/db.go @@ -0,0 +1,76 @@ +package db + +import ( + "database/sql" + "fmt" + "log" + + _ "github.com/lib/pq" +) + +const ( + host = "localhost" + port = 5432 // This is the default port for PostgreSQL + user = "admin" + password = "helse123" + dbname = "helseveileder" +) + +func SetupDb() { + // Connection string + psqlInfo := fmt.Sprintf("host=%s port=%d user=%s "+ + "password=%s dbname=%s sslmode=disable", + host, port, user, password, dbname) + + // Connect to the database + db, err := sql.Open("postgres", psqlInfo) + if err != nil { + log.Fatalf("Error opening database: %v\n", err) + } + defer db.Close() + + // Check the connection + err = db.Ping() + if err != nil { + log.Fatalf("Error connecting to the database: %v\n", err) + } + + // SQL statements to create tables + createTableStatements := []string{ + `CREATE TABLE IF NOT EXISTS Spørsmål ( + spørsmålID SERIAL PRIMARY KEY, + tekst TEXT NOT NULL + );`, + `CREATE TABLE IF NOT EXISTS SpørsmålSvar ( + svarID SERIAL PRIMARY KEY, + spørsmålID INT REFERENCES Spørsmål(spørsmålID), + chatgpt BOOL NOT NULL, + svartekst TEXT NOT NULL + );`, + `CREATE TABLE IF NOT EXISTS Respondent ( + respondentID SERIAL PRIMARY KEY, + alder TEXT NOT NULL, + utdanningsgrad TEXT NOT NULL, + helsepersonell BOOL NOT NULL, + kjønn TEXT NOT NULL + );`, + `CREATE TABLE IF NOT EXISTS SvarVurdering ( + vurderingID SERIAL PRIMARY KEY, + respondentID INT REFERENCES Respondent(respondentID), + svarID INT REFERENCES SpørsmålSvar(svarID), + kunnskap INT, + empati INT, + hjelpsomhet INT + );`, + } + + // Execute SQL statements + for _, stmt := range createTableStatements { + _, err := db.Exec(stmt) + if err != nil { + log.Fatalf("Error creating table: %v\n", err) + } + } + + fmt.Println("Tables created successfully.") +} diff --git a/backend/db/insert_data.go b/backend/db/insert_data.go new file mode 100644 index 0000000..e08eb01 --- /dev/null +++ b/backend/db/insert_data.go @@ -0,0 +1,47 @@ +package db + +import ( + "database/sql" + "fmt" + "log" + + _ "github.com/lib/pq" +) + +func InsertData(age string, education string, healthcarepersonell bool, gender string) { + // Connection string + psqlInfo := fmt.Sprintf("host=%s port=%d user=%s "+ + "password=%s dbname=%s sslmode=disable", + host, port, user, password, dbname) + + // Connect to the database + db, err := sql.Open("postgres", psqlInfo) + if err != nil { + log.Fatalf("Error opening database: %v\n", err) + } + defer db.Close() + + // Check the connection + err = db.Ping() + if err != nil { + log.Fatalf("Error connecting to the database: %v\n", err) + } + + insertStatement := ` + INSERT INTO Respondent (alder, utdanningsgrad, helsepersonell, kjønn) + VALUES ($1, $2, $3, $4) + ` + + stmt, err := db.Prepare(insertStatement) + if err != nil { + log.Fatalf("Error preparing statement: %v\n", err) + } + defer stmt.Close() + + _, err = stmt.Exec(age, education, healthcarepersonell, gender) + if err != nil { + log.Fatalf("Error executing statement: %v\n", err) + } + + fmt.Println("Data inserted successfully") +} \ No newline at end of file