Allow querying a single user by ID

This commit is contained in:
raul 2024-12-09 17:21:03 +01:00
parent 271d66216c
commit 2ab9b83b43
Signed by: raul
GPG Key ID: C1AA797073F17129
1 changed files with 14 additions and 0 deletions

View File

@ -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")