/**
* Create a new book in the system.
+ * The cover image of a book can be set by sending a file via an 'image' property within a 'multipart/form-data' request.
+ * If the 'image' property is null then the book cover image will be removed.
*
* @throws ValidationException
*/
public function create(Request $request)
{
$this->checkPermission('book-create-all');
- $requestData = $this->validate($request, $this->rules['create']);
+ $requestData = $this->validate($request, $this->rules()['create']);
$book = $this->bookRepo->create($requestData);
/**
* Update the details of a single book.
+ * The cover image of a book can be set by sending a file via an 'image' property within a 'multipart/form-data' request.
+ * If the 'image' property is null then the book cover image will be removed.
*
* @throws ValidationException
*/
$book = Book::visible()->findOrFail($id);
$this->checkOwnablePermission('book-update', $book);
- $requestData = $this->validate($request, $this->rules['update']);
+ $requestData = $this->validate($request, $this->rules()['update']);
$book = $this->bookRepo->update($book, $requestData);
return response()->json($book);
* Create a new shelf in the system.
* An array of books IDs can be provided in the request. These
* will be added to the shelf in the same order as provided.
+ * The cover image of a shelf can be set by sending a file via an 'image' property within a 'multipart/form-data' request.
+ * If the 'image' property is null then the shelf cover image will be removed.
*
* @throws ValidationException
*/
public function create(Request $request)
{
$this->checkPermission('bookshelf-create-all');
- $requestData = $this->validate($request, $this->rules['create']);
+ $requestData = $this->validate($request, $this->rules()['create']);
$bookIds = $request->get('books', []);
$shelf = $this->bookshelfRepo->create($requestData, $bookIds);
* An array of books IDs can be provided in the request. These
* will be added to the shelf in the same order as provided and overwrite
* any existing book assignments.
+ * The cover image of a shelf can be set by sending a file via an 'image' property within a 'multipart/form-data' request.
+ * If the 'image' property is null then the shelf cover image will be removed.
*
* @throws ValidationException
*/
$shelf = Bookshelf::visible()->findOrFail($id);
$this->checkOwnablePermission('bookshelf-update', $shelf);
- $requestData = $this->validate($request, $this->rules['update']);
+ $requestData = $this->validate($request, $this->rules()['update']);
$bookIds = $request->get('books', null);
$shelf = $this->bookshelfRepo->update($shelf, $requestData, $bookIds);
use Carbon\Carbon;
use Illuminate\Support\Facades\DB;
use Tests\TestCase;
+use Tests\Uploads\UsesImages;
class BooksApiTest extends TestCase
{
use TestsApi;
+ use UsesImages;
protected string $baseEndpoint = '/api/books';
$this->assertGreaterThan(Carbon::now()->subDay()->unix(), $book->updated_at->unix());
}
+ public function test_update_cover_image_control()
+ {
+ $this->actingAsApiEditor();
+ /** @var Book $book */
+ $book = Book::visible()->first();
+ $this->assertNull($book->cover);
+ $file = $this->getTestImage('image.png');
+
+ // Ensure cover image can be set via API
+ $resp = $this->call('PUT', $this->baseEndpoint . "/{$book->id}", [
+ 'name' => 'My updated API book with image',
+ ], [], ['image' => $file]);
+ $book->refresh();
+
+ $resp->assertStatus(200);
+ $this->assertNotNull($book->cover);
+
+ // Ensure further updates without image do not clear cover image
+ $resp = $this->put($this->baseEndpoint . "/{$book->id}", [
+ 'name' => 'My updated book again'
+ ]);
+ $book->refresh();
+
+ $resp->assertStatus(200);
+ $this->assertNotNull($book->cover);
+
+ // Ensure update with null image property clears image
+ $resp = $this->put($this->baseEndpoint . "/{$book->id}", [
+ 'image' => null,
+ ]);
+ $book->refresh();
+
+ $resp->assertStatus(200);
+ $this->assertNull($book->cover);
+ }
+
public function test_delete_endpoint()
{
$this->actingAsApiEditor();
use Carbon\Carbon;
use Illuminate\Support\Facades\DB;
use Tests\TestCase;
+use Tests\Uploads\UsesImages;
class ShelvesApiTest extends TestCase
{
use TestsApi;
+ use UsesImages;
protected string $baseEndpoint = '/api/shelves';
$this->assertTrue($shelf->books()->count() === 0);
}
+ public function test_update_cover_image_control()
+ {
+ $this->actingAsApiEditor();
+ /** @var Book $shelf */
+ $shelf = Bookshelf::visible()->first();
+ $this->assertNull($shelf->cover);
+ $file = $this->getTestImage('image.png');
+
+ // Ensure cover image can be set via API
+ $resp = $this->call('PUT', $this->baseEndpoint . "/{$shelf->id}", [
+ 'name' => 'My updated API shelf with image',
+ ], [], ['image' => $file]);
+ $shelf->refresh();
+
+ $resp->assertStatus(200);
+ $this->assertNotNull($shelf->cover);
+
+ // Ensure further updates without image do not clear cover image
+ $resp = $this->put($this->baseEndpoint . "/{$shelf->id}", [
+ 'name' => 'My updated shelf again'
+ ]);
+ $shelf->refresh();
+
+ $resp->assertStatus(200);
+ $this->assertNotNull($shelf->cover);
+
+ // Ensure update with null image property clears image
+ $resp = $this->put($this->baseEndpoint . "/{$shelf->id}", [
+ 'image' => null,
+ ]);
+ $shelf->refresh();
+
+ $resp->assertStatus(200);
+ $this->assertNull($shelf->cover);
+ }
+
public function test_delete_endpoint()
{
$this->actingAsApiEditor();