Allow querying a single user by ID
This commit is contained in:
parent
271d66216c
commit
2ab9b83b43
|
@ -98,10 +98,24 @@ func server() {
|
|||
r.GET("/", helloWorld)
|
||||
// TODO: Have fun creating new endpoints
|
||||
r.GET("/api/users", getUsers)
|
||||
r.GET("/api/user/:userid", getUser)
|
||||
|
||||
r.Run(":" + ListenPort)
|
||||
}
|
||||
|
||||
func getUser(c *gin.Context) {
|
||||
id := c.Param("userid")
|
||||
user := user{}
|
||||
err := db.QueryRow("SELECT * FROM alumnos WHERE id_alumno = "+id).Scan(&user.Id, &user.Name, &user.Surname1, &user.Surname2, &user.Email)
|
||||
if err != nil {
|
||||
e := fmt.Sprintf("SOMETHING BAD HAPPENED QUERYING THE DATABASE: %v\n", err)
|
||||
log.Print(e)
|
||||
c.String(http.StatusInternalServerError, e)
|
||||
return
|
||||
}
|
||||
c.JSON(http.StatusOK, user)
|
||||
}
|
||||
|
||||
func getUsers(c *gin.Context) {
|
||||
users := []user{}
|
||||
rows, err := db.Query("SELECT * FROM alumnos")
|
||||
|
|
Loading…
Reference in New Issue