CRUD REST API Using Vert.x and Redis in Kotlin

This time I am going to show you how to build CRUD using Redis and we build it in Kotlin using Vert.x. But before we start, I recommend you to check my previous article which is Build Simple REST API Using Vert.x in Kotlin, if you haven’t read it yet, because the current article is very related with the previous one.

Before you start, I would suggest you to copy the code and paste it to your favorite code editor then read it side by side with the explanation below. Hopefully it can help you to digest the code.

Reactive Redis client

First thing first we must add Redis client dependency in our build.gradle in order to call the Redis function on our code. You can see on line 45 in the codes below.

Modify HttpServerVerticle.kt to implement Redis client

We are going to add several stuff here. First, on line 20 and 21, we add the connection config then line 23 until 29 we connect it to Redis server.

Lets dig out from .getUsers() method on line 47 — anyway I use RxJava to get the method’s return in order to avoid the callback hell. You can learn about RxJava on its official website though.

On line 54, we call .rxKeys() method to get all keys with the matching pattern “heroes:*”. Then we transform the emitted values to Observable. We call .map() — line 57 — to process the values to become the data that we need.

Line 59 until 69, It itterate all keys, and we use each key to call .rxHmGet() to get the values inside the hash that associated with the key. Then we store it in ArrayList.

Line 72, we call .flatMap() to transform emitted values into something else, in this case, we transform into Observable that later we can be subscribed it. There is something that I am interested in which is on line 74, the Observable.concat(). This method can concatenate the list of Observable, so we can subscribe all the Observable that we have stored in ArrayList previously.

But you have to be careful when you call the .subscribe(). See line 76 until 95, I do some tricks. Why did I do it? because it will call the .subscribe() many times, depends on the size of ArrayList which is passed on to Observable.concat(). Then we send a response after we got all the emissions.

Lets jump to .updateUser() method. On line 154 we call .rxHmget() to get the hash values that are associated with the key “heroes:${userId}”. Then we transform the emission with .map(), we check it whether the data exists or doesn’t exist, and return it as boolean. After that — see line 164 — if data exista then we can update the hash using .rxHmset() method, otherwise just return Maybe.just(false). Then we subscribe it and send a response based on its result.

I think for other methods is obvious, you can just read it and can dig out by yourself.

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Mei Rizal F

Mei Rizal F

Software development engineer who have been loving to code since 2007