Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[API] add priority #4313

Merged
merged 5 commits into from
Aug 21, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions app/Api/ApiEntityListFormatter.php
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
2 changes: 2 additions & 0 deletions app/Entities/Controllers/ChapterApiController.php
Original file line number Diff line number Diff line change
Expand Up @@ -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'],
],
];

Expand Down
2 changes: 2 additions & 0 deletions app/Entities/Controllers/PageApiController.php
Original file line number Diff line number Diff line change
Expand Up @@ -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'],
Expand All @@ -29,6 +30,7 @@ class PageApiController extends ApiController
'html' => ['string'],
'markdown' => ['string'],
'tags' => ['array'],
'priority' => ['integer'],
],
];

Expand Down
2 changes: 1 addition & 1 deletion app/Entities/Repos/ChapterRepo.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down
2 changes: 1 addition & 1 deletion app/Entities/Repos/PageRepo.php
Original file line number Diff line number Diff line change
Expand Up @@ -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'));
Expand Down
6 changes: 4 additions & 2 deletions dev/api/requests/chapters-create.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,7 @@
"tags": [
{"name": "Category", "value": "Top Content"},
{"name": "Rating", "value": "Highest"}
]
}
],
"priority": 15
}
}
5 changes: 3 additions & 2 deletions dev/api/requests/chapters-update.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,6 @@
"tags": [
{"name": "Category", "value": "Kinda Good Content"},
{"name": "Rating", "value": "Medium"}
]
}
],
"priority": 15
}
5 changes: 3 additions & 2 deletions dev/api/requests/pages-create.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,6 @@
"tags": [
{"name": "Category", "value": "Not Bad Content"},
{"name": "Rating", "value": "Average"}
]
}
],
"priority": 15
}
5 changes: 3 additions & 2 deletions dev/api/requests/pages-update.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,6 @@
"tags": [
{"name": "Category", "value": "API Examples"},
{"name": "Rating", "value": "Alright"}
]
}
],
"priority": 15
}
32 changes: 32 additions & 0 deletions tests/Api/ChaptersApiTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down Expand Up @@ -137,6 +168,7 @@ public function test_update_endpoint()
'value' => 'freshtagval',
],
],
'priority' => 15,
];

$resp = $this->putJson($this->baseEndpoint . "/{$chapter->id}", $details);
Expand Down
34 changes: 34 additions & 0 deletions tests/Api/PagesApiTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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' => '<p>My new page content</p>',
'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();
Expand Down Expand Up @@ -207,6 +240,7 @@ public function test_update_endpoint()
'value' => 'freshtagval',
],
],
'priority' => 15,
];

$resp = $this->putJson($this->baseEndpoint . "/{$page->id}", $details);
Expand Down