Refactor: extract helpers to satisfy detekt (throws, returns, loops)

This commit is contained in:
2026-09-27 22:00:15 +03:00
parent b7d96d2ea4
commit 511df7977e
7 changed files with 168 additions and 92 deletions

View File

@@ -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 {