firstcommit

This commit is contained in:
2025-08-17 16:23:14 +05:45
commit 76bf4c0a18
2648 changed files with 362795 additions and 0 deletions

View File

@@ -0,0 +1,142 @@
<?php
namespace Modules\Blog\app\Http\Controllers;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;
use App\Http\Controllers\Controller;
use App\Jobs\SendNewBlogNotification;
use Illuminate\Http\RedirectResponse;
use Modules\Blog\app\Repositories\BlogRepository;
use Modules\Blog\app\Http\Requests\CreateBlogRequest;
class BlogController extends Controller
{
protected $blogRepository;
public function __construct()
{
$this->blogRepository = new BlogRepository;
}
/**
* Display a listing of the resource.
*/
public function index(Request $request)
{
$perPage = $request->has('per-page') ? $request->input('per-page') : null;
$filter = $request->has('filter') ? $request->input('filter') : [];
$blogs = $this->blogRepository->allBlogs($perPage, $filter);
return view('blog::index', compact('blogs'));
}
/**
* Show the form for creating a new resource.
*/
public function create()
{
return view('blog::create');
}
/**
* Store a newly created resource in storage.
*/
public function store(CreateBlogRequest $request)
{
try {
$validated = $request->validated();
$blog = $this->blogRepository->storeBlog($validated);
dispatch(new SendNewBlogNotification($blog));
toastr()->success('Blog created successfully.');
return redirect()->route('cms.blogs.index');
} catch (\Throwable $th) {
report($th);
toastr()->error('Something went wrong.');
return back();
}
}
/**
* Show the specified resource.
*/
public function show($id)
{
return view('blog::show');
}
/**
* Show the form for editing the specified resource.
*/
public function edit($uuid)
{
$blog = $this->blogRepository->findBlogByUuid($uuid);
if (!$blog) {
toastr()->error('Blog not found.');
return back();
}
return view('blog::edit', compact('blog'));
}
/**
* Update the specified resource in storage.
*/
public function update(CreateBlogRequest $request, $uuid): RedirectResponse
{
try {
$validated = $request->validated();
$blog = $this->blogRepository->updateBlog($validated, $uuid);
if (!$blog) {
toastr()->error('Blog not found !');
return back();
}
toastr()->success('Blog updated successfully.');
return redirect()->route('cms.blogs.index');
} catch (\Throwable $th) {
report($th);
toastr()->error('Something went wrong.');
return back();
}
}
/**
* Remove the specified resource from storage.
*/
public function destroy($uuid)
{
try {
$blog = $this->blogRepository->deleteBlog($uuid);
if (!$blog) {
toastr()->error('Blog not found.');
return back();
}
DB::commit();
toastr()->success('Blog deleted successfully.');
return redirect()->route('cms.blogs.index');
} catch (\Throwable $th) {
DB::rollback();
report($th);
toastr()->error('Something went wrong.');
return back();
}
}
}

View File

View File

@@ -0,0 +1,76 @@
<?php
namespace Modules\Blog\app\Http\Requests;
use Illuminate\Foundation\Http\FormRequest;
class CreateBlogRequest extends FormRequest
{
/**
* Get the validation rules that apply to the request.
*/
public function rules(): array
{
return [
'title' => 'required|string|max:255',
'content' => 'required|string',
'author' => 'required|string|max:255',
'summary' => 'required|string',
'published_date' => 'required|date',
'status' => 'required|in:active,inactive',
'meta_title' => 'sometimes|nullable|string|max:255',
'meta_description' => 'sometimes|nullable|string',
'meta_keywords' => 'sometimes|nullable|string|max:255',
'image' => 'sometimes|nullable|mimes:png,jpg,jpeg',
'slug' => 'required',
];
}
public function messages()
{
return [
'title.required' => 'The title field is required.',
'title.string' => 'The title field must be a string.',
'title.max' => 'The title may not be greater than 255 characters.',
'content.required' => 'The content field is required.',
'content.string' => 'The content field must be a string.',
'author.required' => 'The author field is required.',
'author.string' => 'The author field must be a string.',
'author.max' => 'The author may not be greater than 255 characters.',
'summary.required' => 'The summary field is required.',
'summary.string' => 'The summary field must be a string.',
'published_date.required' => 'The published date field is required.',
'published_date.date' => 'The published date field must be a valid date.',
'status.required' => 'The status field is required.',
'status.in' => 'The status field must be either "active" or "inactive".',
'meta_title.string' => 'The meta title field must be a string.',
'meta_title.max' => 'The meta title may not be greater than 255 characters.',
'meta_description.string' => 'The meta description field must be a string.',
'meta_keywords.string' => 'The meta keywords field must be a string.',
'meta_keywords.max' => 'The meta keywords may not be greater than 255 characters.',
'image.mimes' => 'The image must be a file of type: png, jpg, jpeg.',
'slug.required' => 'The slug field is required.',
];
}
/**
* Determine if the user is authorized to make this request.
*/
public function authorize(): bool
{
return true;
// return auth()->user()->can('users.create');
}
}

View File

View File

@@ -0,0 +1,62 @@
<?php
namespace Modules\Blog\app\Models;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Modules\Blog\Database\factories\BlogFactory;
use Illuminate\Database\Eloquent\SoftDeletes;
class Blog extends Model
{
// use HasFactory;
use SoftDeletes;
protected $dates = ['published_date'];
/**
* The attributes that are mass assignable.
*/
protected $fillable = [
'uuid',
'title',
'summary',
'content',
'author',
'published_date',
'image',
'image_path',
'status',
'slug',
];
protected $casts = [
'published_date' => 'date:Y-m-d H:i:s',
];
/**
*
*/
public function getFullImageAttribute()
{
$result = null;
if ($this->image_path) {
$result = asset('storage/uploads/' . $this->image_path);
}
return $result;
}
public function blogMeta()
{
return $this->hasOne(BlogMeta::class, 'blog_id');
}
// protected static function newFactory(): BlogFactory
// {
// //return BlogFactory::new();
// }
}

View File

@@ -0,0 +1,33 @@
<?php
namespace Modules\Blog\app\Models;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Modules\Blog\Database\factories\BlogMetaFactory;
class BlogMeta extends Model
{
use SoftDeletes;
/**
* The attributes that are mass assignable.
*/
protected $fillable = [
'blog_id',
'meta_title',
'meta_description',
'meta_keywords',
];
public function blog()
{
return $this->belongsTo(Blog::class, 'blog_id');
}
// protected static function newFactory(): BlogMetaFactory
// {
// //return BlogMetaFactory::new();
// }
}

View File

View File

@@ -0,0 +1,114 @@
<?php
namespace Modules\Blog\app\Providers;
use Illuminate\Support\Facades\Blade;
use Illuminate\Support\ServiceProvider;
class BlogServiceProvider extends ServiceProvider
{
protected string $moduleName = 'Blog';
protected string $moduleNameLower = 'blog';
/**
* Boot the application events.
*/
public function boot(): void
{
$this->registerCommands();
$this->registerCommandSchedules();
$this->registerTranslations();
$this->registerConfig();
$this->registerViews();
$this->loadMigrationsFrom(module_path($this->moduleName, 'database/migrations'));
}
/**
* Register the service provider.
*/
public function register(): void
{
$this->app->register(RouteServiceProvider::class);
}
/**
* Register commands in the format of Command::class
*/
protected function registerCommands(): void
{
// $this->commands([]);
}
/**
* Register command Schedules.
*/
protected function registerCommandSchedules(): void
{
// $this->app->booted(function () {
// $schedule = $this->app->make(Schedule::class);
// $schedule->command('inspire')->hourly();
// });
}
/**
* Register translations.
*/
public function registerTranslations(): void
{
$langPath = resource_path('lang/modules/'.$this->moduleNameLower);
if (is_dir($langPath)) {
$this->loadTranslationsFrom($langPath, $this->moduleNameLower);
$this->loadJsonTranslationsFrom($langPath);
} else {
$this->loadTranslationsFrom(module_path($this->moduleName, 'lang'), $this->moduleNameLower);
$this->loadJsonTranslationsFrom(module_path($this->moduleName, 'lang'));
}
}
/**
* Register config.
*/
protected function registerConfig(): void
{
$this->publishes([module_path($this->moduleName, 'config/config.php') => config_path($this->moduleNameLower.'.php')], 'config');
$this->mergeConfigFrom(module_path($this->moduleName, 'config/config.php'), $this->moduleNameLower);
}
/**
* Register views.
*/
public function registerViews(): void
{
$viewPath = resource_path('views/modules/'.$this->moduleNameLower);
$sourcePath = module_path($this->moduleName, 'resources/views');
$this->publishes([$sourcePath => $viewPath], ['views', $this->moduleNameLower.'-module-views']);
$this->loadViewsFrom(array_merge($this->getPublishableViewPaths(), [$sourcePath]), $this->moduleNameLower);
$componentNamespace = str_replace('/', '\\', config('modules.namespace').'\\'.$this->moduleName.'\\'.config('modules.paths.generator.component-class.path'));
Blade::componentNamespace($componentNamespace, $this->moduleNameLower);
}
/**
* Get the services provided by the provider.
*/
public function provides(): array
{
return [];
}
private function getPublishableViewPaths(): array
{
$paths = [];
foreach (config('view.paths') as $path) {
if (is_dir($path.'/modules/'.$this->moduleNameLower)) {
$paths[] = $path.'/modules/'.$this->moduleNameLower;
}
}
return $paths;
}
}

View File

@@ -0,0 +1,59 @@
<?php
namespace Modules\Blog\app\Providers;
use Illuminate\Support\Facades\Route;
use Illuminate\Foundation\Support\Providers\RouteServiceProvider as ServiceProvider;
class RouteServiceProvider extends ServiceProvider
{
/**
* The module namespace to assume when generating URLs to actions.
*/
protected string $moduleNamespace = 'Modules\Blog\app\Http\Controllers';
/**
* Called before routes are registered.
*
* Register any model bindings or pattern based filters.
*/
public function boot(): void
{
parent::boot();
}
/**
* Define the routes for the application.
*/
public function map(): void
{
$this->mapApiRoutes();
$this->mapWebRoutes();
}
/**
* Define the "web" routes for the application.
*
* These routes all receive session state, CSRF protection, etc.
*/
protected function mapWebRoutes(): void
{
Route::middleware('web')
->namespace($this->moduleNamespace)
->group(module_path('Blog', '/routes/web.php'));
}
/**
* Define the "api" routes for the application.
*
* These routes are typically stateless.
*/
protected function mapApiRoutes(): void
{
Route::prefix('api')
->middleware('api')
->namespace($this->moduleNamespace)
->group(module_path('Blog', '/routes/api.php'));
}
}

View File

@@ -0,0 +1,152 @@
<?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;
}
}
}

View File

@@ -0,0 +1,66 @@
<?php
namespace Modules\Blog\app\Services;
use Illuminate\Support\Facades\Storage;
class FileManagementService
{
//-- store file
public static function storeFile($file, $uploadedFolderName, $model)
{
try {
$originalFileName = $file->getClientOriginalName();
$modifiedFileName = date('YmdHis') . "_" . uniqid() . "." . $originalFileName;
$file->storeAs($uploadedFolderName, $modifiedFileName, 'public_uploads'); // This line uses 'public_uploads' disk
$model->image = $modifiedFileName;
$model->image_path = $uploadedFolderName . '/' . $modifiedFileName;
$model->save();
} catch (\Throwable $th) {
report($th);
toastr()->error('Something went wrong.');
return redirect()->back();
}
}
//-- update file
public static function uploadFile($file, $uploadedFolderName ,$filePath, $model)
{
try {
if ($filePath && Storage::disk('public_uploads')->exists($filePath)) {
Storage::disk('public_uploads')->delete($filePath);
}
$originalFileName = $file->getClientOriginalName();
$modifiedFileName = date('YmdHis') . "_" . uniqid() . "." . $originalFileName;
$file->storeAs($uploadedFolderName, $modifiedFileName, 'public_uploads'); // This line uses 'public_uploads' disk
$model->image = $modifiedFileName;
$model->image_path = $uploadedFolderName . '/' . $modifiedFileName;
$model->save();
} catch (\Throwable $th) {
report($th);
toastr()->error('Something went wrong.');
return redirect()->back();
}
}
//-- delete file
public static function deleteFile($filePath)
{
try {
if ($filePath && Storage::disk('public_uploads')->exists($filePath)) {
Storage::disk('public_uploads')->delete($filePath);
} else {
toastr()->error('File Not wrong.');
}
} catch (\Throwable $th) {
report($th);
toastr()->error('Something went wrong while deleting the file.');
}
}
}