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) public function update(Request $request)
{ {
$body = $request->collect(); $body = json_decode($request->getContent());
$images = $request->file('images'); //check to see if required fields are present
$apiKey = $request->header('fipamo-api-key'); if ($body->uuid == '' || $body->uuid == null) {
$imageList = ''; $error = [
$fileList = ''; 'message' => "[uuidx ] is a required field",
$body = $body->all(); 'type' => 'postError',
//if new images are present, replace existing ];
//if not, keep current return response()->json($error)->header('Content-Type', 'application/json');
if ($request->hasfile('images')) {
foreach ($images as $key => $image) {
$response = $this->upload->handlefile($image, false);
$imageList = $imageList . $response['filePath'] . ',';
}
} else {
$imageList = $body['imageList'];
} }
$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(); $folks = $this->member->getAll();
$member = find($folks, ['key' => $apiKey]); $member = find($folks, ['key' => $apiKey]);
$request->session()->put('member', $member); $request->session()->put('member', $member);
$body['slug'] = strtolower(str_replace(' ', '-', $body['title'])); $result = $this->pages->update($body);
$body['imageList'] = $imageList; //clear session once page is created
$body['fileList'] = $fileList;
$body = (object) $body;
$result = $this->pages->update($body);
session()->flush(); session()->flush();
return response()->json($result)->header('Content-Type', 'application/json'); return response()->json($result)->header('Content-Type', 'application/json');
} }
public function create(Request $request) public function create(Request $request)
{ {
$body = $request->collect(); $body = json_decode($request->getContent());
$images = $request->file('images'); //check to see if required fields are present
$apiKey = $request->header('fipamo-api-key'); if ($body->title == '' || $body->title == null) {
$imageList = ''; $error = [
$fileList = ''; 'message' => "[title] is a required field",
if ($request->hasfile('images')) { 'type' => 'postError',
foreach ($images as $key => $image) { ];
$response = $this->upload->handlefile($image, false); return response()->json($error)->header('Content-Type', 'application/json');
$imageList = $imageList . $response['filePath'] . ',';
}
} }
$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]); $member = find($folks, ['key' => $apiKey]);
$request->session()->put('member', $member); $request->session()->put('member', $member);
$body = $body->all(); $result = $this->pages->create($body);
$body['slug'] = strtolower(str_replace(' ', '-', $body['title'])); //clear session once page is created
$body['imageList'] = $imageList;
$body['fileList'] = $fileList;
$body = (object) $body;
$result = $this->pages->create($body);
session()->flush(); session()->flush();
return response()->json($result)->header('Content-Type', 'application/json'); return response()->json($result)->header('Content-Type', 'application/json');
} }
public function delete(Request $request) 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'); $apiKey = $request->header('fipamo-api-key');
$folks = $this->member->getAll(); $folks = $this->member->getAll();
$member = find($folks, ['key' => $apiKey]); $member = find($folks, ['key' => $apiKey]);

View file

@ -101,6 +101,15 @@ class PageRepository implements PageRepositoryInterface
$body->menu = 'false'; $body->menu = 'false';
$body->published = 'false'; $body->published = 'false';
$body->featured = '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 { } else {
$deleted = isset($page['deleted']) ? $page['deleted'] : 'false'; $deleted = isset($page['deleted']) ? $page['deleted'] : 'false';
} }

View file

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