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
This commit is contained in:
ro 2024-07-24 14:57:04 -06:00
parent 321feb9b92
commit d8ed8b62c0
No known key found for this signature in database
GPG key ID: 29B551CDBD4D3B50
3 changed files with 60 additions and 39 deletions

View file

@ -28,63 +28,75 @@ class PageAPIController extends Controller
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'];
$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);
$body['slug'] = strtolower(str_replace(' ', '-', $body['title']));
$body['imageList'] = $imageList;
$body['fileList'] = $fileList;
$body = (object) $body;
$result = $this->pages->update($body);
$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 = $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'] . ',';
}
$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');
}
$folks = $this->member->getAll();
$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);
$body = $body->all();
$body['slug'] = strtolower(str_replace(' ', '-', $body['title']));
$body['imageList'] = $imageList;
$body['fileList'] = $fileList;
$body = (object) $body;
$result = $this->pages->create($body);
$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());
$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]);

View file

@ -101,6 +101,15 @@ class PageRepository implements PageRepositoryInterface
$body->menu = 'false';
$body->published = 'false';
$body->featured = 'false';
//set body object with prexisting data
$body->id = $page['id'];
$body->layout = $page['layout'];
$body->slug = $page['slug'];
$body->title = $page['title'];
$body->imageList = $page['feature'];
$body->fileList = $page['files'];
$body->tags = $page['tags'];
$body->content = $page['content'];
} else {
$deleted = isset($page['deleted']) ? $page['deleted'] : 'false';
}

View file

@ -24,7 +24,7 @@ Route::post("/v1/restore", [InitAPIController::class, 'setupRestore']);
//handle page editing actions
Route::group(['prefix' => '/admin/page', 'middleware' => 'validate.key'], function () {
Route::post("/update", [PageAPIController::class, 'update']);
Route::put("/update", [PageAPIController::class, 'update']);
Route::post("/create", [PageAPIController::class, 'create']);
Route::delete("/delete", [PageAPIController::class, 'delete']);
});