✨ reportbug is "finished"
just need to update database! Co-authored-by: haraldnilsen <harald_998@hotmail.com> Signed-off-by: Sindre Kjelsrud <kjelsrudsindre@gmail.com>
This commit is contained in:
		
							parent
							
								
									deb7d05b98
								
							
						
					
					
						commit
						113b0fb4b7
					
				
					 6 changed files with 135 additions and 17 deletions
				
			
		|  | @ -23,6 +23,10 @@ type FormData struct { | |||
|     RespondentId       int    `json:"respondentID"` | ||||
| } | ||||
| 
 | ||||
| type BugReport struct { | ||||
|     BugText string `json:"bugText"` | ||||
| } | ||||
| 
 | ||||
| func main() { | ||||
|     router := gin.Default() | ||||
|     router.Use(cors.Default()) | ||||
|  | @ -89,6 +93,28 @@ func main() { | |||
| 		c.JSON(http.StatusOK, "Successfully inserted formdata!") | ||||
|     }) | ||||
| 
 | ||||
|     router.POST("/submitbug", func(c *gin.Context) { | ||||
|         var requestBody BugReport | ||||
| 
 | ||||
|         // 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.InsertBugReport(requestBody.BugText) | ||||
|         if err != nil { | ||||
|             fmt.Print(err) | ||||
|             c.JSON(http.StatusInternalServerError, gin.H{"error": "Unable to insert bug report"}) | ||||
|             return | ||||
|         } | ||||
| 
 | ||||
|         // Respond with a success message | ||||
|         c.JSON(http.StatusOK, "Successfully submitted bug report!") | ||||
|     }) | ||||
| 
 | ||||
|     // Run the server on port 8080 | ||||
|     router.Run(":8080") | ||||
|     //db.SetupDb() | ||||
|  |  | |||
							
								
								
									
										48
									
								
								backend/db/insert_bug_report.go
									
										
									
									
									
										Normal file
									
								
							
							
						
						
									
										48
									
								
								backend/db/insert_bug_report.go
									
										
									
									
									
										Normal file
									
								
							|  | @ -0,0 +1,48 @@ | |||
| package db | ||||
| 
 | ||||
| import ( | ||||
| 	"database/sql" | ||||
| 	"fmt" | ||||
| 	"log" | ||||
| 
 | ||||
| 	_ "github.com/lib/pq" | ||||
| ) | ||||
| 
 | ||||
| func InsertBugReport(bugText 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 FeilRapport (feilTekst) | ||||
| 	VALUES ($1) | ||||
| 	` | ||||
| 	stmt, err := db.Prepare(insertStatement) | ||||
| 	if err != nil { | ||||
| 		log.Fatalf("Error preparing statement: %v\n", err) | ||||
| 	} | ||||
| 	defer stmt.Close() | ||||
| 
 | ||||
| 	_, err = stmt.Exec(bugText) | ||||
| 	if err != nil { | ||||
| 		log.Fatalf("Error executing statement: %v\n", err) | ||||
| 	} | ||||
| 
 | ||||
| 	fmt.Print("Inserted bug successfully") | ||||
| 
 | ||||
| 	return nil | ||||
| } | ||||
							
								
								
									
										20
									
								
								frontend/src/api/postBugData.ts
									
										
									
									
									
										Normal file
									
								
							
							
						
						
									
										20
									
								
								frontend/src/api/postBugData.ts
									
										
									
									
									
										Normal file
									
								
							|  | @ -0,0 +1,20 @@ | |||
| export const postBugData = (bugText: string) => { | ||||
| 	let url = "http://localhost:8080/submitbug"; | ||||
| 
 | ||||
| 	const response = fetch(url, { | ||||
| 		method: "POST", | ||||
| 		body: JSON.stringify({ | ||||
| 			bugText: bugText, | ||||
| 		}), | ||||
| 	}) | ||||
| 		.then((response) => { | ||||
| 			if (!response.ok) { | ||||
| 				throw new Error(`HTTP error! Status: ${response.status}`); | ||||
| 			} | ||||
| 			return response.ok; | ||||
| 		}) | ||||
| 		.catch((error) => { | ||||
| 			console.log(error); | ||||
| 		}); | ||||
| 	return response; | ||||
| }; | ||||
|  | @ -13,7 +13,7 @@ | |||
|         <p class="text-sm">{formQuestion}</p> | ||||
|     </div> | ||||
|     <div class="flex justify-end pr-20 items-center w-1/3"> | ||||
|         <a class="-" href="/reporterror"> | ||||
|         <a class="-" href="/reportbug"> | ||||
|             <CircleExclamation width="16" height="16" />     | ||||
|         </a> | ||||
|     </div> | ||||
|  |  | |||
							
								
								
									
										40
									
								
								frontend/src/routes/reportbug/+page.svelte
									
										
									
									
									
										Normal file
									
								
							
							
						
						
									
										40
									
								
								frontend/src/routes/reportbug/+page.svelte
									
										
									
									
									
										Normal file
									
								
							|  | @ -0,0 +1,40 @@ | |||
| <script lang="ts"> | ||||
|     import { postBugData } from "../../api/postBugData"; | ||||
|     import { goto } from "$app/navigation"; | ||||
| 
 | ||||
|     let bugText:string = "" | ||||
| 
 | ||||
|     const handleBugSubmit = () => { | ||||
|         if (bugText) { | ||||
|             postBugData(bugText) | ||||
|             goto("javascript:history.back()") | ||||
|         } else { | ||||
|             console.log("error"); | ||||
|         } | ||||
|     } | ||||
| </script> | ||||
| 
 | ||||
| <div class="flex flex-col justify-between items-center h-full"> | ||||
|     <div class="flex flex-col gap-4 h-full mt-8"> | ||||
|         <div class="flex flex-col gap-4 px-96 items-center"> | ||||
|             <h1 class="text-3xl text-primary font-bold">Å nei!</h1> | ||||
|             <p>Har du funnet feil eller mangler, eller er det noe du ønsker å melde i fra om angående spørreundersøkelsen? Skriv det i tekstfeltet under. (Dette er selvfølgelig helt anonymt også!)</p> | ||||
|         </div> | ||||
|          | ||||
|         <div class="flex flex-col gap-4 justify-center items-center"> | ||||
|             <textarea bind:value={bugText} cols="30" rows="8" class="border-solid border-gray-400 border-2 p-3 md:text-l w-1/3" placeholder="Skriv feil 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={handleBugSubmit} | ||||
|             > | ||||
|                 Send feil | ||||
|             </button> | ||||
|         </div> | ||||
|            | ||||
|            | ||||
|     </div> | ||||
|     <div class="flex justify-center items-center gap-8 text-primary font-bold"> | ||||
|         <!-- svelte-ignore a11y-invalid-attribute --> | ||||
|         <button><a href="javascript:history.back()">Tilbake</a></button> | ||||
|     </div> | ||||
| </div> | ||||
|  | @ -1,16 +0,0 @@ | |||
| <div class="flex flex-col justify-between items-center h-full"> | ||||
|     <div class="flex h-full mt-8"> | ||||
|         <div class="flex flex-col items-center"> | ||||
|             <h1 class="text-3xl text-primary font-bold">Å nei!</h1> | ||||
|             <p>Har du funnet feil eller mangler, eller er det noe du ønsker å melde i fra om angående spørreundersøkelsen? Skriv det i tekstfeltet under.</p> | ||||
|         </div> | ||||
|     </div> | ||||
|     <div> | ||||
|         <form> | ||||
|             <input type="text"/> | ||||
|         </form> | ||||
|     </div> | ||||
|     <div class="flex justify-center items-center gap-8 text-primary font-bold"> | ||||
|         <button><a href="/">Tilbake</a></button> | ||||
|     </div> | ||||
| </div> | ||||
		Reference in a new issue
	
	 Sindre Kjelsrud
						Sindre Kjelsrud