{{article.title}}
By {{article.author.firstname}}, on {{article.addedAt}}
{{article.headline}}
{{article.content}}
{{> footer}}
----
We update the `HtmlController` in order to render blog and article pages with the formatted date. `ArticleRepository` and `MarkdownConverter` constructor parameters will be automatically autowired since `HtmlController` has a single constructor (implicit `@Autowired`).
`src/main/kotlin/blog/HtmlController.kt`
[source,kotlin]
----
@Controller
class HtmlController(private val repository: ArticleRepository) {
@GetMapping("/")
fun blog(model: Model): String {
model["articles"] = repository.findAllByOrderByAddedAtDesc().map { it.render() }
return "blog"
}
@GetMapping("/article/{slug}")
fun article(@PathVariable slug: String, model: Model): String {
val article = repository
.findBySlug(slug)
?.render()
?: throw IllegalArgumentException("Wrong article slug provided")
model["title"] = article.title
model["article"] = article
return "article"
}
fun Article.render() = RenderedArticle(
slug,
title,
headline,
content,
author,
addedAt.format()
)
data class RenderedArticle(
val slug: String,
val title: String,
val headline: String,
val content: String,
val author: User,
val addedAt: String)
}
----
Then, we add data initialization to a new `BlogConfiguration` class.
`src/main/kotlin/blog/BlogConfiguration.kt`
[source,kotlin]
----
@Configuration
class BlogConfiguration {
@Bean
fun databaseInitializer(userRepository: UserRepository,
articleRepository: ArticleRepository) = ApplicationRunner {
val smaldini = userRepository.save(User("smaldini", "Stéphane", "Maldini"))
articleRepository.save(Article(
"Reactor Bismuth is out",
"Lorem ipsum",
"dolor sit amet",
smaldini
))
articleRepository.save(Article(
"Reactor Aluminium has landed",
"Lorem ipsum",
"dolor sit amet",
smaldini
))
}
}
----
And we also update the integration tests accordingly.
`src/test/kotlin/blog/IntegrationTests.kt`
[source,kotlin]
----
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
class IntegrationTests(@Autowired val restTemplate: TestRestTemplate) {
@BeforeAll
fun setup() {
println(">> Setup")
}
@Test
fun `Assert blog page title, content and status code`() {
println(">> Assert blog page title, content and status code")
val entity = restTemplate.getForEntity