🚧 working on test evaluation
Co-authored-by: haraldnilsen <harald_998@hotmail.com> Signed-off-by: Sindre Kjelsrud <kjelsrudsindre@gmail.com>
This commit is contained in:
		
							parent
							
								
									113b0fb4b7
								
							
						
					
					
						commit
						ed474d4433
					
				
					 4 changed files with 129 additions and 0 deletions
				
			
		|  | @ -27,6 +27,10 @@ type BugReport struct { | ||||||
|     BugText string `json:"bugText"` |     BugText string `json:"bugText"` | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
|  | type Evaluation struct { | ||||||
|  |     EvaluationText string `json:"evaluationText"` | ||||||
|  | } | ||||||
|  | 
 | ||||||
| func main() { | func main() { | ||||||
|     router := gin.Default() |     router := gin.Default() | ||||||
|     router.Use(cors.Default()) |     router.Use(cors.Default()) | ||||||
|  | @ -115,6 +119,28 @@ func main() { | ||||||
|         c.JSON(http.StatusOK, "Successfully submitted bug report!") |         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 |     // Run the server on port 8080 | ||||||
|     router.Run(":8080") |     router.Run(":8080") | ||||||
|     //db.SetupDb() |     //db.SetupDb() | ||||||
|  |  | ||||||
							
								
								
									
										48
									
								
								backend/db/insert_evaluation.go
									
										
									
									
									
										Normal file
									
								
							
							
						
						
									
										48
									
								
								backend/db/insert_evaluation.go
									
										
									
									
									
										Normal file
									
								
							|  | @ -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 | ||||||
|  | } | ||||||
							
								
								
									
										20
									
								
								frontend/src/api/postEvaluationData.ts
									
										
									
									
									
										Normal file
									
								
							
							
						
						
									
										20
									
								
								frontend/src/api/postEvaluationData.ts
									
										
									
									
									
										Normal file
									
								
							|  | @ -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; | ||||||
|  | }; | ||||||
							
								
								
									
										35
									
								
								frontend/src/routes/evaluation/+page.svelte
									
										
									
									
									
										Normal file
									
								
							
							
						
						
									
										35
									
								
								frontend/src/routes/evaluation/+page.svelte
									
										
									
									
									
										Normal file
									
								
							|  | @ -0,0 +1,35 @@ | ||||||
|  | <script lang="ts"> | ||||||
|  |     import { postEvaluationData } from "../../api/postEvaluationData"; | ||||||
|  |     import { goto } from "$app/navigation"; | ||||||
|  | 
 | ||||||
|  |     let evaluationText:string = "" | ||||||
|  | 
 | ||||||
|  |     const handleEvaluationSubmit = () => { | ||||||
|  |         if (evaluationText) { | ||||||
|  |             postEvaluationData(evaluationText) | ||||||
|  |             goto("/") | ||||||
|  |         } else { | ||||||
|  |             console.log("error"); | ||||||
|  |         } | ||||||
|  |     } | ||||||
|  | </script> | ||||||
|  | 
 | ||||||
|  | <div class="flex flex-col justify-between items-center h-full"> | ||||||
|  |     <div class="flex flex-col gap-8 h-full mt-8"> | ||||||
|  |         <div class="flex flex-col gap-4 px-96 items-center"> | ||||||
|  |             <h1 class="text-3xl text-primary font-bold">Takk for at du tok deg tid!</h1> | ||||||
|  |             <p>Tusen takk for hjelpen i denne undersøkelsen! Vi setter stor pris på det og håper du får en fin dag videre.</p> | ||||||
|  |             <p>Har du tid så setter vi veldig pris på om du skrevet en liten tilbakemelding til oss i tekstfeltet under:</p> | ||||||
|  |         </div> | ||||||
|  |          | ||||||
|  |         <div class="flex flex-col gap-4 justify-center items-center"> | ||||||
|  |             <textarea bind:value={evaluationText} cols="30" rows="8" class="border-solid border-gray-400 border-2 p-3 md:text-l w-1/3" placeholder="Skriv evaluering her"></textarea> | ||||||
|  |             <button  | ||||||
|  |                 class="text-primary hover:bg-primary hover:text-bg font-bold border-primary border-2 rounded-full px-8 py-1" | ||||||
|  |                 on:click={handleEvaluationSubmit} | ||||||
|  |             > | ||||||
|  |                 Send evaluering | ||||||
|  |             </button> | ||||||
|  |         </div> | ||||||
|  |     </div> | ||||||
|  | </div> | ||||||
		Reference in a new issue
	
	 Sindre Kjelsrud
						Sindre Kjelsrud