🚑 added respondentId to POST-endpoint
Co-authored-by: haraldnilsen <harald_998@hotmail.com> Signed-off-by: Sindre Kjelsrud <kjelsrudsindre@gmail.com>
This commit is contained in:
		
							parent
							
								
									d56893e9dd
								
							
						
					
					
						commit
						db0f1f8de0
					
				
					 5 changed files with 41 additions and 9 deletions
				
			
		| 
						 | 
				
			
			@ -2,9 +2,11 @@ package main
 | 
			
		|||
 | 
			
		||||
import (
 | 
			
		||||
	"fmt"
 | 
			
		||||
	"net/http"
 | 
			
		||||
 | 
			
		||||
	"helseveileder/db"
 | 
			
		||||
 | 
			
		||||
	"github.com/gin-contrib/cors"
 | 
			
		||||
	"github.com/gin-gonic/gin"
 | 
			
		||||
)
 | 
			
		||||
 | 
			
		||||
| 
						 | 
				
			
			@ -17,7 +19,8 @@ type FormData struct {
 | 
			
		|||
 | 
			
		||||
func main() {
 | 
			
		||||
    router := gin.Default()
 | 
			
		||||
 | 
			
		||||
    router.Use(cors.Default())
 | 
			
		||||
    
 | 
			
		||||
    // Info about user
 | 
			
		||||
    router.POST("/submitform", func(c *gin.Context) {
 | 
			
		||||
 | 
			
		||||
| 
						 | 
				
			
			@ -27,11 +30,22 @@ func main() {
 | 
			
		|||
            fmt.Print(err)
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        fmt.Print(requestBody)
 | 
			
		||||
        // Capture both the ID and error returned from InsertData
 | 
			
		||||
		respondentId, err := db.InsertData(requestBody.Age, requestBody.Education, requestBody.HealthcarePersonnel, requestBody.Gender)
 | 
			
		||||
		
 | 
			
		||||
        if err != nil {
 | 
			
		||||
			fmt.Print(err)
 | 
			
		||||
			c.JSON(http.StatusInternalServerError, gin.H{"error": "Unable to insert data"})
 | 
			
		||||
			return
 | 
			
		||||
		}
 | 
			
		||||
 | 
			
		||||
        db.InsertData(requestBody.Age, requestBody.Education, requestBody.HealthcarePersonnel, requestBody.Gender)
 | 
			
		||||
        // Respond with the ID of the newly inserted respondent
 | 
			
		||||
		c.JSON(http.StatusOK, gin.H{"respondentID": respondentId})
 | 
			
		||||
    })
 | 
			
		||||
 | 
			
		||||
    // Get questions & answers from database
 | 
			
		||||
    //router.GET("/")
 | 
			
		||||
 | 
			
		||||
    // Run the server on port 8080
 | 
			
		||||
    router.Run(":8080")
 | 
			
		||||
    //db.SetupDb()
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -8,7 +8,7 @@ import (
 | 
			
		|||
	_ "github.com/lib/pq"
 | 
			
		||||
)
 | 
			
		||||
 | 
			
		||||
func InsertData(age string, education string, healthcarepersonell bool, gender string) {
 | 
			
		||||
func InsertData(age string, education string, healthcarepersonell bool, gender string) (int, error) {
 | 
			
		||||
	// Connection string
 | 
			
		||||
	psqlInfo := fmt.Sprintf("host=%s port=%d user=%s "+
 | 
			
		||||
		"password=%s dbname=%s sslmode=disable",
 | 
			
		||||
| 
						 | 
				
			
			@ -30,6 +30,7 @@ func InsertData(age string, education string, healthcarepersonell bool, gender s
 | 
			
		|||
	insertStatement := `
 | 
			
		||||
	INSERT INTO Respondent (alder, utdanningsgrad, helsepersonell, kjønn)
 | 
			
		||||
	VALUES ($1, $2, $3, $4)
 | 
			
		||||
	RETURNING respondentID
 | 
			
		||||
	`
 | 
			
		||||
	
 | 
			
		||||
	stmt, err := db.Prepare(insertStatement)
 | 
			
		||||
| 
						 | 
				
			
			@ -38,10 +39,12 @@ func InsertData(age string, education string, healthcarepersonell bool, gender s
 | 
			
		|||
	}
 | 
			
		||||
	defer stmt.Close()
 | 
			
		||||
 | 
			
		||||
	_, err = stmt.Exec(age, education, healthcarepersonell, gender)
 | 
			
		||||
	var respondentID int
 | 
			
		||||
	err = stmt.QueryRow(age, education, healthcarepersonell, gender).Scan(&respondentID)
 | 
			
		||||
	if err != nil {
 | 
			
		||||
		log.Fatalf("Error executing statement: %v\n", err)
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	fmt.Println("Data inserted successfully")
 | 
			
		||||
	fmt.Printf("Data inserted successfully with respondentID: %d\n", respondentID)
 | 
			
		||||
	return respondentID, nil
 | 
			
		||||
}
 | 
			
		||||
| 
						 | 
				
			
			@ -7,6 +7,7 @@ require (
 | 
			
		|||
	github.com/chenzhuoyu/base64x v0.0.0-20230717121745-296ad89f973d // indirect
 | 
			
		||||
	github.com/chenzhuoyu/iasm v0.9.1 // indirect
 | 
			
		||||
	github.com/gabriel-vasile/mimetype v1.4.3 // indirect
 | 
			
		||||
	github.com/gin-contrib/cors v1.5.0 // indirect
 | 
			
		||||
	github.com/gin-contrib/sse v0.1.0 // indirect
 | 
			
		||||
	github.com/gin-gonic/gin v1.9.1 // indirect
 | 
			
		||||
	github.com/go-playground/locales v0.14.1 // indirect
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -13,6 +13,8 @@ github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSs
 | 
			
		|||
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
 | 
			
		||||
github.com/gabriel-vasile/mimetype v1.4.3 h1:in2uUcidCuFcDKtdcBxlR0rJ1+fsokWf+uqxgUFjbI0=
 | 
			
		||||
github.com/gabriel-vasile/mimetype v1.4.3/go.mod h1:d8uq/6HKRL6CGdk+aubisF/M5GcPfT7nKyLpA0lbSSk=
 | 
			
		||||
github.com/gin-contrib/cors v1.5.0 h1:DgGKV7DDoOn36DFkNtbHrjoRiT5ExCe+PC9/xp7aKvk=
 | 
			
		||||
github.com/gin-contrib/cors v1.5.0/go.mod h1:TvU7MAZ3EwrPLI2ztzTt3tqgvBCq+wn8WpZmfADjupI=
 | 
			
		||||
github.com/gin-contrib/sse v0.1.0 h1:Y/yl/+YNO8GZSjAhjMsSuLt29uWRFHdHYUb5lYOV9qE=
 | 
			
		||||
github.com/gin-contrib/sse v0.1.0/go.mod h1:RHrZQHXnP2xjPF+u1gW/2HnVO7nvIa9PG3Gm+fLHvGI=
 | 
			
		||||
github.com/gin-gonic/gin v1.9.1 h1:4idEAncQnU5cB7BeOkPtxjfCSye0AAm1R0RVIqJ+Jmg=
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -15,8 +15,20 @@ export const postFormData = (
 | 
			
		|||
			healthcare_personnel: personnel,
 | 
			
		||||
			gender: gender,
 | 
			
		||||
		}),
 | 
			
		||||
	}).catch((error) => {
 | 
			
		||||
		console.log(error);
 | 
			
		||||
	});
 | 
			
		||||
	})
 | 
			
		||||
		.then((response) => {
 | 
			
		||||
			if (!response.ok) {
 | 
			
		||||
				throw new Error(`HTTP error! Status: ${response.status}`);
 | 
			
		||||
			}
 | 
			
		||||
			return response.json();
 | 
			
		||||
		})
 | 
			
		||||
		.then((data) => {
 | 
			
		||||
			console.log(data);
 | 
			
		||||
			localStorage.setItem("RespondentId", data);
 | 
			
		||||
			return data;
 | 
			
		||||
		})
 | 
			
		||||
		.catch((error) => {
 | 
			
		||||
			console.log(error);
 | 
			
		||||
		});
 | 
			
		||||
	return response;
 | 
			
		||||
};
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
		Reference in a new issue