-
Notifications
You must be signed in to change notification settings - Fork 76
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #752 from owlchester/api-manage-thumbnails
API: Manage Thumbnails
- Loading branch information
Showing
9 changed files
with
283 additions
and
0 deletions.
There are no files selected for viewing
76 changes: 76 additions & 0 deletions
76
app/Http/Controllers/Api/v1/DefaultThumbnailApiController.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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']), | ||
]; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"]) | ||
|
||
; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters