From d8ed8b62c0ace992053951483cbdb46d2c21d256 Mon Sep 17 00:00:00 2001 From: ro Date: Wed, 24 Jul 2024 14:57:04 -0600 Subject: [PATCH] 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 --- .../Controllers/API/PageAPIController.php | 88 +++++++++++-------- app/Repositories/PageRepository.php | 9 ++ routes/api.php | 2 +- 3 files changed, 60 insertions(+), 39 deletions(-) diff --git a/app/Http/Controllers/API/PageAPIController.php b/app/Http/Controllers/API/PageAPIController.php index 04e9537..32e9ebe 100644 --- a/app/Http/Controllers/API/PageAPIController.php +++ b/app/Http/Controllers/API/PageAPIController.php @@ -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]); diff --git a/app/Repositories/PageRepository.php b/app/Repositories/PageRepository.php index 2fe8433..fb6cc81 100644 --- a/app/Repositories/PageRepository.php +++ b/app/Repositories/PageRepository.php @@ -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'; } diff --git a/routes/api.php b/routes/api.php index ddecda8..afc94cb 100644 --- a/routes/api.php +++ b/routes/api.php @@ -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']); });