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 2 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
31 changes: 20 additions & 11 deletions app/Http/Controllers/Api/v1/EntityMoveApiController.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
use App\Models\Entity;
use App\Http\Requests\MoveEntity as Request;
use App\Services\Entity\MoveService;
use Exception;

class EntityMoveApiController extends ApiController
{
Expand All @@ -25,12 +26,12 @@ public function transfer(Request $request, Campaign $campaign)
{
$this->authorize('access', $campaign);
$count = 0;
$copy = null === $request->copy ? true : $request->copy;

foreach ($request->entities as $id) {
$entity = Entity::find($id);
if ($this->authorize('update', $entity->child)) {
$this->service
$copy = is_null($request->copy) ? false : $request->copy;
spitfire305 marked this conversation as resolved.
Show resolved Hide resolved
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())
Expand All @@ -39,13 +40,21 @@ public function transfer(Request $request, Campaign $campaign)
->validate()
->process()
;
$count++;
$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) {
spitfire305 marked this conversation as resolved.
Show resolved Hide resolved
return response()->json([
'error' => true,
'message' => $e->getMessage(),
]);
}
return response()->json(['success' => 'Succesfully transfered ' . $count . ' entities.']);

}
}
2 changes: 1 addition & 1 deletion resources/api-docs/1.0/entities.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
2 changes: 1 addition & 1 deletion tests/Feature/Entities/EntityTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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)
Expand Down