fipamo/app/Http/Controllers/API/PageAPIController.php
ro 321feb9b92
Page Admin API, part 1
API accessible tasks (create, update, delete) have been updated to the
new format.

The controller needs to be cleaned up because it's a bit heavy and the
new API flow still needs to be properly documented, but it's a good
start
2024-07-23 17:46:45 -06:00

97 lines
3.4 KiB
PHP

<?php
namespace App\Http\Controllers\API;
use App\Interfaces\PageRepositoryInterface;
use App\Interfaces\MemberRepositoryInterface;
use App\Http\Controllers\Controller;
use App\Services\Assets\FileUploadService;
use Illuminate\Http\Request;
use function _\find;
class PageAPIController extends Controller
{
protected $pages;
protected $member;
protected $upload;
public function __construct(
PageRepositoryInterface $pageRepository,
MemberRepositoryInterface $memberRepo,
FileUploadService $fileUploadService,
) {
$this->pages = $pageRepository;
$this->upload = $fileUploadService;
$this->member = $memberRepo;
}
public function update(Request $request)
{
$body = $request->collect();
$images = $request->file('images');
$apiKey = $request->header('fipamo-api-key');
$imageList = '';
$fileList = '';
$body = $body->all();
//if new images are present, replace existing
//if not, keep current
if ($request->hasfile('images')) {
foreach ($images as $key => $image) {
$response = $this->upload->handlefile($image, false);
$imageList = $imageList . $response['filePath'] . ',';
}
} else {
$imageList = $body['imageList'];
}
$folks = $this->member->getAll();
$member = find($folks, ['key' => $apiKey]);
$request->session()->put('member', $member);
$body['slug'] = strtolower(str_replace(' ', '-', $body['title']));
$body['imageList'] = $imageList;
$body['fileList'] = $fileList;
$body = (object) $body;
$result = $this->pages->update($body);
session()->flush();
return response()->json($result)->header('Content-Type', 'application/json');
}
public function create(Request $request)
{
$body = $request->collect();
$images = $request->file('images');
$apiKey = $request->header('fipamo-api-key');
$imageList = '';
$fileList = '';
if ($request->hasfile('images')) {
foreach ($images as $key => $image) {
$response = $this->upload->handlefile($image, false);
$imageList = $imageList . $response['filePath'] . ',';
}
}
$folks = $this->member->getAll();
$member = find($folks, ['key' => $apiKey]);
$request->session()->put('member', $member);
$body = $body->all();
$body['slug'] = strtolower(str_replace(' ', '-', $body['title']));
$body['imageList'] = $imageList;
$body['fileList'] = $fileList;
$body = (object) $body;
$result = $this->pages->create($body);
session()->flush();
return response()->json($result)->header('Content-Type', 'application/json');
}
public function delete(Request $request)
{
$body = json_decode($request->getContent());
$apiKey = $request->header('fipamo-api-key');
$folks = $this->member->getAll();
$member = find($folks, ['key' => $apiKey]);
$request->session()->put('member', $member);
$result = $this->pages->delete($body);
session()->flush();
return response()->json($result)->header('Content-Type', 'application/json');
}
}