The RSS reader tutorial (Step 3)

Now that Vert.x 3.6.0 has been released, it’s the perfect time to conclude our Vert.x Cassandra Client tutorial!

In the previous step, we have successfully implemented the second endpoint of the RSS reader app.

The RSS reader example assumes implementing three endpoints. This article is dedicated to implementing the last GET /articles/by_rss_link?link={rss_link} endpoint.

Before completing this step, make sure your are in the step_3 git branch:

$ git checkout step_3

Implementing the 3rd endpoint

The 3rd endpoint serves a list of articles related to a specific RSS channel. In a request, we specify RSS channel by providing a link. On the application side, after receiving a request we execute the following query:

SELECT title, article_link, description, pubDate FROM articles_by_rss_link WHERE rss_link = RSS_LINK_FROM_REQUEST ;

Implementation

For obtaining articles by RSS link we need to prepare a related statement first. Change AppVerticle#prepareSelectArticlesByRssLink in this way:

private Future<Void> prepareSelectArticlesByRssLink() {
    return Util.prepareQueryAndSetReference(client,
            "SELECT title, article_link, description, pubDate FROM articles_by_rss_link WHERE rss_link = ? ;",
            selectArticlesByRssLink
    );
}

And now, we can implement the AppVerticle#getArticles method. Basically, it will use the selectArticlesByRssLink statement for finding articles by the given link. Here’s the implementation:

private void getArticles(RoutingContext ctx) {
    String link = ctx.request().getParam("link");
    if (link == null) {
        responseWithInvalidRequest(ctx);
    } else {
        client.executeWithFullFetch(selectArticlesByRssLink.bind(link), handler -> {
            if (handler.succeeded()) {
                List<Row> rows = handler.result();
 
                JsonObject responseJson = new JsonObject();
                JsonArray articles = new JsonArray();
 
                rows.forEach(eachRow -> articles.add(
                        new JsonObject()
                                .put("title", eachRow.getString(0))
                                .put("link", eachRow.getString(1))
                                .put("description", eachRow.getString(2))
                                .put("pub_date", eachRow.getTimestamp(3).getTime())
                ));
 
                responseJson.put("articles", articles);
                ctx.response().end(responseJson.toString());
            } else {
                log.error("failed to get articles for " + link, handler.cause());
                ctx.response().setStatusCode(500).end("Unable to retrieve the info from C*");
            }
        });
    }
}

Conclusion

During the series, we have shown how the RSS reader app can be implemented with the Vert.x Cassandra client.

Thanks for reading this. I hope you enjoyed this series. See you soon on our Gitter channel!

Posted on 6 December 2018
in guides
2 min read

Related posts