Synchronize the code with the guide
This commit is contained in:
@@ -1,10 +1,12 @@
|
||||
package com.example.blog
|
||||
|
||||
import org.springframework.http.HttpStatus.*
|
||||
import org.springframework.stereotype.Controller
|
||||
import org.springframework.ui.Model
|
||||
import org.springframework.ui.set
|
||||
import org.springframework.web.bind.annotation.GetMapping
|
||||
import org.springframework.web.bind.annotation.PathVariable
|
||||
import org.springframework.web.server.ResponseStatusException
|
||||
|
||||
@Controller
|
||||
class HtmlController(private val repository: ArticleRepository,
|
||||
@@ -23,7 +25,7 @@ class HtmlController(private val repository: ArticleRepository,
|
||||
val article = repository
|
||||
.findBySlug(slug)
|
||||
?.render()
|
||||
?: throw IllegalArgumentException("Wrong article slug provided")
|
||||
?: throw ResponseStatusException(NOT_FOUND, "This article does not exist")
|
||||
model["title"] = article.title
|
||||
model["article"] = article
|
||||
return "article"
|
||||
|
||||
@@ -13,7 +13,7 @@ class ArticleController(private val repository: ArticleRepository) {
|
||||
|
||||
@GetMapping("/{slug}")
|
||||
fun findOne(@PathVariable slug: String) =
|
||||
repository.findBySlug(slug) ?: throw ResponseStatusException(NOT_FOUND, "Wrong article slug provided")
|
||||
repository.findBySlug(slug) ?: throw ResponseStatusException(NOT_FOUND, "This article does not exist")
|
||||
|
||||
}
|
||||
|
||||
@@ -25,5 +25,5 @@ class UserController(private val repository: UserRepository) {
|
||||
fun findAll() = repository.findAll()
|
||||
|
||||
@GetMapping("/{login}")
|
||||
fun findOne(@PathVariable login: String) = repository.findByLogin(login)
|
||||
fun findOne(@PathVariable login: String) = repository.findByLogin(login) ?: throw ResponseStatusException(NOT_FOUND, "This user does not exist")
|
||||
}
|
||||
|
||||
@@ -8,6 +8,6 @@ interface ArticleRepository : CrudRepository<Article, Long> {
|
||||
}
|
||||
|
||||
interface UserRepository : CrudRepository<User, Long> {
|
||||
fun findByLogin(login: String): User
|
||||
fun findByLogin(login: String): User?
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user