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: Transfer to Campaign Endpoint #746

Merged
merged 7 commits into from
Oct 6, 2023
Merged
Show file tree
Hide file tree
Changes from 5 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
60 changes: 60 additions & 0 deletions app/Http/Controllers/Api/v1/EntityMoveApiController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
<?php

namespace App\Http\Controllers\Api\v1;

use App\Models\Campaign;
use App\Models\Entity;
use App\Http\Requests\MoveEntity as Request;
use App\Services\Entity\MoveService;
use Exception;

class EntityMoveApiController extends ApiController
{
protected MoveService $service;

public function __construct(MoveService $service)
{
$this->middleware('auth');

$this->service = $service;
}

/**
* @throws \Illuminate\Auth\Access\AuthorizationException
*/
public function transfer(Request $request, Campaign $campaign)
{
$this->authorize('access', $campaign);
$count = 0;
$copy = null === $request->copy ? false : $request->copy;
try {
foreach ($request->entities as $id) {
$entity = Entity::find($id);
if ($this->authorize('update', $entity->child)) {
$this->service
->entity($entity)
->campaign($campaign)
->user($request->user())
->to($request->campaign_id)
->copy($copy)
->validate()
->process()
;
$count++;
}
}

if ($copy) {
return response()->json(['success' => 'Succesfully copied ' . $count . ' entities.']);
}
return response()->json(['success' => 'Succesfully transfered ' . $count . ' entities.']);

} catch (Exception $e) {
spitfire305 marked this conversation as resolved.
Show resolved Hide resolved
return response()->json([
'error' => true,
'message' => $e->getMessage(),
]);
}

}
}
35 changes: 35 additions & 0 deletions app/Http/Requests/MoveEntity.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<?php

namespace App\Http\Requests;

use Illuminate\Foundation\Http\FormRequest;

class MoveEntity 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 = [
'entities' => 'array|required',
'entities.*' => 'distinct|exists:entities,id',
'campaign_id' => 'required|exists:campaigns,id',
'copy' => 'boolean'
];

return $rules;
}
}
22 changes: 21 additions & 1 deletion resources/api-docs/1.0/entities.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
- [Filtering Entities](#filtering-entities)
- [Related Entities](#related-entities)
- [Transform Entities](#transform-entities)
- [Transfer Entities](#transfer-entities)
- [Deleted Entities](#deleted-entities)
- [Recover Deleted Entities](#recover-entities)

Expand Down Expand Up @@ -200,7 +201,7 @@ Notice the new array objects `attributes`, `entity_files`, `entity_events`, `pos
<a name="transform-entities"></a>
## Transform Entities

You can post an array with the ids of the entities you want to transform to the `/transform` endpoint to undo transform them into a different entity type.
You can post an array with the ids of the entities you want to transform to the `/transform` endpoint to transform them into a different entity type.

| Method | URI | Headers |
| :- | :- | :- |
Expand All @@ -215,6 +216,25 @@ You can post an array with the ids of the entities you want to transform to the

> {success} Code 200 with JSON.

<a name="transfer-entities"></a>
## Transfer Entities

You can post an array with the ids of the entities you want to transfer to another campaign to the `/transfer` endpoint to transfer or copy them.

| Method | URI | Headers |
| :- | :- | :- |
| POST | `transfer` | Default |

| Parameter | Type | Description
| :- | :- | :- |
| `entities` | `array`(required) | The ids of the entities to transfer or copy. |
| `campaign_id` | `integer`(required) | The id of the campaign the entity will be transfered or copied to. |
| `copy` | `boolean` | True if the entity will be copied, false if the entity will be transfered, defaults to false if left empty |

### Result

> {success} Code 200 with JSON.

<a name="deleted-entities"></a>
## Deleted Entities

Expand Down
2 changes: 2 additions & 0 deletions routes/api.v1.php
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,8 @@

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

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

Route::get('profile', [\App\Http\Controllers\Api\v1\ProfileApiController::class, 'index']);
Route::get('version', function () {
return config('app.version');
Expand Down
27 changes: 27 additions & 0 deletions tests/Feature/Entities/EntityTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,3 +40,30 @@
->assertJsonFragment(['success' => 'Succesfully transformed 3 entities.'])
->assertStatus(200)
;

it('Transfers entities')
->asUser()
->withCampaign()
->withCharacters()
->withCampaigns()
->postJson('/api/1.0/campaigns/1/transfer', [
'entities' => [1,2,3],
'campaign_id' => 2,
])
->assertJsonFragment(['success' => 'Succesfully transfered 3 entities.'])
->assertStatus(200)
;

it('Copies entities')
->asUser()
->withCampaign()
->withCharacters()
->withCampaigns()
->postJson('/api/1.0/campaigns/1/transfer', [
'entities' => [1,2,3],
'campaign_id' => 2,
'copy' => true
])
->assertJsonFragment(['success' => 'Succesfully copied 3 entities.'])
->assertStatus(200)
;
6 changes: 6 additions & 0 deletions tests/TestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,12 @@ public function withCampaign(array $extra = []): self
return $this;
}

public function withCampaigns(array $extra = []): self
{
Campaign::factory()->create($extra);
return $this;
}

public function withPermissions(array $extra = []): self
{
CampaignRole::where('id', 3)->first()->toggle(1, 1);
Expand Down