diff --git a/app/Api/ApiEntityListFormatter.php b/app/Api/ApiEntityListFormatter.php index c170ecf0c9d..7d00834e5d1 100644 --- a/app/Api/ApiEntityListFormatter.php +++ b/app/Api/ApiEntityListFormatter.php @@ -22,6 +22,7 @@ class ApiEntityListFormatter protected $fields = [ 'id', 'name', 'slug', 'book_id', 'chapter_id', 'draft', 'template', 'created_at', 'updated_at', + 'priority' ]; public function __construct(array $list) diff --git a/app/Entities/Controllers/ChapterApiController.php b/app/Entities/Controllers/ChapterApiController.php index 403c58de3c5..7f01e445a57 100644 --- a/app/Entities/Controllers/ChapterApiController.php +++ b/app/Entities/Controllers/ChapterApiController.php @@ -19,12 +19,14 @@ class ChapterApiController extends ApiController 'name' => ['required', 'string', 'max:255'], 'description' => ['string', 'max:1000'], 'tags' => ['array'], + 'priority' => ['integer'], ], 'update' => [ 'book_id' => ['integer'], 'name' => ['string', 'min:1', 'max:255'], 'description' => ['string', 'max:1000'], 'tags' => ['array'], + 'priority' => ['integer'], ], ]; diff --git a/app/Entities/Controllers/PageApiController.php b/app/Entities/Controllers/PageApiController.php index 0e8893450fe..d2947f1bb72 100644 --- a/app/Entities/Controllers/PageApiController.php +++ b/app/Entities/Controllers/PageApiController.php @@ -21,6 +21,7 @@ class PageApiController extends ApiController 'html' => ['required_without:markdown', 'string'], 'markdown' => ['required_without:html', 'string'], 'tags' => ['array'], + 'priority' => ['integer'], ], 'update' => [ 'book_id' => ['integer'], @@ -29,6 +30,7 @@ class PageApiController extends ApiController 'html' => ['string'], 'markdown' => ['string'], 'tags' => ['array'], + 'priority' => ['integer'], ], ]; diff --git a/app/Entities/Repos/ChapterRepo.php b/app/Entities/Repos/ChapterRepo.php index 588854c7e91..dadeec7f897 100644 --- a/app/Entities/Repos/ChapterRepo.php +++ b/app/Entities/Repos/ChapterRepo.php @@ -49,7 +49,7 @@ public function create(array $input, Book $parentBook): Chapter { $chapter = new Chapter(); $chapter->book_id = $parentBook->id; - $chapter->priority = (new BookContents($parentBook))->getLastPriority() + 1; + $chapter->priority = $chapter->priority ?: (new BookContents($parentBook))->getLastPriority() + 1; $this->baseRepo->create($chapter, $input); Activity::add(ActivityType::CHAPTER_CREATE, $chapter); diff --git a/app/Entities/Repos/PageRepo.php b/app/Entities/Repos/PageRepo.php index 521519dc08d..637f4133a90 100644 --- a/app/Entities/Repos/PageRepo.php +++ b/app/Entities/Repos/PageRepo.php @@ -164,7 +164,7 @@ public function publishDraft(Page $draft, array $input): Page $draft->draft = false; $draft->revision_count = 1; - $draft->priority = $this->getNewPriority($draft); + $draft->priority = $draft->priority ?: $this->getNewPriority($draft); $draft->save(); $this->revisionRepo->storeNewForPage($draft, trans('entities.pages_initial_revision')); diff --git a/dev/api/requests/chapters-create.json b/dev/api/requests/chapters-create.json index ca06fc29859..afd3c71f195 100644 --- a/dev/api/requests/chapters-create.json +++ b/dev/api/requests/chapters-create.json @@ -5,5 +5,7 @@ "tags": [ {"name": "Category", "value": "Top Content"}, {"name": "Rating", "value": "Highest"} - ] -} \ No newline at end of file + ], + "priority": 15 +} +} diff --git a/dev/api/requests/chapters-update.json b/dev/api/requests/chapters-update.json index 6bd3a3e5c09..f62d63d1e9c 100644 --- a/dev/api/requests/chapters-update.json +++ b/dev/api/requests/chapters-update.json @@ -5,5 +5,6 @@ "tags": [ {"name": "Category", "value": "Kinda Good Content"}, {"name": "Rating", "value": "Medium"} - ] -} \ No newline at end of file + ], + "priority": 15 +} diff --git a/dev/api/requests/pages-create.json b/dev/api/requests/pages-create.json index 1f53b42d489..5a21a80c753 100644 --- a/dev/api/requests/pages-create.json +++ b/dev/api/requests/pages-create.json @@ -5,5 +5,6 @@ "tags": [ {"name": "Category", "value": "Not Bad Content"}, {"name": "Rating", "value": "Average"} - ] -} \ No newline at end of file + ], + "priority": 15 +} diff --git a/dev/api/requests/pages-update.json b/dev/api/requests/pages-update.json index b9bfeb630de..fc8c73c0c39 100644 --- a/dev/api/requests/pages-update.json +++ b/dev/api/requests/pages-update.json @@ -5,5 +5,6 @@ "tags": [ {"name": "Category", "value": "API Examples"}, {"name": "Rating", "value": "Alright"} - ] -} \ No newline at end of file + ], + "priority": 15 +} diff --git a/tests/Api/ChaptersApiTest.php b/tests/Api/ChaptersApiTest.php index 713d8bba4ba..a99a85af816 100644 --- a/tests/Api/ChaptersApiTest.php +++ b/tests/Api/ChaptersApiTest.php @@ -61,6 +61,37 @@ public function test_create_endpoint() $this->assertActivityExists('chapter_create', $newItem); } + public function test_create_applies_correct_priority() + { + $this->actingAsApiEditor(); + $book = $this->entities->book(); + $details = [ + 'name' => 'My API chapter', + 'description' => 'A chapter created via the API', + 'book_id' => $book->id, + 'tags' => [ + [ + 'name' => 'tagname', + 'value' => 'tagvalue', + ], + ], + 'priority' => 15, + ]; + + $resp = $this->postJson($this->baseEndpoint, $details); + $resp->assertStatus(200); + $newItem = Chapter::query()->orderByDesc('id')->where('name', '=', $details['name'])->first(); + $resp->assertJson(array_merge($details, ['id' => $newItem->id, 'slug' => $newItem->slug])); + $this->assertDatabaseHas('tags', [ + 'entity_id' => $newItem->id, + 'entity_type' => $newItem->getMorphClass(), + 'name' => 'tagname', + 'value' => 'tagvalue', + ]); + $resp->assertJsonMissing(['pages' => []]); + $this->assertActivityExists('chapter_create', $newItem); + } + public function test_chapter_name_needed_to_create() { $this->actingAsApiEditor(); @@ -137,6 +168,7 @@ public function test_update_endpoint() 'value' => 'freshtagval', ], ], + 'priority' => 15, ]; $resp = $this->putJson($this->baseEndpoint . "/{$chapter->id}", $details); diff --git a/tests/Api/PagesApiTest.php b/tests/Api/PagesApiTest.php index 4a81f738bbd..a1f65692f05 100644 --- a/tests/Api/PagesApiTest.php +++ b/tests/Api/PagesApiTest.php @@ -63,6 +63,39 @@ public function test_create_endpoint() $this->assertActivityExists('page_create', $newItem); } + public function test_create_applies_correct_priority() + { + $this->actingAsApiEditor(); + $book = $this->entities->book(); + $details = [ + 'name' => 'My API page', + 'book_id' => $book->id, + 'html' => '
My new page content
', + 'tags' => [ + [ + 'name' => 'tagname', + 'value' => 'tagvalue', + ], + ], + 'priority' => 15, + ]; + + $resp = $this->postJson($this->baseEndpoint, $details); + unset($details['html']); + $resp->assertStatus(200); + $newItem = Page::query()->orderByDesc('id')->where('name', '=', $details['name'])->first(); + $resp->assertJson(array_merge($details, ['id' => $newItem->id, 'slug' => $newItem->slug])); + $this->assertDatabaseHas('tags', [ + 'entity_id' => $newItem->id, + 'entity_type' => $newItem->getMorphClass(), + 'name' => 'tagname', + 'value' => 'tagvalue', + ]); + $resp->assertSeeText('My new page content'); + $resp->assertJsonMissing(['book' => []]); + $this->assertActivityExists('page_create', $newItem); + } + public function test_page_name_needed_to_create() { $this->actingAsApiEditor(); @@ -207,6 +240,7 @@ public function test_update_endpoint() 'value' => 'freshtagval', ], ], + 'priority' => 15, ]; $resp = $this->putJson($this->baseEndpoint . "/{$page->id}", $details);