fipamo/app/Http/Controllers/RoutePutController.php
ro c5afbb9131
API Decouplng Part 2
removed all remaining API requests from the front end and removed the
FipamoAdminAPI js file, changing it to ContentRequest as it now handles
posting data to the system directly, authenticating it self by checking
the embedded CSRF token that regulary rotates

also renamed MaintenanceManager to Maintenance request and moved it to
the same dir as ContentRequest as they are both libraries that connect
to the backend
2024-07-17 15:08:10 -06:00

93 lines
3 KiB
PHP

<?php
namespace App\Http\Controllers;
use App\Interfaces\PageRepositoryInterface;
use App\Services\Assets\AssetService;
use App\Services\Assets\RenderService;
use App\Interfaces\MemberRepositoryInterface;
use App\Services\Data\SettingsService;
use App\Services\Upkeep\MaintenanceService;
use Illuminate\Http\Request;
class RoutePutController extends Controller
{
protected $page;
protected $assets;
protected $render;
protected $settings;
protected $member;
protected $maintenance;
public function __construct(
PageRepositoryInterface $pageRepo,
AssetService $assetService,
RenderService $renderService,
SettingsService $settingsService,
MemberRepositoryInterface $memberRepo,
MaintenanceService $maintenanceService,
) {
$this->page = $pageRepo;
$this->assets = $assetService;
$this->render = $renderService;
$this->settings = $settingsService;
$this->member = $memberRepo;
$this->maintenance = $maintenanceService;
}
public function handleRequest(Request $request)
{
$path = explode('/', $request->path());
switch ($path[0]) {
case 'page':
$body = json_decode($request->getContent());
$result = $this->page->update($body);
return response()->json($result)->header('Content-Type', 'application/json');
break;
case 'settings':
return $this->settingsTasks($request, $path[1]);
break;
case 'backup':
return $this->createBackup($request);
break;
}
}
private function createBackup($request)
{
$body = json_decode($request->getContent());
if ($body->task == 'content_backup') {
return response()->json(
$this->maintenance->createContentBackUp()
)->header('Content-Type', 'application/json');
} else {
return response()->json(
$this->maintenance->createFileBackUp()
)->header('Content-Type', 'application/json');
}
}
private function settingsTasks($request, $task)
{
$result = [];
switch ($task) {
case 'publish':
$this->assets->moveToTheme(true);
$result = $this->render->publishAll();
break;
case 'sync':
$body = json_decode($request->getContent());
//update member if needed
$this->member->update($body->member);
//sync settings
$result = $this->settings->sync($body);
break;
case 'nav-sync':
$body = json_decode($request->getContent());
$result = $this->settings->navSync($body);
break;
}
return response()->json($result)->header('Content-Type', 'application/json');
}
}