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: Manage Thumbnails #752

Merged
merged 6 commits into from
Oct 16, 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
76 changes: 76 additions & 0 deletions app/Http/Controllers/Api/v1/DefaultThumbnailApiController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
<?php

namespace App\Http\Controllers\Api\v1;

use App\Models\Campaign;
use App\Http\Requests\Campaigns\StoreDefaultThumbnail;
use App\Http\Requests\Campaigns\DestroyDefaultThumbnail;
use App\Http\Resources\EntityDefaultThumbnailResource as Resource;
use App\Services\Campaign\DefaultImageService;
use Illuminate\Support\Str;

class DefaultThumbnailApiController extends ApiController
{
/**
* @return \Illuminate\Http\Resources\Json\AnonymousResourceCollection
* @throws \Illuminate\Auth\Access\AuthorizationException
*/
public function index(Campaign $campaign)
{
$this->authorize('access', $campaign);

return Resource::collection($campaign->defaultImages());
}

/**
* @return Resource
* @throws \Illuminate\Auth\Access\AuthorizationException
*/
public function upload(StoreDefaultThumbnail $request, Campaign $campaign)
{
$this->authorize('access', $campaign);

if (!$campaign->boosted()) {
return response()->json(['error' => 'Feature exclusive to premium campaigns'], 422);
}

/** @var DefaultImageService $service */
$service = app()->make(DefaultImageService::class);
$type = Str::plural(array_search($request->post('entity_type'), config('entities.ids')));

if ($service->campaign($campaign)->type($type)->save($request)) {
return response()->json([
'data' => 'Default thumbnail succesfully uploaded'
]);
}

return response()->json(['error' => 'Invalid input'], 422);
}

/**
* @param Request $request
* @return \Illuminate\Http\JsonResponse
* @throws \Illuminate\Auth\Access\AuthorizationException
*/
public function delete(DestroyDefaultThumbnail $request, Campaign $campaign)
{
if (!$campaign->boosted()) {
return response()->json(['error' => 'Feature exclusive to premium campaigns'], 422);
}

/** @var DefaultImageService $service */
$service = app()->make(DefaultImageService::class);
$this->authorize('recover', $campaign);
$type = Str::plural(array_search($request->post('entity_type'), config('entities.ids')));

$result = $service->campaign($campaign)->type($type)->destroy();

if ($result) {
return response()->json([
'data' => 'Default thumbnail succesfully deleted'
]);
}

return response()->json(['error' => 'Invalid input'], 422);
}
}
31 changes: 31 additions & 0 deletions app/Http/Requests/Campaigns/DestroyDefaultThumbnail.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?php

namespace App\Http\Requests\Campaigns;

use Illuminate\Foundation\Http\FormRequest;

class DestroyDefaultThumbnail extends FormRequest
{
/**
* Determine if the user is authorized to make this request.
*
* @return bool
*/
public function authorize()
{
return true;
}

/**
* Get the validation rules that apply to the request.
*
* @return array
*/
public function rules()
{
$rules = [
'entity_type_id' => 'required|exists:entity_types,id',
];
return $rules;
}
}
33 changes: 33 additions & 0 deletions app/Http/Requests/Campaigns/StoreDefaultThumbnail.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?php

namespace App\Http\Requests\Campaigns;

use App\Facades\Limit;
use Illuminate\Foundation\Http\FormRequest;

class StoreDefaultThumbnail extends FormRequest
{
/**
* Determine if the user is authorized to make this request.
*
* @return bool
*/
public function authorize()
{
return true;
}

/**
* Get the validation rules that apply to the request.
*
* @return array
*/
public function rules()
{
$rules = [
'entity_type_id' => 'required|exists:entity_types,id',
'default_entity_image' => 'required|mimes:jpeg,png,jpg,gif,webp|max:' . Limit::upload(),
];
return $rules;
}
}
25 changes: 25 additions & 0 deletions app/Http/Resources/EntityDefaultThumbnailResource.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?php

namespace App\Http\Resources;

use Illuminate\Http\Resources\Json\JsonResource;
use App\Facades\Img;

class EntityDefaultThumbnailResource extends JsonResource
{
/**
* Transform the resource into an array.
*
* @param \Illuminate\Http\Request $request
* @return array
*/
public function toArray($request)
{
$resource = $this->resource;

return [
'entity_type' => $resource['type'],
'url' => Img::url($resource['path']),
];
}
}
75 changes: 75 additions & 0 deletions resources/api-docs/1.0/default-thumbnails.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
# Thumbnails

---

- [All Default Thumbnails](#all-thumbnails)
- [Create a Image](#create-image)
- [Delete a Image](#delete-image)

<a name="all-thumbnails"></a>
## All Thumbnails

You can get a list of all the default thumbnails of a campaign by using the following endpoint. This is a premium campaign feature! If the campaign isn't premium, this API endpoint will result in a 404.

> {warning} Don't forget that all endpoints documented here need to be prefixed with `{{version}}/campaigns/{campaign.id}/`.


| Method | URI | Headers |
| :- | :- | :- |
| GET/HEAD | `default-thumbnails` | Default |

### Results
```json
{
"data": [
{
"entity_type": "abilities",
"url": "https://th.kanka.io/gR8y1nxfEhBC1nVYdQpr2pUW3lY=/48x48/smart/src/app/logos/logo.png",
},
{
"entity_type": "creatures",
"url": "https://th.kanka.io/gR8y1nxfEhBC1nVYdQpr2pUW3lY=/48x48/smart/src/app/logos/logo.png",
}
]
}
```

<a name="create-image"></a>
## Create a Default Image

To create a default image, use the following endpoint.

| Method | URI | Headers |
| :- | :- | :- |
| POST | `default-thumbnails` | Default |

### Body

| Parameter | Type | Detail |
|:------------| :- | :- |
| `entity_type` | `integer`(required) | The entity type id |
| `default_entity_image` | `file` | File uploaded |


### Results

> {success} Code 200 with JSON.

<a name="delete-image"></a>
## Delete a Default Image

To delete a default image, use the following endpoint.

| Method | URI | Headers |
| :- | :- | :- |
| DELETE | `default-thumbnails` | Default |

### Body

| Parameter | Type | Detail |
|:------------| :- | :- |
| `entity_type` | `integer`(required) | The entity type id |

### Results

> {success} Code 200 with JSON.
1 change: 1 addition & 0 deletions resources/api-docs/1.0/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@
- [Pagination](/api-docs/{{version}}/pagination)
- [Bookmarks](/api-docs/{{version}}/bookmark)
- [Dashboard Widgets](/api-docs/{{version}}/dashboard-widgets)
- [Default Thumbnails](/api-docs/{{version}}/default-thumbnails)
- [Mention Language](/api-docs/{{version}}/mention-language)
- [Gallery](/api-docs/{{version}}/images)
- [Templates](/api-docs/{{version}}/templates)
Expand Down
4 changes: 4 additions & 0 deletions routes/api.v1.php
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,10 @@

Route::post('campaigns/{campaign}/transfer', [\App\Http\Controllers\Api\v1\EntityMoveApiController::class, 'transfer']);

Route::get('campaigns/{campaign}/default-thumbnails', [\App\Http\Controllers\Api\v1\DefaultThumbnailApiController::class, 'index']);
Route::post('campaigns/{campaign}/default-thumbnails', [\App\Http\Controllers\Api\v1\DefaultThumbnailApiController::class, 'upload']);
Route::delete('campaigns/{campaign}/default-thumbnails', [\App\Http\Controllers\Api\v1\DefaultThumbnailApiController::class, 'delete']);

Route::get('profile', [\App\Http\Controllers\Api\v1\ProfileApiController::class, 'index']);
Route::get('version', function () {
return config('app.version');
Expand Down
30 changes: 30 additions & 0 deletions tests/Feature/Entities/EntityDefaultThumbnailTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<?php

use Illuminate\Http\UploadedFile;

it('POSTS a new default thumbnail')
->asUser(true)
->withCampaign(['boost_count' => 4])
->postJson('/api/1.0/campaigns/1/default-thumbnails', [
'entity_type' => 2,
'default_entity_image' => UploadedFile::fake()->image('avatar.jpg')
])
->assertJsonFragment(["data" => "Default thumbnail succesfully uploaded"])
;

it('GETS all default thumbnails')
->asUser(true)
->withCampaign(['boost_count' => 4, 'default_images' => ["characters" => "1"]])
->withImages()
->get('/api/1.0/campaigns/1/default-thumbnails')
->assertStatus(200)
;

it('DELETES a default thumbnail')
->asUser(true)
->withCampaign(['boost_count' => 4, 'default_images' => ["characters" => "1"]])
->withImages()
->delete('/api/1.0/campaigns/1/default-thumbnails', ['entity_type' => 1])
->assertJsonFragment(["data" => "Default thumbnail succesfully deleted"])

;
8 changes: 8 additions & 0 deletions tests/TestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -472,6 +472,14 @@ public function withImages(array $extra = []): self
return $this;
}

public function withThumbnails(array $extra = []): self
{
Image::factory()
->count(1)
->create(['campaign_id' => 1] + $extra);
return $this;
}

public function withCharacterTags(array $extra = []): self
{
Character::factory()
Expand Down