first change

This commit is contained in:
2025-07-27 17:40:56 +05:45
commit f8b9a6725b
3152 changed files with 229528 additions and 0 deletions

View File

@@ -0,0 +1,125 @@
<?php
namespace Modules\Product\Http\Controllers;
use App\Http\Controllers\Controller;
use Illuminate\Http\RedirectResponse;
use Illuminate\Http\Request;
use Illuminate\Http\Response;
use Modules\Client\Interfaces\ClientInterface;
use Modules\Product\Interfaces\ProductInterface;
use Modules\Product\Models\Product;
use Yajra\DataTables\Facades\DataTables;
class ProductController extends Controller
{
private $product;
private $client;
public function __construct(ProductInterface $product, ClientInterface $client)
{
$this->product = $product;
$this->client = $client;
}
/**
* Display a listing of the resource.
*/
public function index(Request $request)
{
$data['title'] = 'Product';
$data['status'] = Product::STATUS;
$data['clients'] = $this->client->pluck();
if ($request->ajax()) {
$model = Product::query();
return DataTables::eloquent($model)
->addIndexColumn()
->setRowClass('{{"text-center align-middle"}}')
->addColumn('client', function (Product $product) {
return $product->client?->name;
})
->addColumn('status', '{!! $status_name !!}')
->addColumn('action', 'product::products.datatables.action-btn')
->rawColumns(['client', 'action', 'status','desc'])
->make(true);
// ->toJson();
}
return view('product::products.index', $data);
}
/**
* Show the form for creating a new resource.
*/
public function create()
{
$data['title'] = 'Create Product';
$data['editable'] = false;
return view('product::products.create', $data);
}
/**
* Store a newly created resource in storage.
*/
public function store(Request $request): RedirectResponse
{
try {
$this->product->create($request->all());
return redirect()->route('product.index')->with('sucess', 'Product has been created!');
} catch (\Throwable $th) {
return redirect()->back()->withError($th->getMessage());
}
}
/**
* Show the specified resource.
*/
public function show($id)
{
$data['title'] = 'Product List';
$data['product'] = $this->product->getProductById($id);
return view('product::products.show', $data);
}
/**
* Show the form for editing the specified resource.
*/
public function edit($id)
{
$data['title'] = 'Edit Product';
$data['editable'] = true;
$data['status'] = Product::STATUS;
$data['product'] = $this->product->getProductById($id);
$data['clients'] = $this->client->pluck();
return view('product::products.index', $data);
}
/**
* Update the specified resource in storage.
*/
public function update(Request $request, $id): RedirectResponse
{
try {
$this->product->update($id, $request->except(['_token', '_method']));
return redirect()->route('product.index')->with('success', 'Product has been updated!');
} catch (\Throwable $th) {
return redirect()->back()->withError($th->getMessage());
}
}
/**
* Remove the specified resource from storage.
*/
public function destroy($id)
{
try {
$this->product->delete($id);
return response()->json(['status' => 200, 'message' => 'Product has been deleted!'], 200);
} catch (\Throwable $th) {
return response()->json(['status' => 500, 'message' => 'Product to delete!', 'error' => $th->getMessage()], 500);
}
}
}

View File

@@ -0,0 +1,16 @@
<?php
namespace Modules\Product\Interfaces;
interface ProductInterface
{
public function findAll();
public function getProductById($productId);
public function getProductList();
public function create(array $productDetails);
public function update($productId, array $newDetails);
public function delete($productId);
public function pluck();
public function count();
public function client();
}

View File

@@ -0,0 +1,32 @@
<?php
namespace Modules\Product\Models;
use App\Traits\CreatedUpdatedBy;
use App\Traits\StatusTrait;
use Illuminate\Database\Eloquent\Model;
use Modules\Client\Models\Client;
class Product extends Model
{
use StatusTrait, CreatedUpdatedBy;
/**
* The attributes that are mass assignable.
*/
protected $fillable = [
'name',
'description',
'usps',
'client_id',
'status',
'createdby',
'updatedby',
];
protected $appends = ['status_name'];
public function client()
{
return $this->belongsTo(Client::class, 'client_id');
}
}

View File

View File

@@ -0,0 +1,32 @@
<?php
namespace Modules\Product\Providers;
use Illuminate\Foundation\Support\Providers\EventServiceProvider as ServiceProvider;
class EventServiceProvider extends ServiceProvider
{
/**
* The event handler mappings for the application.
*
* @var array<string, array<int, string>>
*/
protected $listen = [];
/**
* Indicates if events should be discovered.
*
* @var bool
*/
protected static $shouldDiscoverEvents = true;
/**
* Configure the proper event listeners for email verification.
*
* @return void
*/
protected function configureEmailVerification(): void
{
}
}

View File

@@ -0,0 +1,123 @@
<?php
namespace Modules\Product\Providers;
use Illuminate\Support\Facades\Blade;
use Illuminate\Support\ServiceProvider;
use Modules\Product\Interfaces\ProductInterface;
use Modules\Product\Repositories\ProductRepository;
class ProductServiceProvider extends ServiceProvider
{
protected string $moduleName = 'Product';
protected string $moduleNameLower = 'product';
/**
* 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->bind(ProductInterface::class, ProductRepository::class);
$this->app->register(EventServiceProvider::class);
$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 . '\\' . ltrim(config('modules.paths.generator.component-class.path'), config('modules.paths.app_folder', '')));
Blade::componentNamespace($componentNamespace, $this->moduleNameLower);
}
/**
* Get the services provided by the provider.
*
* @return array<string>
*/
public function provides(): array
{
return [];
}
/**
* @return array<string>
*/
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,49 @@
<?php
namespace Modules\Product\Providers;
use Illuminate\Support\Facades\Route;
use Illuminate\Foundation\Support\Providers\RouteServiceProvider as ServiceProvider;
class RouteServiceProvider extends ServiceProvider
{
/**
* 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')->group(module_path('Product', '/routes/web.php'));
}
/**
* Define the "api" routes for the application.
*
* These routes are typically stateless.
*/
protected function mapApiRoutes(): void
{
Route::middleware('api')->prefix('api')->name('api.')->group(module_path('Product', '/routes/api.php'));
}
}

View File

@@ -0,0 +1,60 @@
<?php
namespace Modules\Product\Repositories;
use Modules\Product\Interfaces\ProductInterface;
use Modules\Product\Models\Product;
class ProductRepository implements ProductInterface
{
public function findAll()
{
return Product::get();
}
public function getProductById($productId)
{
return Product::findOrFail($productId);
}
public function getProductList()
{
$products = Product::with('client:id,name')->get();
$keyed = $products->mapWithKeys(function (Product $item) {
return [$item->id => $item->name.' ('.$item->client?->name.')'];
});
return $keyed->all();
}
public function create(array $productDetails)
{
return Product::create($productDetails);
}
public function update($productId, array $newDetails)
{
return Product::whereId($productId)->update($newDetails);
}
public function delete($productId)
{
return Product::destroy($productId);
}
public function pluck()
{
return Product::pluck('name', 'id');
}
public function count()
{
return Product::count();
}
public function client()
{
return Product::with('client');
}
}