fipamo/app/Http/Controllers/API/PageAPIController.php
ro d8ed8b62c0
Page Admin API, part 2
page tasks have been changed to accept JSON data for the sake of
consistency across the API. The only API method that will accept form
data is file uploads.

Also restored the post, put and delete pattern for better organization
and clarity describing what each page method is for
2024-07-24 14:57:04 -06:00

109 lines
3.8 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 = json_decode($request->getContent());
//check to see if required fields are present
if ($body->uuid == '' || $body->uuid == null) {
$error = [
'message' => "[uuidx ] is a required field",
'type' => 'postError',
];
return response()->json($error)->header('Content-Type', 'application/json');
}
$apiKey = $request->header('fipamo-api-key');
$body->slug = strtolower(str_replace(' ', '-', $body->title));
if (!isset($body->imageList)) {
$body->imageList = '';
}
if (!isset($body->fileList)) {
$body->fileList = '';
}
//find member based on key and create a session
$folks = $this->member->getAll();
$member = find($folks, ['key' => $apiKey]);
$request->session()->put('member', $member);
$result = $this->pages->update($body);
//clear session once page is created
session()->flush();
return response()->json($result)->header('Content-Type', 'application/json');
}
public function create(Request $request)
{
$body = json_decode($request->getContent());
//check to see if required fields are present
if ($body->title == '' || $body->title == null) {
$error = [
'message' => "[title] is a required field",
'type' => 'postError',
];
return response()->json($error)->header('Content-Type', 'application/json');
}
$apiKey = $request->header('fipamo-api-key');
$folks = $this->member->getAll();
$body->slug = strtolower(str_replace(' ', '-', $body->title));
if (!isset($body->imageList)) {
$body->imageList = '';
}
if (!isset($body->fileList)) {
$body->fileList = '';
}
//find member based on key and create a session
$member = find($folks, ['key' => $apiKey]);
$request->session()->put('member', $member);
$result = $this->pages->create($body);
//clear session once page is created
session()->flush();
return response()->json($result)->header('Content-Type', 'application/json');
}
public function delete(Request $request)
{
$body = json_decode($request->getContent());
//check to see if required fields are present
if ($body->uuid == '' || $body->uuid == null) {
$error = [
'message' => "[uuidx ] is a required field",
'type' => 'postError',
];
return response()->json($error)->header('Content-Type', 'application/json');
}
$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');
}
}