Going for a ride

Photo by Jay Miller on Unsplash

Going for a ride

Creating POST for an activity

So this morning I went for a bike ride (not really, it was raining, but bear with me) and now I want to log it in SpinTrack®. But we can only get existing activities and we have no existing activities. So let's create our bike ride POST request with Gin.

⚠️ Don't drink and ride your bike.

Gorm helps us to just create a new activity, but we have to check if what we are sending is compatible with the database. And in comes Gin. Let's create our new controllers function.

func CreateNewActivity(c *gin.Context) {
   var activity models.Activity
   if err := c.ShouldBindJSON(&activity); err != nil {
      c.JSON(http.StatusBadRequest, gin.H{
         "error": err.Error(),
      })
      return
   }
   database.DB.Create(&activity)
   c.JSON(http.StatusOK, activity)
}

It's important to point out that the Request's body comes through the gin Context, so we can attribute to our activity variable through its address. But we still have to create the endpoint for this. In our routes file we'll simply add this to our HandleRequests function.

...
r.POST("/api/activities", controllers.CreateNewActivity)
...

Let's try and see if it works.

For GET methods, it's easy. Just use your browser. But for POST requests we have to send a body. So let's try our good ol' reliable Postman.

Postman — Dicas de produtividade. O objetivo desse post é dar alguma… | by  Filipe Ferreira | Medium

Postman is an API platform for building and using APIs. Postman simplifies each step of the API lifecycle and streamlines collaboration so you can create better APIs—faster.

Postman has a buttload of functionalities, but we are simply going to use it to make our HTTP requests for testing. Just a warning: To make HTTP requests for your localhost, you need to install Postman's desktop driver. Not a big deal.

Just to test our brand new API, let's just make a GET request. Which will return empty, because there are no activities, but it shows the status code as 200, which means success.

Cool. So let's log our first bike ride! According to my phone, I rode 4.2 km in 30 minutes. As a unit, I'm going to dictate that distance will be recorded in meters and the duration will be recorded in seconds. So that means my body will be:

{
    "userId": 1,
    "distance": 4200,
    "duration": 1800
}

I've been avoiding talking to you about it, but there is a user id in the body. Well, someday I won't be the only one who logs my exercise. I want to send it to my friends and eventually become a billionaire. For now, it has little use, so let's move along.

Our first POST request is a success!

Where did all these fields come from? Remember when we added gorm.Model to our Activity type? That's what it does. It timestamps the changes to the table row and creates a unique serial Id. That will be VERY useful in the future.

That means we now have activities in the database! So let's GET them.

Beautiful! It works! We now have an API fully connected to an AWS hosted Postgres database. What comes next? Let's figure it out together on the next one!

See you guys, and stay curious!

Check out my project repository!