This commit is contained in:
2025-12-28 12:16:05 +05:45
commit 7c46ec6731
3358 changed files with 467149 additions and 0 deletions

View File

@@ -0,0 +1,154 @@
<?php
namespace Modules\User\Http\Controllers;
use App\Http\Controllers\Controller;
use App\Models\User;
use Illuminate\Http\Request;
use Illuminate\Validation\Rule;
use Illuminate\Validation\Rules;
use Modules\User\Services\UserService;
use Yajra\DataTables\Facades\DataTables;
class UserController extends Controller
{
protected $userService;
public function __construct(UserService $userService)
{
$this->userService = $userService;
}
/**
* Display a listing of the resource.
*/
public function index(?int $id = null)
{
$isEditing = !is_null($id);
$user = $isEditing ? $this->userService->getUserById($id) : null;
if (request()->ajax()) {
$model = user::query()->orderBy('order');
return DataTables::eloquent($model)
->addIndexColumn()
->setRowClass('tableRow')
->editColumn('is_admin', function (User $user) {
return $user->is_admin ? 'Yes' : 'No';
})
->addColumn('action', 'user::user.datatable.action')
->rawColumns(['action'])
->toJson();
}
return view('user::user.index', [
'user' => $user,
'title' => $isEditing ? 'Edit User' : 'Add User',
]);
}
/**
* Show the form for creating a new resource.
*/
public function create()
{
//
}
/**
* Store a newly created resource in storage.
*/
public function store(Request $request)
{
$isEditing = $request->has('id');
if ($isEditing) {
$validated = $request->validate([
'name' => ['required', 'string', 'max:255'],
'email' => [
'required',
'string',
'lowercase',
'email',
'max:255',
Rule::unique(User::class)->ignore($request->id),
],
'password' => ['nullable', 'confirmed', Rules\Password::defaults()],
'is_admin' => ['nullable'],
]);
$user = $this->userService->updateUser($request->id, $validated);
flash()->success("User for {$user->name} has been updated.");
return to_route('user.index');
}
$maxOrder = User::max('order');
$order = $maxOrder ? ++$maxOrder : 1;
$request->mergeIfMissing([
'order' => $order
]);
$validated = $request->validate([
'name' => ['required', 'string', 'max:255'],
'email' => ['required', 'string', 'lowercase', 'email', 'max:255', 'unique:' . User::class],
'password' => ['required', 'confirmed', Rules\Password::defaults()],
'is_admin' => ['nullable'],
'order' => ['integer'],
]);
$user = $this->userService->storeUser($validated);
flash()->success("User for {$user->name} has been created.");
return to_route('user.index');
}
/**
* Show the specified resource.
*/
public function show($id)
{
//
}
/**
* Show the form for editing the specified resource.
*/
public function edit($id)
{
$user = $this->userService->getUserById($id);
return view('user::user.edit', [
'user' => $user,
]);
}
/**
* Update the specified resource in storage.
*/
public function update(Request $request, $id)
{
//
}
/**
* Remove the specified resource from storage.
*/
public function destroy($id)
{
$user = $this->userService->deleteUser($id);
return response()->json(['status' => 200, 'message' => "User has been deleted."], 200);
}
public function reorder(Request $request)
{
$users = $this->userService->getAllUsers();
foreach ($users as $user) {
foreach ($request->order as $order) {
if ($order['id'] == $user->id) {
$user->update(['order' => $order['position']]);
}
}
}
return response(['status' => true, 'message' => 'Reordered successfully'], 200);
}
}

View File

View File

View File

@@ -0,0 +1,30 @@
<?php
namespace Modules\User\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.
*/
protected function configureEmailVerification(): void
{
//
}
}

View File

@@ -0,0 +1,50 @@
<?php
namespace Modules\User\Providers;
use Illuminate\Support\Facades\Route;
use Illuminate\Foundation\Support\Providers\RouteServiceProvider as ServiceProvider;
class RouteServiceProvider extends ServiceProvider
{
protected string $name = 'User';
/**
* 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($this->name, '/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($this->name, '/routes/api.php'));
}
}

View File

@@ -0,0 +1,118 @@
<?php
namespace Modules\User\Providers;
use Illuminate\Support\Facades\Blade;
use Illuminate\Support\ServiceProvider;
use Nwidart\Modules\Traits\PathNamespace;
class UserServiceProvider extends ServiceProvider
{
use PathNamespace;
protected string $name = 'User';
protected string $nameLower = 'user';
/**
* Boot the application events.
*/
public function boot(): void
{
$this->registerCommands();
$this->registerCommandSchedules();
$this->registerTranslations();
$this->registerConfig();
$this->registerViews();
$this->loadMigrationsFrom(module_path($this->name, 'database/migrations'));
}
/**
* Register the service provider.
*/
public function register(): void
{
$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->nameLower);
if (is_dir($langPath)) {
$this->loadTranslationsFrom($langPath, $this->nameLower);
$this->loadJsonTranslationsFrom($langPath);
} else {
$this->loadTranslationsFrom(module_path($this->name, 'lang'), $this->nameLower);
$this->loadJsonTranslationsFrom(module_path($this->name, 'lang'));
}
}
/**
* Register config.
*/
protected function registerConfig(): void
{
$this->publishes([module_path($this->name, 'config/config.php') => config_path($this->nameLower.'.php')], 'config');
$this->mergeConfigFrom(module_path($this->name, 'config/config.php'), $this->nameLower);
}
/**
* Register views.
*/
public function registerViews(): void
{
$viewPath = resource_path('views/modules/'.$this->nameLower);
$sourcePath = module_path($this->name, 'resources/views');
$this->publishes([$sourcePath => $viewPath], ['views', $this->nameLower.'-module-views']);
$this->loadViewsFrom(array_merge($this->getPublishableViewPaths(), [$sourcePath]), $this->nameLower);
$componentNamespace = $this->module_namespace($this->name, $this->app_path(config('modules.paths.generator.component-class.path')));
Blade::componentNamespace($componentNamespace, $this->nameLower);
}
/**
* 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->nameLower)) {
$paths[] = $path.'/modules/'.$this->nameLower;
}
}
return $paths;
}
}

View File

View File

@@ -0,0 +1,72 @@
<?php
namespace Modules\User\Services;
use App\Models\User;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Hash;
class UserService
{
public function getAllUsers(array $filters = [])
{
$query = User::query();
if (isset($filters['name'])) {
$query->where('name', 'like', '%' . $filters['name'] . '%');
}
if (isset($filters['email'])) {
$query->where('email', 'like', '%' . $filters['email'] . '%');
}
return $query->get();
}
public function storeUser(array $userData): User
{
return DB::transaction(function () use ($userData) {
$user = User::create([
'name' => $userData['name'],
'email' => $userData['email'],
'password' => Hash::make($userData['password']),
'is_admin' => $userData['is_admin'] ?? false,
]);
return $user;
});
}
public function getUserById(int $id)
{
return User::findOrFail($id);
}
public function updateUser(int $id, array $userData)
{
$user = $this->getUserById($id);
return DB::transaction(function () use ($user, $userData) {
$user->name = $userData['name'];
$user->email = $userData['email'];
$user->is_admin = !empty($userData['is_admin']);
if (isset($userData['password'])) {
$user->password = Hash::make($userData['password']);
}
$user->save();
return $user;
});
}
public function deleteUser(int $id)
{
return DB::transaction(function () use ($id) {
$user = $this->getUserById($id);
$user->delete();
return true;
});
}
}