Skip to content

Commit

Permalink
refactor: move merge campaigns logic to RegisterCampaignRoutes.php
Browse files Browse the repository at this point in the history
  • Loading branch information
alaca committed Nov 8, 2024
1 parent f292027 commit bd72636
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 65 deletions.
15 changes: 15 additions & 0 deletions src/Campaigns/Controllers/CampaignRequestController.php
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,21 @@ public function updateCampaign(WP_REST_Request $request)
return new WP_REST_Response($campaign->toArray());
}

/**
* @unreleased
*
* @throws Exception
*/
public function mergeCampaigns(WP_REST_Request $request): WP_REST_Response
{
$destinationCampaign = Campaign::find($request->get_param('id'));
$campaignsToMerge = Campaign::query()->whereIn('id', $request->get_param('campaignsToMergeIds'))->getAll();

$campaignsMerged = $destinationCampaign->merge(...$campaignsToMerge);

return new WP_REST_Response($campaignsMerged);
}


/**
* @unreleased
Expand Down
65 changes: 0 additions & 65 deletions src/Campaigns/Routes/MergeCampaigns.php

This file was deleted.

39 changes: 39 additions & 0 deletions src/Campaigns/Routes/RegisterCampaignRoutes.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ public function __invoke()
$this->registerGetCampaign();
$this->registerUpdateCampaign();
$this->registerGetCampaigns();
$this->registerMergeCampaigns();
$this->registerCreateCampaign();
}

Expand Down Expand Up @@ -130,6 +131,44 @@ public function registerUpdateCampaign()
}


/**
* Update Campaign route
*
* @unreleased
*/
public function registerMergeCampaigns()
{
register_rest_route(
CampaignRoute::NAMESPACE,
CampaignRoute::CAMPAIGN . '/merge',
[
[
'methods' => WP_REST_Server::EDITABLE,
'callback' => function (WP_REST_Request $request) {
return $this->campaignRequestController->mergeCampaigns($request);
},
'permission_callback' => function () {
return current_user_can('manage_options');
},
],
'args' => [
'id' => [
'type' => 'integer',
'required' => true,
],
'campaignsToMergeIds' => [
'type' => 'array',
'required' => true,
'items' => [
'type' => 'integer',
],
],
],
]
);
}


/**
* Create Campaign route
*
Expand Down

0 comments on commit bd72636

Please sign in to comment.