Add button to add a book directly from a shelf view

This commit is contained in:
Christopher Wilkinson 2019-04-02 16:35:46 +01:00
parent c836862d89
commit faa3a8b842
5 changed files with 57 additions and 5 deletions

View File

@ -77,21 +77,42 @@ class BookController extends Controller
* Show the form for creating a new book. * Show the form for creating a new book.
* @return Response * @return Response
*/ */
public function create() public function create($shelfSlug = null)
{ {
if ($shelfSlug !== null) {
$bookshelf = $this->entityRepo->getBySlug('bookshelf', $shelfSlug);
$this->checkOwnablePermission('bookshelf-update', $bookshelf);
} else {
$bookshelf = null;
}
$this->checkPermission('book-create-all'); $this->checkPermission('book-create-all');
$this->setPageTitle(trans('entities.books_create')); $this->setPageTitle(trans('entities.books_create'));
return view('books.create'); return view('books.create', [
'bookshelf' => $bookshelf
]);
} }
/** /**
* Store a newly created book in storage. * Store a newly created book in storage.
* *
* @param Request $request * @param Request $request
* @param $shelfSlug
* @return Response * @return Response
*/ */
public function store(Request $request) public function store(Request $request, $shelfSlug = null)
{ {
if ($shelfSlug !== null) {
$bookshelf = $this->entityRepo->getBySlug('bookshelf', $shelfSlug);
$this->checkOwnablePermission('bookshelf-update', $bookshelf);
$shelfBooks = $this->entityRepo->getBookshelfChildren($bookshelf);
$shelfBookIds = $shelfBooks->pluck('id');
} else {
$bookshelf = null;
}
$this->checkPermission('book-create-all'); $this->checkPermission('book-create-all');
$this->validate($request, [ $this->validate($request, [
'name' => 'required|string|max:255', 'name' => 'required|string|max:255',
@ -99,6 +120,19 @@ class BookController extends Controller
]); ]);
$book = $this->entityRepo->createFromInput('book', $request->all()); $book = $this->entityRepo->createFromInput('book', $request->all());
Activity::add($book, 'book_create', $book->id); Activity::add($book, 'book_create', $book->id);
if ($bookshelf) {
$shelfBookIds = $shelfBookIds->toArray();
array_unshift($shelfBookIds, $book->id);
$shelfBookIds = implode(',', $shelfBookIds);
$this->entityRepo->updateShelfBooks($bookshelf, $shelfBookIds);
Activity::add($bookshelf, 'bookshelf_update');
return redirect($bookshelf->getUrl());
}
return redirect($book->getUrl()); return redirect($book->getUrl());
} }

View File

@ -17,7 +17,7 @@
<div class="content-wrap card"> <div class="content-wrap card">
<h1 class="list-heading">{{ trans('entities.books_create') }}</h1> <h1 class="list-heading">{{ trans('entities.books_create') }}</h1>
<form action="{{ baseUrl("/books") }}" method="POST" enctype="multipart/form-data"> <form action="{{ isset($bookshelf) ? $bookshelf->getUrl('/create-book') : baseUrl('/books') }}" method="POST" enctype="multipart/form-data">
@include('books.form') @include('books.form')
</form> </form>
</div> </div>

View File

@ -23,6 +23,12 @@
<hr> <hr>
<p class="text-muted italic mt-xl mb-m">{{ trans('entities.shelves_empty_contents') }}</p> <p class="text-muted italic mt-xl mb-m">{{ trans('entities.shelves_empty_contents') }}</p>
<div class="icon-list inline block"> <div class="icon-list inline block">
@if($currentUser->can('book-create-all'))
<a href="{{ $shelf->getUrl('/create-book') }}" class="icon-list-item text-bookshelf">
<span class="icon">@icon('add')</span>
<span>{{ trans('entities.books_create') }}</span>
</a>
@endif
@if(userCan('bookshelf-update', $shelf)) @if(userCan('bookshelf-update', $shelf))
<a href="{{ $shelf->getUrl('/edit') }}" class="icon-list-item text-bookshelf"> <a href="{{ $shelf->getUrl('/edit') }}" class="icon-list-item text-bookshelf">
<span class="icon">@icon('edit')</span> <span class="icon">@icon('edit')</span>

View File

@ -26,6 +26,8 @@ Route::group(['middleware' => 'auth'], function () {
Route::get('/{slug}/permissions', 'BookshelfController@showPermissions'); Route::get('/{slug}/permissions', 'BookshelfController@showPermissions');
Route::put('/{slug}/permissions', 'BookshelfController@permissions'); Route::put('/{slug}/permissions', 'BookshelfController@permissions');
Route::post('/{slug}/copy-permissions', 'BookshelfController@copyPermissions'); Route::post('/{slug}/copy-permissions', 'BookshelfController@copyPermissions');
Route::get('/{slug}/create-book', 'BookController@create');
Route::post('/{slug}/create-book', 'BookController@store');
}); });
Route::get('/create-book', 'BookController@create'); Route::get('/create-book', 'BookController@create');

View File

@ -99,9 +99,11 @@ class BookShelfTest extends TestCase
{ {
$shelf = Bookshelf::first(); $shelf = Bookshelf::first();
$resp = $this->asAdmin()->get($shelf->getUrl()); $resp = $this->asAdmin()->get($shelf->getUrl());
$resp->assertSee($shelf->getUrl('/create-book'));
$resp->assertSee($shelf->getUrl('/edit')); $resp->assertSee($shelf->getUrl('/edit'));
$resp->assertSee($shelf->getUrl('/permissions')); $resp->assertSee($shelf->getUrl('/permissions'));
$resp->assertSee($shelf->getUrl('/delete')); $resp->assertSee($shelf->getUrl('/delete'));
$resp->assertElementContains('a', 'Create New Book');
$resp->assertElementContains('a', 'Edit'); $resp->assertElementContains('a', 'Edit');
$resp->assertElementContains('a', 'Permissions'); $resp->assertElementContains('a', 'Permissions');
$resp->assertElementContains('a', 'Delete'); $resp->assertElementContains('a', 'Delete');
@ -148,6 +150,14 @@ class BookShelfTest extends TestCase
$this->assertDatabaseHas('bookshelves_books', ['bookshelf_id' => $shelf->id, 'book_id' => $booksToInclude[1]->id]); $this->assertDatabaseHas('bookshelves_books', ['bookshelf_id' => $shelf->id, 'book_id' => $booksToInclude[1]->id]);
} }
public function test_shelf_create_new_book()
{
$shelf = Bookshelf::first();
$resp = $this->asEditor()->get($shelf->getUrl('/create-book'));
$resp->assertSeeText('Create New Book');
}
public function test_shelf_delete() public function test_shelf_delete()
{ {
$shelf = Bookshelf::first(); $shelf = Bookshelf::first();