redirect to last chapter after importing a game into a study

pull/9027/head
Thibault Duplessis 2021-05-25 09:25:02 +02:00
parent 300259161c
commit d7b8712ba8
3 changed files with 15 additions and 3 deletions

View File

@ -292,7 +292,7 @@ final class Study(
) =
env.study.api.importGame(lila.study.StudyMaker.ImportGame(data), me) flatMap {
_.fold(notFound) { sc =>
Redirect(routes.Study.show(sc.study.id.value)).fuccess
Redirect(routes.Study.chapter(sc.study.id.value, sc.chapter.id.value)).fuccess
}
}

View File

@ -30,6 +30,9 @@ final class ChapterRepo(val coll: AsyncColl)(implicit
def firstByStudy(studyId: Study.Id): Fu[Option[Chapter]] =
coll(_.find($studyId(studyId)).sort($sort asc "order").one[Chapter])
private[study] def lastByStudy(studyId: Study.Id): Fu[Option[Chapter]] =
coll(_.find($studyId(studyId)).sort($sort desc "order").one[Chapter])
def existsByStudy(studyId: Study.Id): Fu[Boolean] =
coll(_ exists $studyId(studyId))

View File

@ -76,9 +76,18 @@ final class StudyApi(
}
def byIdWithFirstChapter(id: Study.Id): Fu[Option[Study.WithChapter]] =
byIdWithChapterFinder(id, chapterRepo firstByStudy id)
private[study] def byIdWithLastChapter(id: Study.Id): Fu[Option[Study.WithChapter]] =
byIdWithChapterFinder(id, chapterRepo lastByStudy id)
private def byIdWithChapterFinder(
id: Study.Id,
chapterFinder: => Fu[Option[Chapter]]
): Fu[Option[Study.WithChapter]] =
byId(id) flatMap {
_ ?? { study =>
chapterRepo.firstByStudy(study.id) map {
chapterFinder map {
_ ?? { Study.WithChapter(study, _).some }
} orElse byIdWithChapter(id)
}
@ -112,7 +121,7 @@ final class StudyApi(
studyId = study.id,
data = data.form.toChapterData,
sticky = study.settings.sticky
)(Who(user.id, Sri(""))) >> byIdWithChapter(studyId)
)(Who(user.id, Sri(""))) >> byIdWithLastChapter(studyId)
case _ => fuccess(none)
} orElse importGame(data.copy(form = data.form.copy(asStr = none)), user)
}) addEffect {