-
Notifications
You must be signed in to change notification settings - Fork 76
Commit
- Loading branch information
There are no files selected for viewing
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 GitHub Actions / PHP 8.2
|
||
} | ||
|
||
/** | ||
* @return Resource | ||
* @throws \Illuminate\Auth\Access\AuthorizationException | ||
*/ | ||
public function upload(StoreDefaultThumbnail $request, Campaign $campaign) | ||
{ | ||
$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 GitHub Actions / PHP 8.2
|
||
'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 GitHub Actions / PHP 8.2
|
||
} | ||
|
||
/** | ||
* @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 GitHub Actions / PHP 8.2
Check failure on line 52 in app/Http/Controllers/Api/v1/DefaultThumbnailApiController.php GitHub Actions / PHP 8.2
|
||
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); | ||
} | ||
} |
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; | ||
} | ||
} |
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; | ||
} | ||
} |
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. |
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"]) | ||
|
||
; |