From b6730096352a610ef3dde9fa2a26cd216fcb79ed Mon Sep 17 00:00:00 2001 From: raul Date: Wed, 11 Dec 2024 12:36:02 +0100 Subject: [PATCH] Show whether request was successful or not via json --- cmd/serverFunc.go | 25 +++++++++++++++---------- cmd/structs.go | 5 +++++ 2 files changed, 20 insertions(+), 10 deletions(-) diff --git a/cmd/serverFunc.go b/cmd/serverFunc.go index 9aa19ef..329d68d 100644 --- a/cmd/serverFunc.go +++ b/cmd/serverFunc.go @@ -143,46 +143,51 @@ func createUser(c *gin.Context) { c.String(http.StatusOK, "Success!\n") } +func setResponse(content any, success bool) response { + msg := response{Contents: content, Success: success} + return msg +} + func getUser(c *gin.Context) { id := c.Param("userid") user := user{} - dynStmt := `SELECT id_alumno,nombre,apellido1,apellido2,email FROM alumnos WHERE id_alumno = $1` - err := db.QueryRow(dynStmt, id).Scan(&user.Id, &user.Name, &user.Surname1, &user.Surname2, &user.Email) + dynStmt := `SELECT id_usuario,nombre,apellido1,apellido2,email,rol FROM usuarios WHERE id_usuario = $1` + err := db.QueryRow(dynStmt, id).Scan(&user.Id, &user.Name, &user.Surname1, &user.Surname2, &user.Email, &user.AccountType) if err != nil { if err == sql.ErrNoRows { - c.String(http.StatusNotFound, "User not found") + c.IndentedJSON(http.StatusNotFound, setResponse("User not found", false)) return } e := fmt.Sprintf("SOMETHING BAD HAPPENED QUERYING THE DATABASE: %v\n", err) log.Print(e) - c.String(http.StatusInternalServerError, e) + c.IndentedJSON(http.StatusInternalServerError, setResponse(e, false)) return } - c.IndentedJSON(http.StatusOK, user) + c.IndentedJSON(http.StatusOK, setResponse(user, true)) } func getUsers(c *gin.Context) { users := []user{} - rows, err := db.Query("SELECT id_alumno,nombre,apellido1,apellido2,email FROM alumnos") + rows, err := db.Query("SELECT id_usuario,nombre,apellido1,apellido2,email,rol FROM usuarios") if err != nil { e := fmt.Sprintf("SOMETHING BAD HAPPENED QUERYING THE DATABASE: %v\n", err) log.Print(e) - c.String(http.StatusInternalServerError, e) + c.IndentedJSON(http.StatusInternalServerError, setResponse(e, false)) return } defer rows.Close() for rows.Next() { user := user{} - err = rows.Scan(&user.Id, &user.Name, &user.Surname1, &user.Surname2, &user.Email) + err = rows.Scan(&user.Id, &user.Name, &user.Surname1, &user.Surname2, &user.Email, &user.AccountType) if err != nil { e := fmt.Sprintf("SOMETHING BAD HAPPENED SCANNING THE ROWS: %v\n", err) log.Print(e) - c.String(http.StatusInternalServerError, e) + c.IndentedJSON(http.StatusInternalServerError, setResponse(e, false)) return } users = append(users, user) } - c.IndentedJSON(http.StatusOK, users) + c.IndentedJSON(http.StatusOK, setResponse(users, true)) } func helloWorld(c *gin.Context) { diff --git a/cmd/structs.go b/cmd/structs.go index 9856252..19619d6 100644 --- a/cmd/structs.go +++ b/cmd/structs.go @@ -9,3 +9,8 @@ type user struct { Password string `json:"password"` AccountType string `json:"type"` } + +type response struct { + Contents any `json:"contents"` + Success bool `json:"success"` +}