fipamo/app/Http/Controllers/RoutePostController.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

124 lines
4.1 KiB
PHP

<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Mail;
use App\Mail\SystemEmail;
use App\Interfaces\PageRepositoryInterface;
use App\Services\Upkeep\MaintenanceService;
use App\Services\Assets\FileUploadService;
use App\Interfaces\MemberRepositoryInterface;
use App\Services\Data\SettingsService;
use App\Services\UpKeep\InitService;
use App\Services\UpKeep\ResetService;
class RoutePostController extends Controller
{
protected $page;
protected $gate;
protected $maintenance;
protected $upload;
protected $settings;
protected $member;
protected $init;
protected $reset;
public function __construct(
PageRepositoryInterface $pageRepo,
AuthController $authController,
MaintenanceService $maintenanceService,
FileUploadService $fileUploadService,
SettingsService $settingsService,
MemberRepositoryInterface $memberRepo,
InitService $initService,
ResetService $resetService,
) {
$this->page = $pageRepo;
$this->gate = $authController;
$this->maintenance = $maintenanceService;
$this->upload = $fileUploadService;
$this->settings = $settingsService;
$this->member = $memberRepo;
$this->init = $initService;
$this->reset = $resetService;
}
public function handleRequest(Request $request)
{
$path = explode('/', $request->path());
switch ($path[0]) {
case 'init':
return $this->initTask($path[1], $request);
break;
case 'login':
return $this->gate->enter($request);
break;
case 'page':
$body = json_decode($request->getContent());
$result = $this->page->create($body);
return response()->json($result)->header('Content-Type', 'application/json');
break;
case 'settings':
if ($path[1] == 'mailer') {
return $this->sendNotify($request);
}
break;
case 'upload':
$type = null;
$result = $result = $this->upload->handleFile($request, $type);
//update configs for specfic uploads
switch ($request['source']) {
case 'avatar-upload':
$member = [];
$member = session('member');
$member['avatar'] = $result['filePath'];
$member = (object) $member;
$this->member->update($member);
break;
case 'background-upload':
$this->settings->updateGlobalData('background', $result['filePath']);
break;
}
return $result;
break;
}
}
private function initTask($task, $request)
{
$result = [];
switch ($task) {
case 'fresh':
$result = $this->init->fresh(json_decode($request->getContent()));
break;
case 'restore':
$result = $this->init->restore($request);
break;
case 'reset':
$result = $this->reset->site($request);
break;
}
return response()->json($result)->header('Content-Type', 'application/json');
}
private function sendNotify($request)
{
$result = [];
try {
Mail::to(env('ADMIN_EMAIL'))->send(new SystemEmail($request->content));
$result = [
'type' => 'mail_good',
'message' => 'Mail Sent',
];
} catch (TransportException $e) {
$result = [
'type' => 'mail_not_good',
'message' => 'Mail Not Sent. It\'s cool. Just check mail settings in the .env',
];
}
return response()->json($result)->header('Content-Type', 'application/json');
}
}