153 lines
4.6 KiB
PHP
153 lines
4.6 KiB
PHP
<?php
|
|
|
|
namespace Modules\Blog\app\Repositories;
|
|
|
|
use Illuminate\Support\Facades\DB;
|
|
use Illuminate\Support\Str;
|
|
use Modules\Banner\app\Services\FileManagementService;
|
|
use Modules\Blog\app\Models\Blog;
|
|
use Modules\Blog\app\Models\BlogMeta;
|
|
|
|
class BlogRepository
|
|
{
|
|
//-- Retrieve all Services
|
|
public function allBlogs($perPage = null, $filter = [], $sort = ['by' => 'id', 'sort' => 'DESC'])
|
|
{
|
|
return Blog::with('blogMeta')->when(array_keys($filter, true), function ($query) use ($filter) {
|
|
if (!empty($filter['title'])) {
|
|
$query->where('title', $filter['title']);
|
|
}
|
|
if (!empty($filter['author'])) {
|
|
$query->where('author', 'like', '%' . $filter['author'] . '%');
|
|
}
|
|
})
|
|
->orderBy($sort['by'], $sort['sort'])
|
|
->paginate($perPage ?: env('PAGE_LIMIT', 999));
|
|
}
|
|
|
|
//-- Find Blog by uuid
|
|
public function findBlogByUuid($uuid)
|
|
{
|
|
return Blog::where('uuid', $uuid)->first();
|
|
}
|
|
|
|
public function storeBlog(array $validated)
|
|
{
|
|
DB::beginTransaction();
|
|
try {
|
|
$blog = new Blog();
|
|
$blog->uuid = Str::uuid();
|
|
$blog->title = $validated['title'];
|
|
$blog->content = $validated['content'];
|
|
$blog->author = $validated['author'];
|
|
$blog->summary = $validated['summary'];
|
|
$blog->published_date = $validated['published_date'];
|
|
$blog->status = $validated['status'];
|
|
$blog->slug = $validated['slug'];
|
|
$blog->save();
|
|
|
|
if (isset($validated['image']) && $validated['image']->isValid()) {
|
|
FileManagementService::storeFile(
|
|
file: $validated['image'],
|
|
uploadedFolderName: 'blogs',
|
|
model: $blog
|
|
);
|
|
}
|
|
|
|
$blogMeta = new BlogMeta();
|
|
$blogMeta->uuid = Str::uuid();
|
|
$blogMeta->meta_title = $validated['meta_title'];
|
|
$blogMeta->meta_description = $validated['meta_description'];
|
|
$blogMeta->meta_keywords = $validated['meta_keywords'];
|
|
$blog->blogMeta()->save($blogMeta);
|
|
DB::commit();
|
|
|
|
return $blog;
|
|
} catch (\Throwable $th) {
|
|
report($th);
|
|
DB::rollback();
|
|
|
|
return null;
|
|
}
|
|
}
|
|
|
|
public function updateBlog($validated, $uuid)
|
|
{
|
|
DB::beginTransaction();
|
|
try {
|
|
$blog = $this->findBlogByUuid($uuid);
|
|
if (!$blog) {
|
|
return null;
|
|
}
|
|
$blog->title = $validated['title'];
|
|
$blog->content = $validated['content'];
|
|
$blog->author = $validated['author'];
|
|
$blog->summary = $validated['summary'];
|
|
$blog->published_date = $validated['published_date'];
|
|
$blog->status = $validated['status'];
|
|
$blog->slug = $validated['slug'];
|
|
$blog->save();
|
|
|
|
if (isset($validated['image']) && $validated['image']->isValid()) {
|
|
FileManagementService::uploadFile(
|
|
file: $validated['image'],
|
|
uploadedFolderName: 'blogs',
|
|
filePath: $blog->image_path,
|
|
model: $blog
|
|
);
|
|
}
|
|
|
|
// Update or create blog meta
|
|
$blogMeta = $blog->blogMeta()->firstOrNew([]);
|
|
if (!$blogMeta->exists) {
|
|
$blogMeta->uuid = Str::uuid();
|
|
}
|
|
$blogMeta->meta_title = $validated['meta_title'];
|
|
$blogMeta->meta_description = $validated['meta_description'];
|
|
$blogMeta->meta_keywords = $validated['meta_keywords'];
|
|
$blogMeta->save();
|
|
|
|
DB::commit();
|
|
|
|
return $blog;
|
|
} catch (\Throwable $th) {
|
|
dd($th);
|
|
report($th);
|
|
DB::rollBack();
|
|
|
|
return null;
|
|
}
|
|
}
|
|
|
|
//-- Delete Blog
|
|
public function deleteBlog(string $uuid)
|
|
{
|
|
DB::beginTransaction();
|
|
try {
|
|
$blog = $this->findBlogByUuid($uuid);
|
|
if (!$blog) {
|
|
return null;
|
|
}
|
|
|
|
// Delete the image file associated with the blog
|
|
if ($blog->image_path !== null) {
|
|
FileManagementService::deleteFile($blog->image_path);
|
|
}
|
|
|
|
// Delete associated blog meta and the blog itself
|
|
$blog->blogMeta()->delete();
|
|
|
|
$blog->delete();
|
|
|
|
DB::commit();
|
|
|
|
return $blog;
|
|
} catch (\Throwable $th) {
|
|
DB::rollBack();
|
|
report($th);
|
|
|
|
return null;
|
|
}
|
|
}
|
|
}
|