Show whether request was successful or not via json
This commit is contained in:
parent
e13e9e2019
commit
b673009635
|
@ -143,46 +143,51 @@ func createUser(c *gin.Context) {
|
||||||
c.String(http.StatusOK, "Success!\n")
|
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) {
|
func getUser(c *gin.Context) {
|
||||||
id := c.Param("userid")
|
id := c.Param("userid")
|
||||||
user := user{}
|
user := user{}
|
||||||
dynStmt := `SELECT id_alumno,nombre,apellido1,apellido2,email FROM alumnos WHERE id_alumno = $1`
|
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)
|
err := db.QueryRow(dynStmt, id).Scan(&user.Id, &user.Name, &user.Surname1, &user.Surname2, &user.Email, &user.AccountType)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
if err == sql.ErrNoRows {
|
if err == sql.ErrNoRows {
|
||||||
c.String(http.StatusNotFound, "User not found")
|
c.IndentedJSON(http.StatusNotFound, setResponse("User not found", false))
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
e := fmt.Sprintf("SOMETHING BAD HAPPENED QUERYING THE DATABASE: %v\n", err)
|
e := fmt.Sprintf("SOMETHING BAD HAPPENED QUERYING THE DATABASE: %v\n", err)
|
||||||
log.Print(e)
|
log.Print(e)
|
||||||
c.String(http.StatusInternalServerError, e)
|
c.IndentedJSON(http.StatusInternalServerError, setResponse(e, false))
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
c.IndentedJSON(http.StatusOK, user)
|
c.IndentedJSON(http.StatusOK, setResponse(user, true))
|
||||||
}
|
}
|
||||||
|
|
||||||
func getUsers(c *gin.Context) {
|
func getUsers(c *gin.Context) {
|
||||||
users := []user{}
|
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 {
|
if err != nil {
|
||||||
e := fmt.Sprintf("SOMETHING BAD HAPPENED QUERYING THE DATABASE: %v\n", err)
|
e := fmt.Sprintf("SOMETHING BAD HAPPENED QUERYING THE DATABASE: %v\n", err)
|
||||||
log.Print(e)
|
log.Print(e)
|
||||||
c.String(http.StatusInternalServerError, e)
|
c.IndentedJSON(http.StatusInternalServerError, setResponse(e, false))
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
defer rows.Close()
|
defer rows.Close()
|
||||||
for rows.Next() {
|
for rows.Next() {
|
||||||
user := user{}
|
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 {
|
if err != nil {
|
||||||
e := fmt.Sprintf("SOMETHING BAD HAPPENED SCANNING THE ROWS: %v\n", err)
|
e := fmt.Sprintf("SOMETHING BAD HAPPENED SCANNING THE ROWS: %v\n", err)
|
||||||
log.Print(e)
|
log.Print(e)
|
||||||
c.String(http.StatusInternalServerError, e)
|
c.IndentedJSON(http.StatusInternalServerError, setResponse(e, false))
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
users = append(users, user)
|
users = append(users, user)
|
||||||
}
|
}
|
||||||
c.IndentedJSON(http.StatusOK, users)
|
c.IndentedJSON(http.StatusOK, setResponse(users, true))
|
||||||
}
|
}
|
||||||
|
|
||||||
func helloWorld(c *gin.Context) {
|
func helloWorld(c *gin.Context) {
|
||||||
|
|
|
@ -9,3 +9,8 @@ type user struct {
|
||||||
Password string `json:"password"`
|
Password string `json:"password"`
|
||||||
AccountType string `json:"type"`
|
AccountType string `json:"type"`
|
||||||
}
|
}
|
||||||
|
|
||||||
|
type response struct {
|
||||||
|
Contents any `json:"contents"`
|
||||||
|
Success bool `json:"success"`
|
||||||
|
}
|
||||||
|
|
Loading…
Reference in New Issue