Skip to content

Commit

Permalink
API: Manage Thumbnails
Browse files Browse the repository at this point in the history
  • Loading branch information
spitfire305 committed Oct 12, 2023
1 parent 5089ba2 commit 37ea805
Show file tree
Hide file tree
Showing 9 changed files with 264 additions and 0 deletions.
69 changes: 69 additions & 0 deletions app/Http/Controllers/Api/v1/DefaultThumbnailApiController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
<?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\DefaultImageResource 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 response()->json($campaign->getDefaultImages());

Check failure on line 22 in app/Http/Controllers/Api/v1/DefaultThumbnailApiController.php

View workflow job for this annotation

GitHub Actions / PHP 8.2

Method App\Http\Controllers\Api\v1\DefaultThumbnailApiController::index() should return Illuminate\Http\Resources\Json\AnonymousResourceCollection but returns Illuminate\Http\JsonResponse.
}

/**
* @return Resource
* @throws \Illuminate\Auth\Access\AuthorizationException
*/
public function upload(StoreDefaultThumbnail $request, Campaign $campaign)

Check failure on line 29 in app/Http/Controllers/Api/v1/DefaultThumbnailApiController.php

View workflow job for this annotation

GitHub Actions / PHP 8.2

Method App\Http\Controllers\Api\v1\DefaultThumbnailApiController::upload() has invalid return type App\Http\Resources\DefaultImageResource.
{
$this->authorize('access', $campaign);

if ($campaign->premium()){
/** @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([

Check failure on line 39 in app/Http/Controllers/Api/v1/DefaultThumbnailApiController.php

View workflow job for this annotation

GitHub Actions / PHP 8.2

Method App\Http\Controllers\Api\v1\DefaultThumbnailApiController::upload() should return App\Http\Resources\DefaultImageResource but returns Illuminate\Http\JsonResponse.
'data' => 'Default thumbnail succesfully uploaded'
]);
}
}
return response()->json(['error' => 'Invalid input'], 422);

Check failure on line 44 in app/Http/Controllers/Api/v1/DefaultThumbnailApiController.php

View workflow job for this annotation

GitHub Actions / PHP 8.2

Method App\Http\Controllers\Api\v1\DefaultThumbnailApiController::upload() should return App\Http\Resources\DefaultImageResource but returns Illuminate\Http\JsonResponse.
}

/**
* @param Request $request
* @return \Illuminate\Http\JsonResponse
* @throws \Illuminate\Auth\Access\AuthorizationException
*/
public function delete(DestroyDefaultThumbnail $request, Campaign $campaign) {

Check failure on line 52 in app/Http/Controllers/Api/v1/DefaultThumbnailApiController.php

View workflow job for this annotation

GitHub Actions / PHP 8.2

PHPDoc tag @param for parameter $request with type App\Http\Controllers\Api\v1\Request is not subtype of native type App\Http\Requests\Campaigns\DestroyDefaultThumbnail.

Check failure on line 52 in app/Http/Controllers/Api/v1/DefaultThumbnailApiController.php

View workflow job for this annotation

GitHub Actions / PHP 8.2

Parameter $request of method App\Http\Controllers\Api\v1\DefaultThumbnailApiController::delete() has invalid type App\Http\Controllers\Api\v1\Request.
if ($campaign->premium()){
/** @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' => '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' => 'required|exists:entity_types,id',
'default_entity_image' => 'required|mimes:jpeg,png,jpg,gif,webp|max:' . Limit::upload(),
];
return $rules;
}
}
13 changes: 13 additions & 0 deletions app/Models/Campaign.php
Original file line number Diff line number Diff line change
Expand Up @@ -382,6 +382,19 @@ public function defaultImages(): array
return $data;
}

/**
* Prepare the default entity images
*/
public function getDefaultImages(): array
{
$data = [];
foreach($this->defaultImages() as $image) {
$data[] = ['entity_type' => $image['type'], 'url' => Img::url($image['path'])];
}

return $data;
}

/**
* Determine if a campaign has plugins of the theme type
*/
Expand Down
75 changes: 75 additions & 0 deletions resources/api-docs/1.0/default-images.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
# Images

---

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

<a name="all-images"></a>
## All Images

You can get a list of all the default images of a campaign by using the following endpoint. This is a superboosted campaign feature! If the campaign isn't superboosted, 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 Images](/api-docs/{{version}}/default-images)
- [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

0 comments on commit 37ea805

Please sign in to comment.