firstcommit
This commit is contained in:
0
Modules/Banner/app/Http/Controllers/.gitkeep
Normal file
0
Modules/Banner/app/Http/Controllers/.gitkeep
Normal file
142
Modules/Banner/app/Http/Controllers/BannerController.php
Normal file
142
Modules/Banner/app/Http/Controllers/BannerController.php
Normal file
@@ -0,0 +1,142 @@
|
||||
<?php
|
||||
|
||||
namespace Modules\Banner\app\Http\Controllers;
|
||||
|
||||
use Illuminate\Http\Request;
|
||||
use App\Http\Controllers\Controller;
|
||||
use Modules\Banner\app\Repositories\BannerRepository;
|
||||
use Modules\Banner\app\Http\Requests\CreateBannerRequest;
|
||||
use Modules\Banner\app\Http\Requests\UpdateBannerRequest;
|
||||
|
||||
class BannerController extends Controller
|
||||
{
|
||||
protected $bannerRepository;
|
||||
|
||||
public function __construct()
|
||||
{
|
||||
$this->bannerRepository = new BannerRepository;
|
||||
}
|
||||
|
||||
/**
|
||||
* 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') : [];
|
||||
$banners = $this->bannerRepository->allBanners($perPage, $filter);
|
||||
|
||||
return view('banner::index', compact('banners'));
|
||||
}
|
||||
|
||||
/**
|
||||
* Show the form for creating a new resource.
|
||||
*/
|
||||
public function create()
|
||||
{
|
||||
return view('banner::create');
|
||||
}
|
||||
|
||||
/**
|
||||
* Store a newly created resource in storage.
|
||||
*/
|
||||
public function store(CreateBannerRequest $request)
|
||||
{
|
||||
try {
|
||||
$validated = $request->validated();
|
||||
$this->bannerRepository->storeBanner($validated);
|
||||
|
||||
toastr()->success('Banner created successfully.');
|
||||
|
||||
return redirect()->route('cms.banners.index');
|
||||
} catch (\Throwable $th) {
|
||||
report($th);
|
||||
toastr()->error('Something went wrong.');
|
||||
return back();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Show the specified resource.
|
||||
*/
|
||||
public function show($id)
|
||||
{
|
||||
try {
|
||||
$banner = $this->bannerRepository->findBannerById($id);
|
||||
if (!$banner) {
|
||||
toastr()->error('Banner not found.');
|
||||
return back();
|
||||
}
|
||||
|
||||
return view('banner::show', compact('banner'));
|
||||
} catch (\Throwable $th) {
|
||||
report($th);
|
||||
toastr()->error('Something went wrong.');
|
||||
return back();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Show the form for editing the specified resource.
|
||||
*/
|
||||
public function edit($id)
|
||||
{
|
||||
$banner = $this->bannerRepository->findBannerById($id);
|
||||
if (!$banner) {
|
||||
toastr()->error('Banner not found.');
|
||||
return back();
|
||||
}
|
||||
|
||||
return view('banner::edit', compact('banner'));
|
||||
}
|
||||
|
||||
/**
|
||||
* Update the specified resource in storage.
|
||||
*/
|
||||
public function update(UpdateBannerRequest $request, $uuid)
|
||||
{
|
||||
$validated = $request->validated();
|
||||
|
||||
try {
|
||||
$banner = $this->bannerRepository->updateBanner($validated, $uuid);
|
||||
|
||||
if (!$banner) {
|
||||
toastr()->error('Banner not found !');
|
||||
return back();
|
||||
}
|
||||
|
||||
toastr()->success('Banner updated successfully.');
|
||||
|
||||
return redirect()->route('cms.banners.index');
|
||||
} catch (\Throwable $th) {
|
||||
report($th);
|
||||
toastr()->error('Something went wrong.');
|
||||
return back();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Remove the specified resource from storage.
|
||||
*/
|
||||
public function destroy($uuid)
|
||||
{
|
||||
try {
|
||||
$banner = $this->bannerRepository->deleteBanner($uuid);
|
||||
|
||||
if (!$banner) {
|
||||
toastr()->error('Banner not found.');
|
||||
return back();
|
||||
}
|
||||
|
||||
toastr()->success('Banner deleted successfully.');
|
||||
|
||||
return redirect()->route('cms.banners.index');
|
||||
} catch (\Throwable $th) {
|
||||
report($th);
|
||||
toastr()->error('Something went wrong.');
|
||||
return back();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
}
|
0
Modules/Banner/app/Http/Middleware/.gitkeep
Normal file
0
Modules/Banner/app/Http/Middleware/.gitkeep
Normal file
0
Modules/Banner/app/Http/Requests/.gitkeep
Normal file
0
Modules/Banner/app/Http/Requests/.gitkeep
Normal file
40
Modules/Banner/app/Http/Requests/CreateBannerRequest.php
Normal file
40
Modules/Banner/app/Http/Requests/CreateBannerRequest.php
Normal file
@@ -0,0 +1,40 @@
|
||||
<?php
|
||||
|
||||
namespace Modules\Banner\app\Http\Requests;
|
||||
|
||||
use Illuminate\Foundation\Http\FormRequest;
|
||||
use Illuminate\Contracts\Validation\Validator;
|
||||
|
||||
class CreateBannerRequest extends FormRequest
|
||||
{
|
||||
/**
|
||||
* Get the validation rules that apply to the request.
|
||||
*/
|
||||
public function rules(): array
|
||||
{
|
||||
return [
|
||||
'title' => 'required',
|
||||
'link' => 'sometimes',
|
||||
'ordering' => 'required|integer|unique:banners,ordering',
|
||||
'caption' => 'sometimes',
|
||||
'status' => 'required',
|
||||
'image' => 'sometimes|nullable',
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* Determine if the user is authorized to make this request.
|
||||
*/
|
||||
public function authorize(): bool
|
||||
{
|
||||
return true;
|
||||
// return auth()->user()->can('users.create');
|
||||
}
|
||||
|
||||
protected function failedValidation(Validator $validator)
|
||||
{
|
||||
$message = $validator->errors()->first();
|
||||
toastr()->error($message);
|
||||
parent::failedValidation($validator);
|
||||
}
|
||||
}
|
42
Modules/Banner/app/Http/Requests/UpdateBannerRequest.php
Normal file
42
Modules/Banner/app/Http/Requests/UpdateBannerRequest.php
Normal file
@@ -0,0 +1,42 @@
|
||||
<?php
|
||||
|
||||
namespace Modules\Banner\app\Http\Requests;
|
||||
|
||||
use Illuminate\Foundation\Http\FormRequest;
|
||||
use Modules\Banner\app\Models\Banner;
|
||||
use Illuminate\Contracts\Validation\Validator;
|
||||
|
||||
class UpdateBannerRequest extends FormRequest
|
||||
{
|
||||
/**
|
||||
* Get the validation rules that apply to the request.
|
||||
*/
|
||||
public function rules(): array
|
||||
{
|
||||
$banner = Banner::where('uuid', $this->route('uuid'))->first();
|
||||
|
||||
return [
|
||||
'title' => 'required',
|
||||
'link' => 'sometimes',
|
||||
'ordering' => 'required|integer|unique:banners,ordering,' . ($banner ? $banner->id : 'NULL') . ',id',
|
||||
'caption' => 'sometimes',
|
||||
'status' => 'required',
|
||||
'image' => 'sometimes|nullable',
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* Determine if the user is authorized to make this request.
|
||||
*/
|
||||
public function authorize(): bool
|
||||
{
|
||||
return true;
|
||||
}
|
||||
|
||||
protected function failedValidation(Validator $validator)
|
||||
{
|
||||
$message = $validator->errors()->first();
|
||||
toastr()->error($message);
|
||||
parent::failedValidation($validator);
|
||||
}
|
||||
}
|
0
Modules/Banner/app/Models/.gitkeep
Normal file
0
Modules/Banner/app/Models/.gitkeep
Normal file
47
Modules/Banner/app/Models/Banner.php
Normal file
47
Modules/Banner/app/Models/Banner.php
Normal file
@@ -0,0 +1,47 @@
|
||||
<?php
|
||||
|
||||
namespace Modules\Banner\app\Models;
|
||||
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
use Illuminate\Database\Eloquent\SoftDeletes;
|
||||
use Modules\Banner\Database\factories\BannerFactory;
|
||||
use Illuminate\Database\Eloquent\Factories\HasFactory;
|
||||
|
||||
class Banner extends Model
|
||||
{
|
||||
// use HasFactory;
|
||||
use SoftDeletes;
|
||||
|
||||
/**
|
||||
* The attributes that are mass assignable.
|
||||
*/
|
||||
protected $fillable = [
|
||||
'uuid',
|
||||
'title',
|
||||
'ordering',
|
||||
'caption',
|
||||
'link',
|
||||
'image',
|
||||
'image_path',
|
||||
'status',
|
||||
];
|
||||
|
||||
// protected static function newFactory(): BannerFactory
|
||||
// {
|
||||
// //return BannerFactory::new();
|
||||
// }
|
||||
|
||||
/**
|
||||
*
|
||||
*/
|
||||
public function getFullImageAttribute()
|
||||
{
|
||||
$result = null;
|
||||
|
||||
if($this->image_path) {
|
||||
$result = asset('storage/uploads/' . $this->image_path);
|
||||
}
|
||||
|
||||
return $result;
|
||||
}
|
||||
}
|
0
Modules/Banner/app/Providers/.gitkeep
Normal file
0
Modules/Banner/app/Providers/.gitkeep
Normal file
114
Modules/Banner/app/Providers/BannerServiceProvider.php
Normal file
114
Modules/Banner/app/Providers/BannerServiceProvider.php
Normal file
@@ -0,0 +1,114 @@
|
||||
<?php
|
||||
|
||||
namespace Modules\Banner\app\Providers;
|
||||
|
||||
use Illuminate\Support\Facades\Blade;
|
||||
use Illuminate\Support\ServiceProvider;
|
||||
|
||||
class BannerServiceProvider extends ServiceProvider
|
||||
{
|
||||
protected string $moduleName = 'Banner';
|
||||
|
||||
protected string $moduleNameLower = 'banner';
|
||||
|
||||
/**
|
||||
* 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;
|
||||
}
|
||||
}
|
59
Modules/Banner/app/Providers/RouteServiceProvider.php
Normal file
59
Modules/Banner/app/Providers/RouteServiceProvider.php
Normal file
@@ -0,0 +1,59 @@
|
||||
<?php
|
||||
|
||||
namespace Modules\Banner\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\Banner\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('Banner', '/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('Banner', '/routes/api.php'));
|
||||
}
|
||||
}
|
122
Modules/Banner/app/Repositories/BannerRepository.php
Normal file
122
Modules/Banner/app/Repositories/BannerRepository.php
Normal file
@@ -0,0 +1,122 @@
|
||||
<?php
|
||||
|
||||
namespace Modules\Banner\app\Repositories;
|
||||
|
||||
use Illuminate\Support\Str;
|
||||
use Illuminate\Support\Facades\DB;
|
||||
use Modules\Banner\app\Models\Banner;
|
||||
use Modules\Banner\app\Services\FileManagementService;
|
||||
|
||||
class BannerRepository
|
||||
{
|
||||
public function allBanners($perPage = null, $filter = [], $sort = ['by' => 'id', 'sort' => 'DESC'])
|
||||
{
|
||||
return Banner::when(array_keys($filter, true), function ($query) use ($filter) {
|
||||
if (!empty($filter['title'])) {
|
||||
$query->where('title', $filter['title']);
|
||||
}
|
||||
if (!empty($filter['caption'])) {
|
||||
$query->where('caption', 'like', '%' . $filter['caption'] . '%');
|
||||
}
|
||||
})
|
||||
->orderBy($sort['by'], $sort['sort'])
|
||||
->paginate($perPage ?: env('PAGE_LIMIT', 999));
|
||||
}
|
||||
|
||||
public function findBannerById($uuid)
|
||||
{
|
||||
return Banner::where('uuid', $uuid)->first();
|
||||
}
|
||||
|
||||
public function storeBanner($validated)
|
||||
{
|
||||
DB::beginTransaction();
|
||||
try {
|
||||
$banner = new Banner();
|
||||
$banner->uuid = Str::uuid();
|
||||
$banner->title = $validated['title'];
|
||||
$banner->ordering = $validated['ordering'];
|
||||
$banner->link = $validated['link'];
|
||||
$banner->caption = $validated['caption'];
|
||||
$banner->status = $validated['status'];
|
||||
$banner->save();
|
||||
|
||||
//-- store image
|
||||
if (isset($validated['image']) && $validated['image']->isValid()) {
|
||||
FileManagementService::storeFile(
|
||||
file: $validated['image'],
|
||||
uploadedFolderName: 'banners',
|
||||
model: $banner
|
||||
);
|
||||
}
|
||||
DB::commit();
|
||||
|
||||
return $banner;
|
||||
} catch (\Throwable $th) {
|
||||
report($th);
|
||||
DB::rollback();
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
public function updateBanner($validated, $uuid)
|
||||
{
|
||||
try {
|
||||
$banner = $this->findBannerById($uuid);
|
||||
|
||||
if (!$banner) {
|
||||
return null;
|
||||
}
|
||||
|
||||
$banner->title = $validated['title'];
|
||||
$banner->link = $validated['link'];
|
||||
$banner->ordering = $validated['ordering'];
|
||||
$banner->caption = $validated['caption'];
|
||||
$banner->status = $validated['status'];
|
||||
$banner->save();
|
||||
|
||||
//-- Update image
|
||||
if (isset($validated['image']) && $validated['image']->isValid()) {
|
||||
FileManagementService::uploadFile(
|
||||
file: $validated['image'],
|
||||
uploadedFolderName: 'banners',
|
||||
filePath: $banner->image_path,
|
||||
model: $banner
|
||||
);
|
||||
}
|
||||
|
||||
return $banner;
|
||||
} catch (\Throwable $th) {
|
||||
report($th);
|
||||
DB::transaction();
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
public function deleteBanner($uuid)
|
||||
{
|
||||
DB::beginTransaction();
|
||||
try {
|
||||
$banner = $this->findBannerById($uuid);
|
||||
|
||||
if (!$banner) {
|
||||
return null;
|
||||
}
|
||||
|
||||
// Delete the image file associated with the banner
|
||||
if ($banner->image_path !== null) {
|
||||
FileManagementService::deleteFile($banner->image_path);
|
||||
}
|
||||
|
||||
$banner->delete();
|
||||
DB::commit();
|
||||
|
||||
return true;
|
||||
} catch (\Throwable $th) {
|
||||
DB::rollback();
|
||||
report($th);
|
||||
return null;
|
||||
}
|
||||
}
|
||||
}
|
66
Modules/Banner/app/Services/FileManagementService.php
Normal file
66
Modules/Banner/app/Services/FileManagementService.php
Normal file
@@ -0,0 +1,66 @@
|
||||
<?php
|
||||
|
||||
namespace Modules\Banner\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.');
|
||||
}
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user