Refactor: bind admin forms via @ModelAttribute DTOs; detekt fully default
This commit is contained in:
@@ -12,6 +12,15 @@ import ru.ysu.lms.common.BadRequestException
|
||||
import ru.ysu.lms.common.NotFoundException
|
||||
import java.util.UUID
|
||||
|
||||
data class ArticlePlacement(
|
||||
val title: String,
|
||||
val bodyMd: String,
|
||||
val chapterId: UUID?,
|
||||
val newChapterTitle: String?,
|
||||
val sectionId: UUID?,
|
||||
val newSectionTitle: String?,
|
||||
)
|
||||
|
||||
@Service
|
||||
class ContentAuthoringService(
|
||||
private val chapters: ContentChapterRepository,
|
||||
@@ -141,53 +150,46 @@ class ContentAuthoringService(
|
||||
|
||||
/** Создаёт статью; если раздела нет — создаёт главу/раздел по названиям. */
|
||||
@Transactional
|
||||
fun createArticlePlaced(
|
||||
title: String,
|
||||
bodyMd: String,
|
||||
chapterId: UUID?,
|
||||
newChapterTitle: String?,
|
||||
sectionId: UUID?,
|
||||
newSectionTitle: String?,
|
||||
): ContentArticleView {
|
||||
fun createArticlePlaced(placement: ArticlePlacement): ContentArticleView {
|
||||
val targetSection: ContentSection =
|
||||
when {
|
||||
sectionId != null && !newSectionTitle.isNullOrBlank() -> {
|
||||
placement.sectionId != null && !placement.newSectionTitle.isNullOrBlank() -> {
|
||||
val anchor =
|
||||
sections
|
||||
.findById(sectionId)
|
||||
.findById(placement.sectionId)
|
||||
.orElseThrow { NotFoundException("Раздел не найден") }
|
||||
val node = createSection(anchor.chapterId, newSectionTitle, null)
|
||||
val node = createSection(anchor.chapterId, placement.newSectionTitle, null)
|
||||
sections
|
||||
.findById(UUID.fromString(node.id))
|
||||
.orElseThrow { NotFoundException("Раздел не найден") }
|
||||
}
|
||||
sectionId != null ->
|
||||
placement.sectionId != null ->
|
||||
sections
|
||||
.findById(sectionId)
|
||||
.findById(placement.sectionId)
|
||||
.orElseThrow { NotFoundException("Раздел не найден") }
|
||||
!newSectionTitle.isNullOrBlank() -> {
|
||||
!placement.newSectionTitle.isNullOrBlank() -> {
|
||||
val chapter: ContentChapter =
|
||||
when {
|
||||
chapterId != null ->
|
||||
placement.chapterId != null ->
|
||||
chapters
|
||||
.findById(chapterId)
|
||||
.findById(placement.chapterId)
|
||||
.orElseThrow { NotFoundException("Глава не найдена") }
|
||||
!newChapterTitle.isNullOrBlank() -> {
|
||||
val node = createChapter(newChapterTitle, null)
|
||||
!placement.newChapterTitle.isNullOrBlank() -> {
|
||||
val node = createChapter(placement.newChapterTitle, null)
|
||||
chapters
|
||||
.findById(UUID.fromString(node.id))
|
||||
.orElseThrow { NotFoundException("Глава не найдена") }
|
||||
}
|
||||
else -> throw BadRequestException("Укажите главу")
|
||||
}
|
||||
val node = createSection(chapter.id, newSectionTitle, null)
|
||||
val node = createSection(chapter.id, placement.newSectionTitle, null)
|
||||
sections
|
||||
.findById(UUID.fromString(node.id))
|
||||
.orElseThrow { NotFoundException("Раздел не найден") }
|
||||
}
|
||||
else -> throw BadRequestException("Укажите раздел")
|
||||
}
|
||||
return createArticle(targetSection.id, title, bodyMd)
|
||||
return createArticle(targetSection.id, placement.title, placement.bodyMd)
|
||||
}
|
||||
|
||||
@Transactional
|
||||
|
||||
@@ -12,6 +12,7 @@ import org.springframework.web.bind.annotation.RequestMapping
|
||||
import org.springframework.web.bind.annotation.RequestParam
|
||||
import org.springframework.web.bind.annotation.RestController
|
||||
import org.springframework.web.multipart.MultipartFile
|
||||
import ru.ysu.lms.academic.content.ArticlePlacement
|
||||
import ru.ysu.lms.academic.content.ContentAuthoringService
|
||||
import ru.ysu.lms.academic.content.ContentImportResult
|
||||
import ru.ysu.lms.academic.content.ContentService
|
||||
@@ -170,12 +171,14 @@ class ContentAdminController(
|
||||
fun createArticle(
|
||||
@RequestBody body: ArticleCreatePayload,
|
||||
) = authoring.createArticlePlaced(
|
||||
title = body.title,
|
||||
bodyMd = body.bodyMd,
|
||||
chapterId = body.chapterId?.let { UUID.fromString(it) },
|
||||
newChapterTitle = body.newChapterTitle,
|
||||
sectionId = body.sectionId?.let { UUID.fromString(it) },
|
||||
newSectionTitle = body.newSectionTitle,
|
||||
ArticlePlacement(
|
||||
title = body.title,
|
||||
bodyMd = body.bodyMd,
|
||||
chapterId = body.chapterId?.let { UUID.fromString(it) },
|
||||
newChapterTitle = body.newChapterTitle,
|
||||
sectionId = body.sectionId?.let { UUID.fromString(it) },
|
||||
newSectionTitle = body.newSectionTitle,
|
||||
),
|
||||
)
|
||||
|
||||
@PutMapping("/articles/{id}")
|
||||
|
||||
Reference in New Issue
Block a user