Skip to content

Commit 4017048

Browse files
committed
Page Templates: Changed template field name, added API support
1 parent 7ebe7d4 commit 4017048

File tree

15 files changed

+67
-37
lines changed

15 files changed

+67
-37
lines changed

app/Entities/Controllers/BookApiController.php

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,9 @@
1414

1515
class BookApiController extends ApiController
1616
{
17-
protected BookRepo $bookRepo;
18-
19-
public function __construct(BookRepo $bookRepo)
20-
{
21-
$this->bookRepo = $bookRepo;
17+
public function __construct(
18+
protected BookRepo $bookRepo
19+
) {
2220
}
2321

2422
/**
@@ -58,7 +56,9 @@ public function create(Request $request)
5856
*/
5957
public function read(string $id)
6058
{
61-
$book = Book::visible()->with(['tags', 'cover', 'createdBy', 'updatedBy', 'ownedBy'])->findOrFail($id);
59+
$book = Book::visible()
60+
->with(['tags', 'cover', 'createdBy', 'updatedBy', 'ownedBy'])
61+
->findOrFail($id);
6262

6363
$contents = (new BookContents($book))->getTree(true, false)->all();
6464
$contentsApiData = (new ApiEntityListFormatter($contents))
@@ -116,12 +116,14 @@ protected function rules(): array
116116
'description' => ['string', 'max:1000'],
117117
'tags' => ['array'],
118118
'image' => array_merge(['nullable'], $this->getImageValidationRules()),
119+
'default_template_id' => ['nullable', 'integer'],
119120
],
120121
'update' => [
121122
'name' => ['string', 'min:1', 'max:255'],
122123
'description' => ['string', 'max:1000'],
123124
'tags' => ['array'],
124125
'image' => array_merge(['nullable'], $this->getImageValidationRules()),
126+
'default_template_id' => ['nullable', 'integer'],
125127
],
126128
];
127129
}

app/Entities/Controllers/BookController.php

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -92,11 +92,11 @@ public function store(Request $request, string $shelfSlug = null)
9292
{
9393
$this->checkPermission('book-create-all');
9494
$validated = $this->validate($request, [
95-
'name' => ['required', 'string', 'max:255'],
96-
'description' => ['string', 'max:1000'],
97-
'image' => array_merge(['nullable'], $this->getImageValidationRules()),
98-
'tags' => ['array'],
99-
'default_template' => ['nullable', 'integer'],
95+
'name' => ['required', 'string', 'max:255'],
96+
'description' => ['string', 'max:1000'],
97+
'image' => array_merge(['nullable'], $this->getImageValidationRules()),
98+
'tags' => ['array'],
99+
'default_template_id' => ['nullable', 'integer'],
100100
]);
101101

102102
$bookshelf = null;
@@ -167,11 +167,11 @@ public function update(Request $request, string $slug)
167167
$this->checkOwnablePermission('book-update', $book);
168168

169169
$validated = $this->validate($request, [
170-
'name' => ['required', 'string', 'max:255'],
171-
'description' => ['string', 'max:1000'],
172-
'image' => array_merge(['nullable'], $this->getImageValidationRules()),
173-
'tags' => ['array'],
174-
'default_template' => ['nullable', 'integer'],
170+
'name' => ['required', 'string', 'max:255'],
171+
'description' => ['string', 'max:1000'],
172+
'image' => array_merge(['nullable'], $this->getImageValidationRules()),
173+
'tags' => ['array'],
174+
'default_template_id' => ['nullable', 'integer'],
175175
]);
176176

177177
if ($request->has('image_reset')) {

app/Entities/Controllers/PageController.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -259,7 +259,7 @@ public function showDelete(string $bookSlug, string $pageSlug)
259259
$page = $this->pageRepo->getBySlug($bookSlug, $pageSlug);
260260
$this->checkOwnablePermission('page-delete', $page);
261261
$this->setPageTitle(trans('entities.pages_delete_named', ['pageName' => $page->getShortName()]));
262-
$usedAsTemplate = Book::query()->where('default_template', '=', $page->id)->count() > 0;
262+
$usedAsTemplate = Book::query()->where('default_template_id', '=', $page->id)->count() > 0;
263263

264264
return view('pages.delete', [
265265
'book' => $page->book,
@@ -279,7 +279,7 @@ public function showDeleteDraft(string $bookSlug, int $pageId)
279279
$page = $this->pageRepo->getById($pageId);
280280
$this->checkOwnablePermission('page-update', $page);
281281
$this->setPageTitle(trans('entities.pages_delete_draft_named', ['pageName' => $page->getShortName()]));
282-
$usedAsTemplate = Book::query()->where('default_template', '=', $page->id)->count() > 0;
282+
$usedAsTemplate = Book::query()->where('default_template_id', '=', $page->id)->count() > 0;
283283

284284
return view('pages.delete', [
285285
'book' => $page->book,

app/Entities/Models/Book.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
*
1616
* @property string $description
1717
* @property int $image_id
18-
* @property ?int $default_template
18+
* @property ?int $default_template_id
1919
* @property Image|null $cover
2020
* @property \Illuminate\Database\Eloquent\Collection $chapters
2121
* @property \Illuminate\Database\Eloquent\Collection $pages
@@ -78,7 +78,7 @@ public function coverImageTypeKey(): string
7878
*/
7979
public function defaultTemplate(): BelongsTo
8080
{
81-
return $this->belongsTo(Page::class, 'default_template');
81+
return $this->belongsTo(Page::class, 'default_template_id');
8282
}
8383

8484
/**

app/Entities/Repos/BookRepo.php

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,7 @@ public function create(array $input): Book
8686
$book = new Book();
8787
$this->baseRepo->create($book, $input);
8888
$this->baseRepo->updateCoverImage($book, $input['image'] ?? null);
89+
$this->updateBookDefaultTemplate($book, intval($input['default_template_id'] ?? null));
8990
Activity::add(ActivityType::BOOK_CREATE, $book);
9091

9192
return $book;
@@ -98,8 +99,8 @@ public function update(Book $book, array $input): Book
9899
{
99100
$this->baseRepo->update($book, $input);
100101

101-
if (array_key_exists('default_template', $input)) {
102-
$this->updateBookDefaultTemplate($book, intval($input['default_template']));
102+
if (array_key_exists('default_template_id', $input)) {
103+
$this->updateBookDefaultTemplate($book, intval($input['default_template_id']));
103104
}
104105

105106
if (array_key_exists('image', $input)) {
@@ -118,13 +119,13 @@ public function update(Book $book, array $input): Book
118119
*/
119120
protected function updateBookDefaultTemplate(Book $book, int $templateId): void
120121
{
121-
$changing = $templateId !== intval($book->default_template);
122+
$changing = $templateId !== intval($book->default_template_id);
122123
if (!$changing) {
123124
return;
124125
}
125126

126127
if ($templateId === 0) {
127-
$book->default_template = null;
128+
$book->default_template_id = null;
128129
$book->save();
129130
return;
130131
}
@@ -134,7 +135,7 @@ protected function updateBookDefaultTemplate(Book $book, int $templateId): void
134135
->where('id', '=', $templateId)
135136
->exists();
136137

137-
$book->default_template = $templateExists ? $templateId : null;
138+
$book->default_template_id = $templateExists ? $templateId : null;
138139
$book->save();
139140
}
140141

app/Entities/Tools/TrashCan.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -203,8 +203,8 @@ protected function destroyPage(Page $page): int
203203
}
204204

205205
// Remove book template usages
206-
Book::query()->where('default_template', '=', $page->id)
207-
->update(['default_template' => null]);
206+
Book::query()->where('default_template_id', '=', $page->id)
207+
->update(['default_template_id' => null]);
208208

209209
$page->forceDelete();
210210

database/migrations/2023_12_02_104541_add_default_template_to_books.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ class AddDefaultTemplateToBooks extends Migration
1414
public function up()
1515
{
1616
Schema::table('books', function (Blueprint $table) {
17-
$table->integer('default_template')->nullable()->default(null);
17+
$table->integer('default_template_id')->nullable()->default(null);
1818
});
1919
}
2020

@@ -26,7 +26,7 @@ public function up()
2626
public function down()
2727
{
2828
Schema::table('books', function (Blueprint $table) {
29-
$table->dropColumn('default_template');
29+
$table->dropColumn('default_template_id');
3030
});
3131
}
3232
}

dev/api/requests/books-create.json

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,9 @@
11
{
22
"name": "My own book",
3-
"description": "This is my own little book"
3+
"description": "This is my own little book",
4+
"default_template_id": 12,
5+
"tags": [
6+
{"name": "Category", "value": "Top Content"},
7+
{"name": "Rating", "value": "Highest"}
8+
]
49
}

dev/api/requests/books-update.json

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,8 @@
11
{
22
"name": "My updated book",
3-
"description": "This is my book with updated details"
3+
"description": "This is my book with updated details",
4+
"default_template_id": 12,
5+
"tags": [
6+
{"name": "Subject", "value": "Updates"}
7+
]
48
}

dev/api/responses/books-create.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
"created_by": 1,
77
"updated_by": 1,
88
"owned_by": 1,
9+
"default_template_id": 12,
910
"updated_at": "2020-01-12T14:05:11.000000Z",
1011
"created_at": "2020-01-12T14:05:11.000000Z"
1112
}

0 commit comments

Comments
 (0)