From 7024285b707d4c76d6b44b7b07024923643f250c Mon Sep 17 00:00:00 2001 From: ro Date: Fri, 8 Mar 2024 14:09:43 -0600 Subject: [PATCH] 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. --- .../Controllers/API/PageAPIController.php | 19 +-- app/Http/Controllers/Dash/IndexController.php | 2 +- app/Interfaces/PageRepositoryInterface.php | 4 +- app/Providers/FipamoServiceProvider.php | 9 ++ app/Repositories/PageRepository.php | 101 ++++++++++++- app/Services/DocService.php | 143 ++++++++++++++++++ app/Services/StringService.php | 29 ++++ composer.json | 1 + composer.lock | 2 +- resources/views/back/page.blade.php | 2 +- 10 files changed, 292 insertions(+), 20 deletions(-) create mode 100644 app/Services/DocService.php create mode 100644 app/Services/StringService.php diff --git a/app/Http/Controllers/API/PageAPIController.php b/app/Http/Controllers/API/PageAPIController.php index ad9355a..1b96588 100644 --- a/app/Http/Controllers/API/PageAPIController.php +++ b/app/Http/Controllers/API/PageAPIController.php @@ -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'); } } diff --git a/app/Http/Controllers/Dash/IndexController.php b/app/Http/Controllers/Dash/IndexController.php index 66a64d3..3b652d0 100644 --- a/app/Http/Controllers/Dash/IndexController.php +++ b/app/Http/Controllers/Dash/IndexController.php @@ -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'] ]); } } diff --git a/app/Interfaces/PageRepositoryInterface.php b/app/Interfaces/PageRepositoryInterface.php index e8d1b7f..b9391fe 100644 --- a/app/Interfaces/PageRepositoryInterface.php +++ b/app/Interfaces/PageRepositoryInterface.php @@ -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); } diff --git a/app/Providers/FipamoServiceProvider.php b/app/Providers/FipamoServiceProvider.php index 776702d..da23647 100644 --- a/app/Providers/FipamoServiceProvider.php +++ b/app/Providers/FipamoServiceProvider.php @@ -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(); + }); } /** diff --git a/app/Repositories/PageRepository.php b/app/Repositories/PageRepository.php index 568bfa2..8441b61 100644 --- a/app/Repositories/PageRepository.php +++ b/app/Repositories/PageRepository.php @@ -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, + ]; + } + } } diff --git a/app/Services/DocService.php b/app/Services/DocService.php new file mode 100644 index 0000000..2ff6b06 --- /dev/null +++ b/app/Services/DocService.php @@ -0,0 +1,143 @@ +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; + } +} diff --git a/app/Services/StringService.php b/app/Services/StringService.php new file mode 100644 index 0000000..63d575e --- /dev/null +++ b/app/Services/StringService.php @@ -0,0 +1,29 @@ +