🚧 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"` | ||||
| } | ||||
| 
 | ||||
| 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() | ||||
|  |  | |||
							
								
								
									
										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 | ||||
| } | ||||
		Reference in a new issue
	
	 Sindre Kjelsrud
						Sindre Kjelsrud