page update api up and running

the first part of the page editing API is working again after porting it over
form the old fipamo build. a few changes where made to make the code a
big more managable, but the end to end process works for updating pages.

the remaining page editing methods will be activated after the rendering
engine is in place because that's going to be a pretty siginficant
effort.

but this is a big step.
This commit is contained in:
ro 2024-03-08 14:09:43 -06:00
parent b9b04f1ab2
commit 7024285b70
No known key found for this signature in database
GPG key ID: 29B551CDBD4D3B50
10 changed files with 292 additions and 20 deletions

View file

@ -2,29 +2,24 @@
namespace App\Http\Controllers\API;
use App\Interfaces\PageRepositoryInterface;
use App\Http\Controllers\Controller;
use App\Services\AuthService;
use Illuminate\Http\Request;
class PageAPIController extends Controller
{
protected $pages;
public function __construct(
AuthService $authService
PageRepositoryInterface $pageRepository
) {
$this->auth = $authService;
$this->pages = $pageRepository;
}
public function write(Request $request)
{
$body = json_decode($request->getContent());
//var_dump($body);
$result = [
'message' => 'API IS CONNECTED',
'type' => 'apiTesting',
'uuid' => $body->uuid,
];
$body = json_decode($request->getContent());
$result = $this->pages->update($body);
return response()->json($result)->header('Content-Type', 'application/json');
}
}

View file

@ -70,7 +70,7 @@ class IndexController extends Controller
"mode" => $mode,
"page" => $page,
"views" => $this->themes->getCustomViews($page['layout']),
"title" => 'Editing ' . $page['title']
"title" => $page['title']
]);
}
}

View file

@ -10,9 +10,9 @@ interface PageRepositoryInterface
public function delete($uuid);
public function create(array $page);
public function create($page);
public function update($uuid, array $page);
public function update($page);
public function getGroup($num, $limit, $filter);
}

View file

@ -10,6 +10,7 @@ use App\Services\AuthService;
use App\Services\ContentService;
use App\Services\PaginateService;
use App\Services\ThemeService;
use App\Services\DocService;
class FipamoServiceProvider extends ServiceProvider
{
@ -38,6 +39,14 @@ class FipamoServiceProvider extends ServiceProvider
$this->app->bind(PaginateService::class, function ($app) {
return new PaginateService(new ContentService());
});
$this->app->bind(StringService::class, function ($app) {
return new StringService();
});
$this->app->bind(DocService::class, function ($app) {
return new DocService();
});
}
/**

View file

@ -6,6 +6,9 @@ use App\Interfaces\PageRepositoryInterface;
use App\Services\SettingsService;
use App\Services\ContentService;
use App\Services\PaginateService;
use App\Services\StringService;
use App\Services\DocService;
use Carbon\Carbon;
class PageRepository implements PageRepositoryInterface
{
@ -13,15 +16,21 @@ class PageRepository implements PageRepositoryInterface
protected $setttings;
protected $paginate;
protected $pages;
protected $strings;
protected $docs;
public function __construct(
ContentService $contentService,
SettingsService $settingsService,
PaginateService $paginateService
PaginateService $paginateService,
StringService $stringService,
DocService $docService,
) {
$this->content = $contentService;
$this->settings = $settingsService;
$this->paginate = $paginateService;
$this->strings = $stringService;
$this->docs = $docService;
$this->pages = $this->content->loadAllPages();
}
@ -39,16 +48,102 @@ class PageRepository implements PageRepositoryInterface
{
}
public function create(array $page)
public function create($page)
{
}
public function update($uuid, array $page)
public function update($page)
{
return $this->editPage($page, $this->pages->where('uuid', $page->uuid)->first(), 'update');
//hande result of page update
}
public function getGroup($num, $limit, $sort = "all")
{
return $this->paginate->getPage($num, $limit, $sort);
}
private function editPage($body, $page, $task)
{
$path;
$file;
$writePath;
$message;
if ($task != 'create') {
$path = date('Y', date($page['rawCreated'])) .
'/' .
date('m', date($page['rawCreated']));
} else {
$path = date('Y') . '/' . date('m');
}
if ($task == 'delete') {
$deleted = 'true';
$body->menu = 'false';
$body->published = 'false';
$body->featured = 'false';
} else {
$deleted = isset($page['deleted']) ? $page['deleted'] : 'false';
}
$created = $task != 'create' ? new Carbon($page['rawCreated']) : Carbon::now();
$updated = Carbon::now();
// grab current index from settings and update
$id = $task != 'create' ? $body->id : $this->settings->getGlobal()['currentIndex'];
$uuid = $task != 'create' ? $body->uuid : $this->strings::createUUID();
//set variables post body for saving
$body->id = $id;
$body->uuid = $uuid;
$body->path = $path;
$body->author = session('member')['handle'];
$body->created = $created->format("Y-m-d\TH:i:sP");
$body->updated = $updated->format("Y-m-d\TH:i:sP");
$body->deleted = $deleted;
//set path to save file
if ($body->layout == 'index') {
$writePath = '../content/pages/start/index.md';
} else {
$writePath = '../content/pages/' . $path . '/' . $body->slug . '.md';
}
//write file to path
$saved = $this->docs::writePages($task, $path, $writePath, $this->docs::objectToMD($body));
//handle post save result
if ($saved) {
if (
$this->settings->getGlobal()['renderOnSave'] == 'true' &&
$this->settings->getGlobal()['dynamicRender'] == 'false'
) {
//TODO: RENDER ENGINE STUFF
//$render = new Render();
//$render->renderTags();
//$render->renderArchive();
//$render->renderPages();
$message = 'Filed edited and rendered. NOICE.';
} else {
$message = 'File edited. Nice work';
}
//upadte settings if needed
$body->path = $path;
//Settings::updateMenu($body);
//Settings::updateTags();
// if new page added, update current index in Settings file
if ($task == 'create') {
//Settings::updateIndex();
}
return [
'message' => $message,
'type' => $task == 'update' ? 'postUpdated' : 'postAdded',
'id' => $uuid,
];
} else {
return $response = [
'message' => "Uh oh. File save problem. Don't panic",
'type' => 'postError',
'id' => $uuid,
];
}
}
}

143
app/Services/DocService.php Normal file
View file

@ -0,0 +1,143 @@
<?php
namespace App\Services;
class DocService
{
public function __construct()
{
}
public static function writePages($task, $path, $fileLocation, $fileContents)
{
try {
if ($task == 'create') {
if (!is_dir('../content/pages/' . $path)) {
//Directory does not exist, so lets create it.
mkdir('../content/pages/' . $path, 0755, true);
}
file_put_contents($fileLocation, $fileContents);
} else {
($new = fopen($fileLocation, 'w')) or die('Unable to open file!');
fwrite($new, $fileContents);
fclose($new);
}
return true;
} catch (Error $error) {
return false;
}
}
public static function writeSettings($fileLocation, $fileContents)
{
if (!is_file($fileLocation)) {
file_put_contents($fileLocation, json_encode($fileContents));
} else {
($new = fopen($fileLocation, 'w')) or die('Unable to open file!');
fwrite($new, json_encode($fileContents));
fclose($new);
}
}
public static function writeHTML($location, $html, $path = null)
{
if ($path != null) {
if (!is_dir($path)) {
//Directory does not exist, so lets create it.
mkdir($path, 0755, true);
}
}
if (!is_file($location)) {
file_put_contents($location, $html);
} else {
($new = fopen($location, 'w')) or die('Unable to open file!');
fwrite($new, $html);
fclose($new);
}
}
public static function deleteFolder($path)
{
if (!empty($path) && is_dir($path)) {
$dir = new \RecursiveDirectoryIterator(
$path,
\RecursiveDirectoryIterator::SKIP_DOTS
); //upper dirs are not included,otherwise DISASTER HAPPENS :)
$files = new \RecursiveIteratorIterator(
$dir,
\RecursiveIteratorIterator::CHILD_FIRST
);
foreach ($files as $f) {
if (is_file($f)) {
unlink($f);
} else {
$empty_dirs[] = $f;
}
}
if (!empty($empty_dirs)) {
foreach ($empty_dirs as $eachDir) {
rmdir($eachDir);
}
}
rmdir($path);
}
}
public static function objectToMD($object)
{
$markdown = "---\n" .
'id: ' .
$object->id .
"\n" .
'uuid: ' .
$object->uuid .
"\n" .
'title: ' .
"'" .
$object->title .
"'" .
"\n" .
'feature: ' .
$object->imageList .
"\n" .
'files: ' .
$object->fileList .
"\n" .
'path: ' .
$object->path .
"\n" .
'layout: ' .
$object->layout .
"\n" .
'tags: ' .
$object->tags .
"\n" .
'author: ' .
$object->author .
"\n" .
'created: ' .
$object->created .
"\n" .
'updated: ' .
$object->updated .
"\n" .
'deleted: ' .
$object->deleted .
"\n" .
'slug: ' .
$object->slug .
"\n" .
'menu: ' .
$object->menu .
"\n" .
'published: ' .
$object->menu .
"\n" .
'featured: ' .
$object->featured .
"\n---\n" .
$object->content;
return $markdown;
}
}

View file

@ -0,0 +1,29 @@
<?php
namespace App\Services;
class StringService
{
public function __construct()
{
}
public static function creatUUID()
{
if (function_exists('com_create_guid') === true) {
return trim(com_create_guid(), '{}');
}
return sprintf(
'%04X%04X-%04X-%04X-%04X-%04X%04X%04X',
mt_rand(0, 65535),
mt_rand(0, 65535),
mt_rand(0, 65535),
mt_rand(16384, 20479),
mt_rand(32768, 49151),
mt_rand(0, 65535),
mt_rand(0, 65535),
mt_rand(0, 65535)
);
}
}

View file

@ -25,6 +25,7 @@
"lodash-php/lodash-php": "^0.09.0",
"mindtwo/laravel-blade-spaceless": "^1.2",
"mnapoli/front-yaml": "^2.0",
"nesbot/carbon": "^2.72",
"olegatro/html-sanitizer-relative": "^1.0",
"rbdwllr/reallysimplejwt": "^5.0",
"symfony/yaml": "^7.0",

2
composer.lock generated
View file

@ -4,7 +4,7 @@
"Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies",
"This file is @generated automatically"
],
"content-hash": "6ae8a3cfbf0bceca61b34d852e461d95",
"content-hash": "471bfaaad2a8a9950d793458b1801a22",
"packages": [
{
"name": "brick/math",

View file

@ -1,6 +1,6 @@
@extends('frame')
@section('title', 'The Dash | Edit Page')
@section('title', 'The Dash | Editing '. $title)
@php
if($mode == 'edit')