From bcce17873b4e6852e2666137e472304366130d80 Mon Sep 17 00:00:00 2001 From: Spitfire Date: Fri, 6 Oct 2023 13:04:19 -0600 Subject: [PATCH 1/5] API: Transfer to Campaign Endpoint --- .../Api/v1/EntityMoveApiController.php | 52 +++++++++++++++++++ app/Http/Requests/MoveEntity.php | 35 +++++++++++++ resources/api-docs/1.0/entities.md | 22 +++++++- routes/api.v1.php | 2 + tests/Feature/Entities/EntityTest.php | 27 ++++++++++ tests/TestCase.php | 6 +++ 6 files changed, 143 insertions(+), 1 deletion(-) create mode 100644 app/Http/Controllers/Api/v1/EntityMoveApiController.php create mode 100644 app/Http/Requests/MoveEntity.php diff --git a/app/Http/Controllers/Api/v1/EntityMoveApiController.php b/app/Http/Controllers/Api/v1/EntityMoveApiController.php new file mode 100644 index 0000000000..66d8a9b840 --- /dev/null +++ b/app/Http/Controllers/Api/v1/EntityMoveApiController.php @@ -0,0 +1,52 @@ +middleware('auth'); + + $this->service = $service; + } + + /** + * @throws \Illuminate\Auth\Access\AuthorizationException + */ + public function transfer(Request $request, Campaign $campaign) + { + $this->authorize('access', $campaign); + $count = 0; + $copy = is_null($request->copy) ? true : $request->copy; + + 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.']); + } +} diff --git a/app/Http/Requests/MoveEntity.php b/app/Http/Requests/MoveEntity.php new file mode 100644 index 0000000000..61a2281163 --- /dev/null +++ b/app/Http/Requests/MoveEntity.php @@ -0,0 +1,35 @@ + 'array|required', + 'entities.*' => 'distinct|exists:entities,id', + 'campaign_id' => 'required|exists:campaigns,id', + 'copy' => 'boolean' + ]; + + return $rules; + } +} diff --git a/resources/api-docs/1.0/entities.md b/resources/api-docs/1.0/entities.md index 8cc0beb122..e2d7702918 100644 --- a/resources/api-docs/1.0/entities.md +++ b/resources/api-docs/1.0/entities.md @@ -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) @@ -200,7 +201,7 @@ Notice the new array objects `attributes`, `entity_files`, `entity_events`, `pos ## 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 | | :- | :- | :- | @@ -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. + +## 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 copy if left empty | + +### Result + +> {success} Code 200 with JSON. + ## Deleted Entities diff --git a/routes/api.v1.php b/routes/api.v1.php index e5677b6e5c..7f6a091280 100644 --- a/routes/api.v1.php +++ b/routes/api.v1.php @@ -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'); diff --git a/tests/Feature/Entities/EntityTest.php b/tests/Feature/Entities/EntityTest.php index 821c9c7e38..159db35fc9 100644 --- a/tests/Feature/Entities/EntityTest.php +++ b/tests/Feature/Entities/EntityTest.php @@ -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, + 'copy' => false + ]) + ->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, + ]) + ->assertJsonFragment(['success' => 'Succesfully copied 3 entities.']) + ->assertStatus(200) +; \ No newline at end of file diff --git a/tests/TestCase.php b/tests/TestCase.php index 3ccde61e37..a49dd6c860 100644 --- a/tests/TestCase.php +++ b/tests/TestCase.php @@ -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); From 48de295aeca75c8d70e7e98cbb6b1aa9c7df6eeb Mon Sep 17 00:00:00 2001 From: spitfire305 Date: Fri, 6 Oct 2023 19:05:57 +0000 Subject: [PATCH 2/5] Fix styling --- .../Api/v1/EntityMoveApiController.php | 19 +++++++++---------- tests/Feature/Entities/EntityTest.php | 2 +- 2 files changed, 10 insertions(+), 11 deletions(-) diff --git a/app/Http/Controllers/Api/v1/EntityMoveApiController.php b/app/Http/Controllers/Api/v1/EntityMoveApiController.php index 66d8a9b840..70fe6fbe69 100644 --- a/app/Http/Controllers/Api/v1/EntityMoveApiController.php +++ b/app/Http/Controllers/Api/v1/EntityMoveApiController.php @@ -7,7 +7,6 @@ use App\Http\Requests\MoveEntity as Request; use App\Services\Entity\MoveService; - class EntityMoveApiController extends ApiController { protected MoveService $service; @@ -26,20 +25,20 @@ public function transfer(Request $request, Campaign $campaign) { $this->authorize('access', $campaign); $count = 0; - $copy = is_null($request->copy) ? true : $request->copy; + $copy = null === $request->copy ? true : $request->copy; 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() - ; + ->entity($entity) + ->campaign($campaign) + ->user($request->user()) + ->to($request->campaign_id) + ->copy($copy) + ->validate() + ->process() + ; $count++; } } diff --git a/tests/Feature/Entities/EntityTest.php b/tests/Feature/Entities/EntityTest.php index 159db35fc9..088c307d24 100644 --- a/tests/Feature/Entities/EntityTest.php +++ b/tests/Feature/Entities/EntityTest.php @@ -66,4 +66,4 @@ ]) ->assertJsonFragment(['success' => 'Succesfully copied 3 entities.']) ->assertStatus(200) -; \ No newline at end of file +; From 8d92c80b1ab1500e9ebe96b1b24df1fa6efcb9fb Mon Sep 17 00:00:00 2001 From: Spitfire Date: Fri, 6 Oct 2023 14:28:32 -0600 Subject: [PATCH 3/5] Implemented requested changes --- .../Api/v1/EntityMoveApiController.php | 48 +++++++++++-------- resources/api-docs/1.0/entities.md | 2 +- tests/Feature/Entities/EntityTest.php | 2 +- 3 files changed, 30 insertions(+), 22 deletions(-) diff --git a/app/Http/Controllers/Api/v1/EntityMoveApiController.php b/app/Http/Controllers/Api/v1/EntityMoveApiController.php index 66d8a9b840..d1d31d3e6d 100644 --- a/app/Http/Controllers/Api/v1/EntityMoveApiController.php +++ b/app/Http/Controllers/Api/v1/EntityMoveApiController.php @@ -6,7 +6,7 @@ use App\Models\Entity; use App\Http\Requests\MoveEntity as Request; use App\Services\Entity\MoveService; - +use Exception; class EntityMoveApiController extends ApiController { @@ -26,27 +26,35 @@ public function transfer(Request $request, Campaign $campaign) { $this->authorize('access', $campaign); $count = 0; - $copy = is_null($request->copy) ? true : $request->copy; - - 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++; + $copy = is_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.']); + if ($copy) { + return response()->json(['success' => 'Succesfully copied ' . $count . ' entities.']); + } + return response()->json(['success' => 'Succesfully transfered ' . $count . ' entities.']); + + } catch (Exception $e) { + return response()->json([ + 'error' => true, + 'message' => $e->getMessage(), + ]); } - return response()->json(['success' => 'Succesfully transfered ' . $count . ' entities.']); + } } diff --git a/resources/api-docs/1.0/entities.md b/resources/api-docs/1.0/entities.md index e2d7702918..66a2b71ef2 100644 --- a/resources/api-docs/1.0/entities.md +++ b/resources/api-docs/1.0/entities.md @@ -229,7 +229,7 @@ You can post an array with the ids of the entities you want to transfer to anoth | :- | :- | :- | | `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 copy if left empty | +| `copy` | `boolean` | True if the entity will be copied, false if the entity will be transfered, defaults to false if left empty | ### Result diff --git a/tests/Feature/Entities/EntityTest.php b/tests/Feature/Entities/EntityTest.php index 159db35fc9..2b78f26ac1 100644 --- a/tests/Feature/Entities/EntityTest.php +++ b/tests/Feature/Entities/EntityTest.php @@ -49,7 +49,6 @@ ->postJson('/api/1.0/campaigns/1/transfer', [ 'entities' => [1,2,3], 'campaign_id' => 2, - 'copy' => false ]) ->assertJsonFragment(['success' => 'Succesfully transfered 3 entities.']) ->assertStatus(200) @@ -63,6 +62,7 @@ ->postJson('/api/1.0/campaigns/1/transfer', [ 'entities' => [1,2,3], 'campaign_id' => 2, + 'copy' => true ]) ->assertJsonFragment(['success' => 'Succesfully copied 3 entities.']) ->assertStatus(200) From 8168c4c205c64227a339a072745f2040bcbba953 Mon Sep 17 00:00:00 2001 From: spitfire305 Date: Fri, 6 Oct 2023 20:31:04 +0000 Subject: [PATCH 4/5] Fix styling --- .../Api/v1/EntityMoveApiController.php | 22 +++++++++---------- 1 file changed, 11 insertions(+), 11 deletions(-) diff --git a/app/Http/Controllers/Api/v1/EntityMoveApiController.php b/app/Http/Controllers/Api/v1/EntityMoveApiController.php index d1d31d3e6d..b98ddd546f 100644 --- a/app/Http/Controllers/Api/v1/EntityMoveApiController.php +++ b/app/Http/Controllers/Api/v1/EntityMoveApiController.php @@ -26,20 +26,20 @@ public function transfer(Request $request, Campaign $campaign) { $this->authorize('access', $campaign); $count = 0; - $copy = is_null($request->copy) ? false : $request->copy; + $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() - ; + ->entity($entity) + ->campaign($campaign) + ->user($request->user()) + ->to($request->campaign_id) + ->copy($copy) + ->validate() + ->process() + ; $count++; } } @@ -48,13 +48,13 @@ public function transfer(Request $request, Campaign $campaign) return response()->json(['success' => 'Succesfully copied ' . $count . ' entities.']); } return response()->json(['success' => 'Succesfully transfered ' . $count . ' entities.']); - + } catch (Exception $e) { return response()->json([ 'error' => true, 'message' => $e->getMessage(), ]); } - + } } From 138007799ff4a0143124f282c505ddcba591a7b3 Mon Sep 17 00:00:00 2001 From: Spitfire Date: Fri, 6 Oct 2023 16:11:07 -0600 Subject: [PATCH 5/5] Fix --- app/Http/Controllers/Api/v1/EntityMoveApiController.php | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/app/Http/Controllers/Api/v1/EntityMoveApiController.php b/app/Http/Controllers/Api/v1/EntityMoveApiController.php index d1d31d3e6d..e7b6b32e69 100644 --- a/app/Http/Controllers/Api/v1/EntityMoveApiController.php +++ b/app/Http/Controllers/Api/v1/EntityMoveApiController.php @@ -2,11 +2,11 @@ namespace App\Http\Controllers\Api\v1; +use App\Exceptions\TranslatableException; 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 { @@ -26,7 +26,7 @@ public function transfer(Request $request, Campaign $campaign) { $this->authorize('access', $campaign); $count = 0; - $copy = is_null($request->copy) ? false : $request->copy; + $copy = $request->has('copy'); try { foreach ($request->entities as $id) { $entity = Entity::find($id); @@ -49,10 +49,10 @@ public function transfer(Request $request, Campaign $campaign) } return response()->json(['success' => 'Succesfully transfered ' . $count . ' entities.']); - } catch (Exception $e) { + } catch (TranslatableException $e) { return response()->json([ 'error' => true, - 'message' => $e->getMessage(), + 'message' => $e->getTranslatedMessage(), ]); }