DEV Community

Adam Miedema
Adam Miedema

Posted on • Originally published at Medium on

How to create a fully functional blog with Alpas, Kotlin, and Tailwind — part 2: setup service…

How to create a fully functional blog with Alpas, Kotlin, and Tailwind — part 2: setup service provider, markdown convertor, and controller

In this part 2 continuation of creating a fully functional blog with Alpas, Kotlin, and Tailwind series, we will be adding a service provider, use the Flexmark Java library to convert markdown to HTML, and build out our controller.

Step 4: Create service provider

steps continued from part 1

Service providers are classes that allow you to register and boot your services in a more controlled way in one central place. Our service provider will be simple and assist with making blogs.

  1. Use the ./alpas make:provider BlogServiceProvider command to create the service provider
  2. In the BlogServiceProvider file, add app.bind(Blogs(app.make())) to the override fun boot function
  3. Navigate to the HttpKernal file and add BlogServiceProvider::class to the list of providers

Let’s next code the logic to convert markdown to HTML as well as extract some blog post meta data.

💡 Check your progress against BlogServiceProvider

📄 Related Documentation

Step 5: Create a markdown convertor

Our blog will allow bloggers to format content using markdown. Once they save their changes, our code will convert the markdown content to HTML. We will also write some logic to read through post headers to extract meta data.

  1. Create a new directory under the Kotlin directory named actions
  2. Under the actions directory, create a new file named Markdown.kt
  3. Open Markdown.kt and add the code from Blogify repo on GitHub

You will notice that we import the Flexmark library. If you didn’t know it already, Kotlin is compatible with Java libraries — so we can natively pull in and use the best Java based modules out there!

📄 Related Documentation

Step 6: Add blog retrieval and pagination functions

We will need a way to retrieve a range of blogs, retrieve blog content, and retrieve blog metadata. And, pagination to page through results on the front-end of course! Let’s create those functions now before we build out the controller.

  1. Under the actions directory, create two new files —  GetBlogs.kt and Pagination.kt
  2. Open GetBlogs.kt and add the code from Blogify repo on GitHub
  3. Open Pagination.kt and add the code from Blogify repo on GitHub

Be sure to read through the code and comments to gain a good understanding of what is going on with the functions. Next up — let’s work on the controller!

Step 7: Create blog controller

We are super close — just a few more steps!

Now it’s time to setup our blog controller.

  1. Create a new controller using ./alpas make:controller BlogController
  2. In the newly created BlogController.kt file, copy and paste in the code from Blogify repo on GitHub

You may have noticed under the submit function we are referring to a BlogData validation rule. This is a custom rule that we will setup in the next step.

💡 Read through the code and comments to ensure a good understanding of what is going on in the controller

📄 Related Documentation

We made a lot of great progress towards creating a fully functional blog in Step 2. Step 3 will be 90% focused on the front-end. You can always jump ahead and look at the finished project on GitHub.

In part 3 of this series, we will work on beautifying your blog using Tailwind CSS!

Read part 1
Read part 3


Top comments (0)