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:
parent
321feb9b92
commit
d8ed8b62c0
3 changed files with 60 additions and 39 deletions
|
@ -28,56 +28,60 @@ class PageAPIController extends Controller
|
|||
|
||||
public function update(Request $request)
|
||||
{
|
||||
$body = $request->collect();
|
||||
$images = $request->file('images');
|
||||
$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');
|
||||
$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'] . ',';
|
||||
$body->slug = strtolower(str_replace(' ', '-', $body->title));
|
||||
if (!isset($body->imageList)) {
|
||||
$body->imageList = '';
|
||||
}
|
||||
} else {
|
||||
$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);
|
||||
//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');
|
||||
$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');
|
||||
$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();
|
||||
$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);
|
||||
//clear session once page is created
|
||||
session()->flush();
|
||||
return response()->json($result)->header('Content-Type', 'application/json');
|
||||
}
|
||||
|
@ -85,6 +89,14 @@ class PageAPIController extends Controller
|
|||
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]);
|
||||
|
|
|
@ -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';
|
||||
}
|
||||
|
|
|
@ -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']);
|
||||
});
|
||||
|
|
Loading…
Reference in a new issue