Building Microservices in Go : Part 5(CRUD continued)

Hi everyone !!.
It’s been sometime since we last met. Hope everyone’s doing grand
Welcome to Part 5 in our series of building Micro-service using Go. I’ll list the previous parts here just in case you guys missed them
- Part 1 (Basic Structure Setup)
- Part 2 (Live Reload using Air)
- Part 3 (Database, Models, Migrations)
- Part 4 (Environment Variables, Validators, CRUD)
We are going to keep this one short and simple by finishing our implementation of CRUD operations. In the last part we talked about and configured environment variables plus validators and also created the AddAuthor endpoint and got that working. We also defined the basic signatures for the other CRUD endpoints so today we are going to go ahead and implement those.
We are going to start by implementing GetAllAuthors. We had a lot of steps when it came to adding an author but this is going to be pretty simple. We are going to create a QuerySetter object using the ORM layer provided by Beego and then use another utility by Beego to store the results without having to create a different struct. Let’s see how this looks in code
First we get our base response object and then we create our query setter object by specifying our base table and then specifying the order by condition. The “-” before the created_at just means that we want the records to be sorted in descending order. After that we create an object of Params which basically allows us to specify the field we need instead of creating a separate struct to hold those records.
In the next line we pass the maps object to the Values function of query setter which basically populates the provided variable with the fields that we specify next to it. In this case we are just fetching the ID and Title of authors. Values returns two values, one is the count of the records and the other is the error value. We check if there are any errors and if there aren’t any then we pass the maps object and the count value to our response object and send that back to the user. Let’s test this out on Postman

Let’s move to the GetAuthorByID endpoint. In our signature we specified that we will pass an ID as a query parameter to GetAuthorByID. So the first thing that we need to do in our route function is get the Id from params and convert it into int. We do that by using the Params function of our context variable and passing it the key that we specified in our route signature. Next we’ll create an author object using the Id that we got from params. We will look for an author record using this object and if we come across any error we will send that back in our response. One more thing to do here will be to check the error type so we can also cater to the record not found situation.
This sounds like a lot but trust me it will make more sense when we turn these steps into code. Let’s see what that looks like
Now let’s test this out on Postman

Now before we start implementing delete and update there’s one piece of optimization that we need to do. As programmers the worst thing (in my honest opinion) that we can do is repeat ourselves. For all our call that requires a post body we do two things, we check if the post body is a valid JSON and if it has all the fields that we need. Now instead of doing this in all our routes we are simply going to create a function in our utilities.go file and use that everywhere we need to validate a post body. Let’s see what that looks like
Now for our delete and update endpoint let’s first define some validations struct in validators.go file
For delete we only need the ID of the record but for update we also need the value of title so we can use it to update the title of the author. Now let’s code the delete and author endpoints. I’ll add comments so it can make more sense but this is pretty much what we’ve been doing, nothing new here.
Now lets test this out on Postman

Now the update author endpoint
Let’s run the GetAllAuthors route on Postman

Now the update route

And GetAllAuthors again

Anndddd Voilaa !!! We got a minimal dockerized microservice up and running that can perform CRUD, read from .env files, use validators among other stuff. Now I don’t know about you guys but I am super proud of all of us for making it this far before calling it quits.
Programming can be very frustrating but it can be extremely satisfying at the same time. Whenever you guys come across something that feels like impossible at the time, I suggest that you take a small break and think about solving some other problem. I am sure that you’ll get the hang of it after sometime.
With that let’s end this part here and meet again for the next one where we will deploy our microservice to Google Cloud run.
You can find the full code here