Build REST API with Go Fiber and PlanetScale - Part 4

maful

Maful

Posted on October 19, 2022

Build REST API with Go Fiber and PlanetScale - Part 4

Update a user

Again, add a new function called UpdateUser in the users handler.

func UpdateUser(c *fiber.Ctx) error {
    // first, check if the user is exist
    user := models.User{}
    if err := models.DB.First(&user, "id = ?", c.Params("id")).Error; err != nil {
        return c.Status(http.StatusNotFound).JSON(&fiber.Map{
            "message": "Record not found!",
        })
    }

    // second, parse the request body
    request := &updateUserRequest{}
    if err := c.BodyParser(request); err != nil {
        return c.Status(http.StatusBadRequest).JSON(&fiber.Map{
            "message": err.Error(),
        })
    }

    // third, update the user
    updateUser := models.User{
        Name:    request.Name,
        Email:   request.Email,
        Website: request.Website,
    }
    models.DB.Model(&user).Updates(&updateUser)

    return c.Status(http.StatusOK).JSON(&fiber.Map{
        "user": user,
    })
}
Enter fullscreen mode Exit fullscreen mode

register update user to main.go

app.Put("/users/:id", handlers.UpdateUser)
Enter fullscreen mode Exit fullscreen mode

now, re-run the application. Update the user that we created before.

$ curl --location --request PUT 'http://localhost:3000/users/1' \
--header 'Content-Type: application/json' \
--data-raw '{
    "name": "Machine name"
}'
Enter fullscreen mode Exit fullscreen mode

response

{
  "user": {
    "ID": 1,
    "CreatedAt": "2021-09-08T08:07:25.042+07:00",
    "UpdatedAt": "2021-09-08T08:15:52.249+07:00",
    "DeletedAt": null,
    "name": "Machine name",
    "email": "joh@example.com",
    "website": "google.com"
  }
}
Enter fullscreen mode Exit fullscreen mode

when the user doesn't exist

$ curl --location --request PUT 'http://localhost:3000/users/100' \
--header 'Content-Type: application/json' \
--data-raw '{
    "name": "Machine name"
}'
Enter fullscreen mode Exit fullscreen mode

response

{
  "message": "Record not found!"
}
Enter fullscreen mode Exit fullscreen mode

Delete a user

Add the delete user function at the bottom of the users handler.

func DeleteUser(c *fiber.Ctx) error {
    // first, check if the user is exist
    user := models.User{}
    if err := models.DB.First(&user, "id = ?", c.Params("id")).Error; err != nil {
        return c.Status(http.StatusNotFound).JSON(&fiber.Map{
            "message": "Record not found!",
        })
    }

    // second, delete the user
    models.DB.Delete(&user)

    return c.Status(http.StatusOK).JSON(&fiber.Map{
        "message": "Success",
    })
}
Enter fullscreen mode Exit fullscreen mode

register the function

app.Delete("/users/:id", handlers.DeleteUser)
Enter fullscreen mode Exit fullscreen mode

so, create a new user again

$ curl --location --request POST 'http://localhost:3000/users' \
--header 'Content-Type: application/json' \
--data-raw '{
    "name": "Badu",
    "email": "joh@example.com",
    "website": "google.com"
}'
Enter fullscreen mode Exit fullscreen mode

see the id from the response, we will delete that user

$ curl --location --request DELETE 'http://localhost:3000/users/2'
Enter fullscreen mode Exit fullscreen mode

response

{
  "message": "Success"
}
Enter fullscreen mode Exit fullscreen mode

Summary

PlanetScale offers Developer plan pricing that you can use for the development lifecycle and it's completely FREE. You can create up to 3 databases and 3 branches for each database. Basically, this is going to be new knowledge for the developer who never use a serverless database and with a new workflow how to make a schema.

Fiber is a great web framework to build application in Go, they are fast, have rich features and the documentation is good.

This post, it's just a simple web API application to give a basic understanding of how to use Fiber and PlanetScale database. In the next one, we are going to build a more complex web API with the same tech stacks.

Download the full source code on this repository.

Thank you.

💖 💪 🙅 🚩
maful
Maful

Posted on October 19, 2022

Join Our Newsletter. No Spam, Only the good stuff.

Sign up to receive the latest update from our blog.

Related