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 (
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,40 +87,68 @@ 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)
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
}
fmt.Println(newUser)
c.String(http.StatusOK, "Everything's fine!\n")
users = append(users, user)
}
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()

View File

@ -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"`
}