Refactor: bind content admin forms via @ModelAttribute DTOs

This commit is contained in:
2026-09-27 22:06:09 +03:00
parent 9a2a4023b5
commit 4a8a10971c
2 changed files with 52 additions and 33 deletions

View File

@@ -3,6 +3,7 @@ package ru.ysu.lms.web.admin
import jakarta.servlet.http.HttpSession
import org.springframework.stereotype.Controller
import org.springframework.web.bind.annotation.GetMapping
import org.springframework.web.bind.annotation.ModelAttribute
import org.springframework.web.bind.annotation.PathVariable
import org.springframework.web.bind.annotation.PostMapping
import org.springframework.web.bind.annotation.RequestBody
@@ -91,19 +92,17 @@ class ContentAdminController(
@PostMapping("/{kind}/toggle-published")
fun togglePublished(
@PathVariable kind: String,
@RequestParam id: String,
@RequestParam published: Boolean,
@RequestParam(required = false) back: String?,
@ModelAttribute form: TogglePublishedForm,
session: HttpSession,
redirect: RedirectAttributes,
): String =
try {
api.setContentPublished(token(session), kind, id, published)
api.setContentPublished(token(session), kind, form.id, form.published)
redirect.addFlashAttribute(
"notice",
if (published) "Материал опубликован — теперь его видят студенты" else "Материал скрыт от студентов",
if (form.published) "Материал опубликован — теперь его видят студенты" else "Материал скрыт от студентов",
)
redirectBack(back, "redirect:/learn")
redirectBack(form.back, "redirect:/learn")
} catch (ex: ApiException) {
redirect.addFlashAttribute("error", ex.message)
"redirect:/learn"
@@ -112,16 +111,14 @@ class ContentAdminController(
@PostMapping("/articles/{id}/save")
fun save(
@PathVariable id: String,
@RequestParam title: String,
@RequestParam bodyMd: String,
@RequestParam(required = false) back: String?,
@ModelAttribute form: ArticleSaveForm,
session: HttpSession,
redirect: RedirectAttributes,
): String =
try {
api.updateContentArticle(token(session), id, title, bodyMd)
api.updateContentArticle(token(session), id, form.title, form.bodyMd)
redirect.addFlashAttribute("notice", "Статья сохранена")
redirectBack(back, "redirect:/admin/content")
redirectBack(form.back, "redirect:/admin/content")
} catch (ex: ApiException) {
redirect.addFlashAttribute("error", ex.message)
"redirect:/admin/content/articles/$id/edit"
@@ -195,17 +192,14 @@ class ContentAdminController(
@PostMapping("/sections/create")
fun createSection(
@RequestParam chapterId: String,
@RequestParam title: String,
@RequestParam(required = false) slug: String?,
@RequestParam(required = false) back: String?,
@ModelAttribute form: SectionCreateForm,
session: HttpSession,
redirect: RedirectAttributes,
): String =
try {
api.createSection(token(session), chapterId, title.trim(), slug)
redirect.addFlashAttribute("notice", "Раздел «${title.trim()}» создан")
redirectBack(back, "redirect:/admin/content")
api.createSection(token(session), form.chapterId, form.title.trim(), form.slug)
redirect.addFlashAttribute("notice", "Раздел «${form.title.trim()}» создан")
redirectBack(form.back, "redirect:/admin/content")
} catch (ex: ApiException) {
redirect.addFlashAttribute("error", ex.message)
"redirect:/admin/content"
@@ -252,28 +246,22 @@ class ContentAdminController(
@PostMapping("/articles/create")
fun createArticle(
@RequestParam(required = false) chapterId: String?,
@RequestParam(required = false) sectionId: String?,
@RequestParam(required = false) newChapterTitle: String?,
@RequestParam(required = false) newSectionTitle: String?,
@RequestParam title: String,
@RequestParam(required = false, defaultValue = "") bodyMd: String,
@RequestParam(required = false) back: String?,
@ModelAttribute form: ArticleCreateForm,
session: HttpSession,
redirect: RedirectAttributes,
): String =
try {
api.createArticle(
token(session),
chapterId,
sectionId,
newChapterTitle?.takeIf { it.isNotBlank() },
newSectionTitle?.takeIf { it.isNotBlank() },
title.trim(),
bodyMd,
form.chapterId,
form.sectionId,
form.newChapterTitle?.takeIf { it.isNotBlank() },
form.newSectionTitle?.takeIf { it.isNotBlank() },
form.title.trim(),
form.bodyMd,
)
redirect.addFlashAttribute("notice", "Статья «${title.trim()}» создана")
redirectBack(back, "redirect:/admin/content")
redirect.addFlashAttribute("notice", "Статья «${form.title.trim()}» создана")
redirectBack(form.back, "redirect:/admin/content")
} catch (ex: ApiException) {
redirect.addFlashAttribute("error", ex.message)
"redirect:/admin/content"

View File

@@ -0,0 +1,31 @@
package ru.ysu.lms.web.admin
/** Формы админки материалов: связываются через @ModelAttribute, имена полей = имена в шаблонах. */
data class TogglePublishedForm(
val id: String = "",
val published: Boolean = false,
val back: String? = null,
)
data class ArticleSaveForm(
val title: String = "",
val bodyMd: String = "",
val back: String? = null,
)
data class SectionCreateForm(
val chapterId: String = "",
val title: String = "",
val slug: String? = null,
val back: String? = null,
)
data class ArticleCreateForm(
val chapterId: String? = null,
val sectionId: String? = null,
val newChapterTitle: String? = null,
val newSectionTitle: String? = null,
val title: String = "",
val bodyMd: String = "",
val back: String? = null,
)