- $chapter = new Chapter();
- $chapter->book_id = $parentBook->id;
- $chapter->priority = (new BookContents($parentBook))->getLastPriority() + 1;
- $this->baseRepo->create($chapter, $input);
- Activity::addForEntity($chapter, ActivityType::CHAPTER_CREATE);
- return $chapter;
+ return (new DatabaseTransaction(function () use ($input, $parentBook) {
+ $chapter = new Chapter();
+ $chapter->book_id = $parentBook->id;
+ $chapter->priority = (new BookContents($parentBook))->getLastPriority() + 1;
+ $this->baseRepo->create($chapter, $input);
+ $this->baseRepo->updateDefaultTemplate($chapter, intval($input['default_template_id'] ?? null));
+ Activity::add(ActivityType::CHAPTER_CREATE, $chapter);
+
+ $this->baseRepo->sortParent($chapter);
+
+ return $chapter;
+ }))->run();