From ed474d4433f4c7eb1c1e031c82bf85e543e811d2 Mon Sep 17 00:00:00 2001 From: Sindre Kjelsrud Date: Wed, 10 Jan 2024 13:31:02 +0100 Subject: [PATCH] :construction: working on test evaluation Co-authored-by: haraldnilsen Signed-off-by: Sindre Kjelsrud --- backend/cmd/main.go | 26 +++++++++++ backend/db/insert_evaluation.go | 48 +++++++++++++++++++++ frontend/src/api/postEvaluationData.ts | 20 +++++++++ frontend/src/routes/evaluation/+page.svelte | 35 +++++++++++++++ 4 files changed, 129 insertions(+) create mode 100644 backend/db/insert_evaluation.go create mode 100644 frontend/src/api/postEvaluationData.ts create mode 100644 frontend/src/routes/evaluation/+page.svelte diff --git a/backend/cmd/main.go b/backend/cmd/main.go index 1d7f216..3763477 100644 --- a/backend/cmd/main.go +++ b/backend/cmd/main.go @@ -27,6 +27,10 @@ type BugReport struct { BugText string `json:"bugText"` } +type Evaluation struct { + EvaluationText string `json:"evaluationText"` +} + func main() { router := gin.Default() router.Use(cors.Default()) @@ -115,6 +119,28 @@ func main() { c.JSON(http.StatusOK, "Successfully submitted bug report!") }) + 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!") + }) + // Run the server on port 8080 router.Run(":8080") //db.SetupDb() diff --git a/backend/db/insert_evaluation.go b/backend/db/insert_evaluation.go new file mode 100644 index 0000000..e25b093 --- /dev/null +++ b/backend/db/insert_evaluation.go @@ -0,0 +1,48 @@ +package db + +import ( + "database/sql" + "fmt" + "log" + + _ "github.com/lib/pq" +) + +func InsertEvaluation(evaluationText string) (error) { + // 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 Evaluering (evalueringTekst) + VALUES ($1) + ` + stmt, err := db.Prepare(insertStatement) + if err != nil { + log.Fatalf("Error preparing statement: %v\n", err) + } + defer stmt.Close() + + _, err = stmt.Exec(evaluationText) + if err != nil { + log.Fatalf("Error executing statement: %v\n", err) + } + + fmt.Print("Inserted evaluation successfully") + + return nil +} \ No newline at end of file diff --git a/frontend/src/api/postEvaluationData.ts b/frontend/src/api/postEvaluationData.ts new file mode 100644 index 0000000..62ebf74 --- /dev/null +++ b/frontend/src/api/postEvaluationData.ts @@ -0,0 +1,20 @@ +export const postEvaluationData = (evaluationText: string) => { + let url = "http://localhost:8080/submiteval"; + + const response = fetch(url, { + method: "POST", + body: JSON.stringify({ + evaluationText: evaluationText, + }), + }) + .then((response) => { + if (!response.ok) { + throw new Error(`HTTP error! Status: ${response.status}`); + } + return response.ok; + }) + .catch((error) => { + console.log(error); + }); + return response; +}; diff --git a/frontend/src/routes/evaluation/+page.svelte b/frontend/src/routes/evaluation/+page.svelte new file mode 100644 index 0000000..5361b3f --- /dev/null +++ b/frontend/src/routes/evaluation/+page.svelte @@ -0,0 +1,35 @@ + + +
+
+
+

Takk for at du tok deg tid!

+

Tusen takk for hjelpen i denne undersøkelsen! Vi setter stor pris på det og håper du får en fin dag videre.

+

Har du tid så setter vi veldig pris på om du skrevet en liten tilbakemelding til oss i tekstfeltet under:

+
+ +
+ + +
+
+
\ No newline at end of file