automatically create a new study chapter when the last one is deleted

report-discarder
Thibault Duplessis 2017-10-14 10:49:52 -05:00
parent 79d9f04357
commit 736a8d8483
2 changed files with 19 additions and 12 deletions

View File

@ -181,13 +181,13 @@ private[study] object ChapterMaker {
case class Data(
name: Chapter.Name,
game: Option[String],
variant: Option[String],
fen: Option[String],
pgn: Option[String],
orientation: String,
mode: String,
initial: Boolean
game: Option[String] = None,
variant: Option[String] = None,
fen: Option[String] = None,
pgn: Option[String] = None,
orientation: String = "white",
mode: String = ChapterMaker.Mode.Normal.key,
initial: Boolean = false
) extends ChapterData
case class EditData(

View File

@ -553,14 +553,21 @@ final class StudyApi(
Contribute(byUserId, study) {
chapterRepo.byIdAndStudy(chapterId, studyId) flatMap {
_ ?? { chapter =>
chapterRepo.orderedMetadataByStudy(studyId).flatMap {
case chaps if chaps.size > 1 => (study.position.chapterId == chapterId).?? {
chapterRepo.orderedMetadataByStudy(studyId).flatMap { chaps =>
// deleting the only chapter? Automatically create an empty one
if (chaps.size < 2) {
chapterMaker(study, ChapterMaker.Data(Chapter.Name("Chapter 1")), 1, byUserId) flatMap {
_ ?? { c =>
doAddChapter(study, c, sticky = true, uid) >> doSetChapter(study, c.id, uid)
}
}
} // deleting the current chapter? Automatically move to another one
else (study.position.chapterId == chapterId).?? {
chaps.find(_.id != chapterId) ?? { newChap =>
doSetChapter(study, newChap.id, uid)
}
} >> chapterRepo.delete(chapter.id)
case _ => funit
} >>- reloadChapters(study)
}
} >> chapterRepo.delete(chapter.id) >>- reloadChapters(study)
} >>- indexStudy(study)
}
}