Allow querying all students from database

This commit is contained in:
raul 2024-12-09 10:42:31 +01:00
parent 699547584d
commit 37444f6e9b
Signed by: raul
GPG Key ID: C1AA797073F17129
2 changed files with 58 additions and 26 deletions

View File

@ -14,6 +14,8 @@ import (
var ( var (
ListenPort = "8080" ListenPort = "8080"
db *sql.DB
DB_Host string DB_Host string
DB_Port string DB_Port string
DB_User string DB_User string
@ -64,13 +66,14 @@ func getDBInfo() error {
return nil 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) 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 { if err != nil {
return nil, err return err
} }
return db, nil db = dba
return nil
} }
func setPort() { func setPort() {
@ -84,41 +87,69 @@ func server() {
log.SetPrefix("[DRAHOOT] ") log.SetPrefix("[DRAHOOT] ")
setPort() setPort()
if err := getDBInfo(); err != nil { 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) gin.SetMode(gin.ReleaseMode)
r := gin.Default() r := gin.Default()
r.GET("/", helloWorld) r.GET("/", helloWorld)
r.POST("/api/user", createUser) // TODO: Have fun creating new endpoints
r.GET("/api/users", getUsers)
r.Run(":" + ListenPort) r.Run(":" + ListenPort)
} }
func createUser(c *gin.Context) { func getUsers(c *gin.Context) {
var newUser user users := []user{}
if err := c.BindJSON(&newUser); err != nil { rows, err := db.Query("SELECT * FROM alumnos")
return
}
dynStmt := `INSERT INTO "alumnos"("nombre", "apellido1", "apellido2", "email") values($1, $2, $3, $4)`
db, err := openDB()
if err != nil { 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) c.String(http.StatusInternalServerError, e)
return return
} }
defer rows.Close()
_, e := db.Exec(dynStmt, newUser.Name, newUser.Surname1, newUser.Surname2, newUser.Email) for rows.Next() {
if e != nil { user := user{}
e := fmt.Sprintf("We couldn't complete your request because the database server imploded, more at 11: %v\n", err) err = rows.Scan(&user.Id, &user.Name, &user.Surname1, &user.Surname2, &user.Email)
c.String(http.StatusInternalServerError, e) if err != nil {
return 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)
} }
c.JSON(http.StatusOK, users)
fmt.Println(newUser)
c.String(http.StatusOK, "Everything's fine!\n")
} }
// 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) { func helloWorld(c *gin.Context) {
ua := c.Request.UserAgent() ua := c.Request.UserAgent()
message := fmt.Sprintf("Hello %v!\n", ua) message := fmt.Sprintf("Hello %v!\n", ua)

View File

@ -1,8 +1,9 @@
package cmd package cmd
type user struct { type user struct {
Name string `json:"name"` Id int `json:"id"`
Surname1 string `json:"fSurname"` Name string `json:"nombre"`
Surname2 string `json:"lSurname"` Surname1 string `json:"apellido1"`
Surname2 string `json:"apellido2"`
Email string `json:"email"` Email string `json:"email"`
} }