🚧 working on get questions endpoint
Co-authored-by: Sindre Kjelsrud <kjelsrudsindre@gmail.com>
This commit is contained in:
		
							parent
							
								
									87a2914573
								
							
						
					
					
						commit
						191404a9ee
					
				
					 3 changed files with 71 additions and 7 deletions
				
			
		|  | @ -13,7 +13,7 @@ import ( | ||||||
| type FormData struct { | type FormData struct { | ||||||
|     Age                string `json:"age"` |     Age                string `json:"age"` | ||||||
|     Education          string `json:"education"` |     Education          string `json:"education"` | ||||||
|     HealthcarePersonnel bool   `json:"healthcare_personnel"` |     HealthcarePersonnel bool  `json:"healthcare_personnel"` | ||||||
|     Gender             string `json:"gender"` |     Gender             string `json:"gender"` | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
|  | @ -31,7 +31,7 @@ func main() { | ||||||
|         } |         } | ||||||
| 
 | 
 | ||||||
|         // Capture both the ID and error returned from InsertData |         // Capture both the ID and error returned from InsertData | ||||||
| 		respondentId, err := db.InsertData(requestBody.Age, requestBody.Education, requestBody.HealthcarePersonnel, requestBody.Gender) | 		respondentId, err := db.InsertUserData(requestBody.Age, requestBody.Education, requestBody.HealthcarePersonnel, requestBody.Gender) | ||||||
| 		 | 		 | ||||||
|         if err != nil { |         if err != nil { | ||||||
| 			fmt.Print(err) | 			fmt.Print(err) | ||||||
|  | @ -39,6 +39,8 @@ func main() { | ||||||
| 			return | 			return | ||||||
| 		} | 		} | ||||||
| 
 | 
 | ||||||
|  | 
 | ||||||
|  | 
 | ||||||
|         // Respond with the ID of the newly inserted respondent |         // Respond with the ID of the newly inserted respondent | ||||||
| 		c.JSON(http.StatusOK, gin.H{"respondentID": respondentId}) | 		c.JSON(http.StatusOK, gin.H{"respondentID": respondentId}) | ||||||
|     }) |     }) | ||||||
|  |  | ||||||
							
								
								
									
										61
									
								
								backend/db/get_user_questions.go
									
										
									
									
									
										Normal file
									
								
							
							
						
						
									
										61
									
								
								backend/db/get_user_questions.go
									
										
									
									
									
										Normal file
									
								
							|  | @ -0,0 +1,61 @@ | ||||||
|  | package db | ||||||
|  | 
 | ||||||
|  | import ( | ||||||
|  | 	"database/sql" | ||||||
|  | 	"fmt" | ||||||
|  | 	"log" | ||||||
|  | 
 | ||||||
|  | 	_ "github.com/lib/pq" | ||||||
|  | ) | ||||||
|  | 
 | ||||||
|  | type FormQuestion struct { | ||||||
|  | 	QuestionID int | ||||||
|  | 	QuestionText string | ||||||
|  | } | ||||||
|  | 
 | ||||||
|  | type QuestionAnswer struct { | ||||||
|  | 	AnswerID int | ||||||
|  | 	QuestionID int | ||||||
|  | 	IsChatGPT bool | ||||||
|  | 	AnswerText string | ||||||
|  | } | ||||||
|  | 
 | ||||||
|  | type UserQuestions struct { | ||||||
|  | 	Question FormQuestion | ||||||
|  | 	Answers [2]QuestionAnswer | ||||||
|  | } | ||||||
|  | 
 | ||||||
|  | func GetUserQuestions(respondendID int) ([5]UserQuestions, 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) | ||||||
|  | 	} | ||||||
|  | 	 | ||||||
|  | 	getQuestionsStatement := fmt.Sprintf("SELECT s.SpørsmålID, s.tekst"+ | ||||||
|  | 		"FROM Spørsmål s"+ | ||||||
|  | 		"LEFT JOIN Spørsmålsvar ss ON s.spørsmålID == ss.spørsmålID"+ | ||||||
|  | 		"LEFT JOIN SvarVurdering sv ON ss.svarID == sv.svarID AND sv.respondentID = :$respondendID"+ | ||||||
|  | 		"WHERE sv.vurderingID IS NULL"+ | ||||||
|  | 		"LIMIT 5",  | ||||||
|  | 		respondendID) | ||||||
|  | 
 | ||||||
|  | 		stmt, err := db.Prepare(getQuestionsStatement) | ||||||
|  | 		if err != nil { | ||||||
|  | 			log.Fatalf("Error preparing statement: %v\n", err) | ||||||
|  | 		} | ||||||
|  | 		defer stmt.Close() | ||||||
|  | 
 | ||||||
|  | } | ||||||
|  | @ -8,7 +8,7 @@ import ( | ||||||
| 	_ "github.com/lib/pq" | 	_ "github.com/lib/pq" | ||||||
| ) | ) | ||||||
| 
 | 
 | ||||||
| func InsertData(age string, education string, healthcarepersonell bool, gender string) (int, error) { | func InsertUserData(age string, education string, healthcarepersonell bool, gender string) (int, error) { | ||||||
| 	// Connection string | 	// Connection string | ||||||
| 	psqlInfo := fmt.Sprintf("host=%s port=%d user=%s "+ | 	psqlInfo := fmt.Sprintf("host=%s port=%d user=%s "+ | ||||||
| 		"password=%s dbname=%s sslmode=disable", | 		"password=%s dbname=%s sslmode=disable", | ||||||
|  | @ -28,9 +28,9 @@ func InsertData(age string, education string, healthcarepersonell bool, gender s | ||||||
| 	} | 	} | ||||||
| 
 | 
 | ||||||
| 	insertStatement := ` | 	insertStatement := ` | ||||||
| 	INSERT INTO Respondent (alder, utdanningsgrad, helsepersonell, kjønn) | 		INSERT INTO Respondent (alder, utdanningsgrad, helsepersonell, kjønn) | ||||||
| 	VALUES ($1, $2, $3, $4) | 		VALUES ($1, $2, $3, $4) | ||||||
| 	RETURNING respondentID | 		RETURNING respondentID | ||||||
| 	` | 	` | ||||||
| 	 | 	 | ||||||
| 	stmt, err := db.Prepare(insertStatement) | 	stmt, err := db.Prepare(insertStatement) | ||||||
|  | @ -42,9 +42,10 @@ func InsertData(age string, education string, healthcarepersonell bool, gender s | ||||||
| 	var respondentID int | 	var respondentID int | ||||||
| 	err = stmt.QueryRow(age, education, healthcarepersonell, gender).Scan(&respondentID) | 	err = stmt.QueryRow(age, education, healthcarepersonell, gender).Scan(&respondentID) | ||||||
| 	if err != nil { | 	if err != nil { | ||||||
| 		log.Fatalf("Error executing statement: %v\n", err) | 		log.Fatalf("Error inserting userdata: %v\n", err) | ||||||
| 	} | 	} | ||||||
| 
 | 
 | ||||||
| 	fmt.Printf("Data inserted successfully with respondentID: %d\n", respondentID) | 	fmt.Printf("Data inserted successfully with respondentID: %d\n", respondentID) | ||||||
|  | 
 | ||||||
| 	return respondentID, nil | 	return respondentID, nil | ||||||
| } | } | ||||||
		Reference in a new issue
	
	 haraldnilsen
						haraldnilsen