Refactor: extract helpers to satisfy detekt (throws, returns, loops)
This commit is contained in:
@@ -32,6 +32,21 @@ class LearnController(
|
||||
|
||||
private fun isAdmin(session: HttpSession): Boolean = (session.getAttribute(SessionKeys.USER) as? SessionUser)?.isAdmin == true
|
||||
|
||||
private fun treeContainsArticle(
|
||||
tree: ContentTreeNodeDto,
|
||||
chapter: String,
|
||||
section: String,
|
||||
article: String,
|
||||
): Boolean {
|
||||
val sec =
|
||||
tree.chapters
|
||||
.firstOrNull { it.slug == chapter }
|
||||
?.sections
|
||||
?.firstOrNull { it.slug == section }
|
||||
?: return false
|
||||
return sec.articles.any { it.slug == article }
|
||||
}
|
||||
|
||||
private fun sectionChoices(tree: ContentTreeNodeDto): List<Map<String, String>> =
|
||||
tree.chapters.flatMap { chapter ->
|
||||
chapter.sections.map { section ->
|
||||
@@ -103,9 +118,7 @@ class LearnController(
|
||||
): String {
|
||||
val admin = isAdmin(session)
|
||||
val tree: ContentTreeNodeDto = api.contentTree(token(session), all = admin)
|
||||
val ch = tree.chapters.firstOrNull { it.slug == chapter } ?: return "redirect:/learn"
|
||||
val sec = ch.sections.firstOrNull { it.slug == section } ?: return "redirect:/learn"
|
||||
if (sec.articles.none { it.slug == article }) return "redirect:/learn"
|
||||
if (!treeContainsArticle(tree, chapter, section, article)) return "redirect:/learn"
|
||||
|
||||
val a: ContentArticleDto =
|
||||
try {
|
||||
|
||||
Reference in New Issue
Block a user