🚧 working on endpoint for useranswer submit
Co-authored-by: haraldnilsen <harald_998@hotmail.com> Signed-off-by: Sindre Kjelsrud <kjelsrudsindre@gmail.com>
This commit is contained in:
parent
7ffea02700
commit
9f113c1258
5 changed files with 107 additions and 4 deletions
|
@ -11,13 +11,18 @@ import (
|
|||
"github.com/gin-gonic/gin"
|
||||
)
|
||||
|
||||
type FormData struct {
|
||||
type UserformData struct {
|
||||
Age string `json:"age"`
|
||||
Education string `json:"education"`
|
||||
HealthcarePersonnel bool `json:"healthcare_personnel"`
|
||||
Gender string `json:"gender"`
|
||||
}
|
||||
|
||||
type FormData struct {
|
||||
FormAnswers string `json:"allFormAnswers"`
|
||||
RespondentId int `json:"respondentID"`
|
||||
}
|
||||
|
||||
func main() {
|
||||
router := gin.Default()
|
||||
router.Use(cors.Default())
|
||||
|
@ -25,7 +30,7 @@ func main() {
|
|||
// Info about user
|
||||
router.POST("/submituserform", func(c *gin.Context) {
|
||||
|
||||
var requestBody FormData
|
||||
var requestBody UserformData
|
||||
|
||||
if err := c.BindJSON(&requestBody); err != nil {
|
||||
fmt.Print(err)
|
||||
|
@ -71,6 +76,17 @@ func main() {
|
|||
if err := c.BindJSON(&requestBody); err != nil {
|
||||
fmt.Print(err)
|
||||
}
|
||||
|
||||
respondentID, err := db.InsertUserAnswers(requestBody.RespondentId, requestBody.FormAnswers)
|
||||
|
||||
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})
|
||||
})
|
||||
|
||||
// Run the server on port 8080
|
||||
|
|
55
backend/db/insert_user_answers.go
Normal file
55
backend/db/insert_user_answers.go
Normal file
|
@ -0,0 +1,55 @@
|
|||
package db
|
||||
|
||||
import (
|
||||
"database/sql"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"log"
|
||||
|
||||
_ "github.com/lib/pq"
|
||||
)
|
||||
|
||||
func InsertUserAnswers(respondentId int, allAnswers string) (int, error) {
|
||||
// Parse allAnswers from JSON string to map
|
||||
var allAnswersArray []string
|
||||
err := json.Unmarshal([]byte(allAnswers), &allAnswersArray)
|
||||
if err != nil {
|
||||
return 0, fmt.Errorf("invalid JSON format for allAnswers: %v", err)
|
||||
}
|
||||
|
||||
// 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 SvarVurdering (svarID, respondentID, kunnskap, empati, hjelpsomhet)
|
||||
VALUES ($1, $2, $3, $4, $5)
|
||||
`
|
||||
|
||||
tx, err := db.Begin()
|
||||
if err != nil {
|
||||
log.Fatalf("Error creating transaction: %v\n", err)
|
||||
}
|
||||
|
||||
err = tx.Commit()
|
||||
if err != nil {
|
||||
log.Fatalf("Transaction commit failed: %v\n", err)
|
||||
}
|
||||
|
||||
fmt.Printf("Data inserted successfully for respondentID: %d\n", respondentId)
|
||||
return respondentId, nil
|
||||
}
|
21
frontend/src/api/postFormData.ts
Normal file
21
frontend/src/api/postFormData.ts
Normal file
|
@ -0,0 +1,21 @@
|
|||
export const postFormData = (respondentID: number, allFormAnswers: string) => {
|
||||
let url = "http://localhost:8080/submitanswers";
|
||||
|
||||
const response = fetch(url, {
|
||||
method: "POST",
|
||||
body: JSON.stringify({
|
||||
respondentID: respondentID,
|
||||
allFormAnswers: allFormAnswers,
|
||||
}),
|
||||
})
|
||||
.then((response) => {
|
||||
if (!response.ok) {
|
||||
throw new Error(`HTTP error! Status: ${response.status}`);
|
||||
}
|
||||
return response.ok;
|
||||
})
|
||||
.catch((error) => {
|
||||
console.log(error);
|
||||
});
|
||||
return response;
|
||||
};
|
|
@ -28,7 +28,7 @@ export const postUserformData = (
|
|||
})
|
||||
.then((data) => {
|
||||
console.log(data);
|
||||
localStorage.setItem("RespondentId", data);
|
||||
localStorage.setItem("RespondentId", data.respondentID);
|
||||
return data;
|
||||
})
|
||||
.catch((error) => {
|
||||
|
|
|
@ -2,6 +2,7 @@
|
|||
import ArrowChevron from "../../svg/ArrowChevron.svelte";
|
||||
import ButtonComponent from "../../userform/inputs/ButtonComponent.svelte";
|
||||
import { goto } from "$app/navigation";
|
||||
import { postFormData } from "../../../api/postFormData";
|
||||
|
||||
export let questionNum:number
|
||||
export let answeredAll:boolean
|
||||
|
@ -13,6 +14,16 @@
|
|||
const gotoPrevPage = async (questionNum: number) => {
|
||||
goto(`${questionNum - 1}`)
|
||||
}
|
||||
|
||||
const handleFormSubmit = () => {
|
||||
let allFormAnswers:string | null = localStorage.getItem("allFormAnswers")
|
||||
let respondentID:string | null = localStorage.getItem("RespondentId")
|
||||
|
||||
if (allFormAnswers && respondentID) {
|
||||
postFormData(Number(respondentID), allFormAnswers)
|
||||
}
|
||||
}
|
||||
|
||||
</script>
|
||||
|
||||
<div class="flex justify-center items-center gap-8">
|
||||
|
@ -20,7 +31,7 @@
|
|||
<ArrowChevron width=16 direction="left"/>
|
||||
Forrige spørsmål
|
||||
</button>
|
||||
<button disabled={questionNum == 0 || questionNum % 4 != 0}
|
||||
<button disabled={questionNum == 0 || questionNum % 4 != 0} on:click={handleFormSubmit}
|
||||
class={`${questionNum == 0 || questionNum % 4 != 0 ? "hidden" : "bg-primary text-bg hover:bg-bg hover:text-primary"} font-bold uppercase border-primary border-2 rounded-full px-8 py-3`}>
|
||||
Send inn svar
|
||||
</button>
|
||||
|
|
Reference in a new issue