diff --git a/cmd/serverFunc.go b/cmd/serverFunc.go index 97da120..5d6c899 100644 --- a/cmd/serverFunc.go +++ b/cmd/serverFunc.go @@ -14,6 +14,8 @@ import ( var ( ListenPort = "8080" + db *sql.DB + DB_Host string DB_Port string DB_User string @@ -64,13 +66,14 @@ func getDBInfo() error { return nil } -func openDB() (*sql.DB, error) { +func openDB() error { psqlconn := fmt.Sprintf("host=%s port=%s user=%s password=%s dbname=%s sslmode=disable", DB_Host, DB_Port, DB_User, DB_Pass, DB_Name) - db, err := sql.Open("postgres", psqlconn) + dba, err := sql.Open("postgres", psqlconn) if err != nil { - return nil, err + return err } - return db, nil + db = dba + return nil } func setPort() { @@ -84,41 +87,69 @@ func server() { log.SetPrefix("[DRAHOOT] ") setPort() if err := getDBInfo(); err != nil { - log.Fatalf("INVALID DB INFO: %v\nPlease refer to the example configuration file in the repo at https://git.bulgariu.xyz/raul/drahoot/src/branch/main/sample-config", err) + log.Fatalf("INVALID DB INFO: %v\nPlease refer to the example configuration file in the repo at https://git.bulgariu.xyz/raul/drahoot/src/branch/main/sample-config (default config path = ~/.config/drahoot/drahoot.toml)", err) + } + if err := openDB(); err != nil { + log.Fatalf("Database machine broke: %v\n", err) } gin.SetMode(gin.ReleaseMode) r := gin.Default() r.GET("/", helloWorld) - r.POST("/api/user", createUser) + // TODO: Have fun creating new endpoints + r.GET("/api/users", getUsers) r.Run(":" + ListenPort) } -func createUser(c *gin.Context) { - var newUser user - if err := c.BindJSON(&newUser); err != nil { - return - } - dynStmt := `INSERT INTO "alumnos"("nombre", "apellido1", "apellido2", "email") values($1, $2, $3, $4)` - db, err := openDB() +func getUsers(c *gin.Context) { + users := []user{} + rows, err := db.Query("SELECT * FROM alumnos") if err != nil { - e := fmt.Sprintf("We couldn't complete your request because the database server exploded, more at 11: %v\n", err) + e := fmt.Sprintf("SOMETHING BAD HAPPENED QUERYING THE DATABASE: %v\n", err) + log.Print(e) c.String(http.StatusInternalServerError, e) return } - - _, e := db.Exec(dynStmt, newUser.Name, newUser.Surname1, newUser.Surname2, newUser.Email) - if e != nil { - e := fmt.Sprintf("We couldn't complete your request because the database server imploded, more at 11: %v\n", err) - c.String(http.StatusInternalServerError, e) - return + defer rows.Close() + for rows.Next() { + user := user{} + err = rows.Scan(&user.Id, &user.Name, &user.Surname1, &user.Surname2, &user.Email) + if err != nil { + e := fmt.Sprintf("SOMETHING BAD HAPPENED SCANNING THE ROWS: %v\n", err) + log.Print(e) + c.String(http.StatusInternalServerError, e) + return + } + users = append(users, user) } - - fmt.Println(newUser) - c.String(http.StatusOK, "Everything's fine!\n") + c.JSON(http.StatusOK, users) } +// func createUser(c *gin.Context) { +// var newUser user +// if err := c.BindJSON(&newUser); err != nil { +// return +// } +// dynStmt := `INSERT INTO "alumnos"("nombre", "apellido1", "apellido2", "email") values($1, $2, $3, $4)` +// db, err := openDB() +// if err != nil { +// e := fmt.Sprintf("We couldn't complete your request because the database server exploded, more at 11: %v\n", err) +// c.String(http.StatusInternalServerError, e) +// return +// } +// +// _, e := db.Exec(dynStmt, newUser.Name, newUser.Surname1, newUser.Surname2, newUser.Email) +// if e != nil { +// e := fmt.Sprintf("We couldn't complete your request because the database server imploded, more at 11: %v\n", err) +// c.String(http.StatusInternalServerError, e) +// return +// } +// +// fmt.Println(newUser) +// c.String(http.StatusOK, "Everything's fine!\n") +// } + func helloWorld(c *gin.Context) { ua := c.Request.UserAgent() message := fmt.Sprintf("Hello %v!\n", ua) diff --git a/cmd/structs.go b/cmd/structs.go index 66e1983..3d81ead 100644 --- a/cmd/structs.go +++ b/cmd/structs.go @@ -1,8 +1,9 @@ package cmd type user struct { - Name string `json:"name"` - Surname1 string `json:"fSurname"` - Surname2 string `json:"lSurname"` + Id int `json:"id"` + Name string `json:"nombre"` + Surname1 string `json:"apellido1"` + Surname2 string `json:"apellido2"` Email string `json:"email"` }