employee module

This commit is contained in:
2024-04-07 17:39:18 +05:45
parent 4f34db3381
commit 6b6696ded4
10 changed files with 433 additions and 427 deletions

View File

@ -5,15 +5,24 @@ namespace Modules\Employee\Http\Controllers;
use App\Http\Controllers\Controller;
use Illuminate\Http\RedirectResponse;
use Illuminate\Http\Request;
use Modules\Employee\Repositories\EmployeeInterface;
class EmployeeController extends Controller
{
private EmployeeInterface $employeeRepository;
public function __construct(EmployeeInterface $employeeRepository)
{
$this->employeeRepository = $employeeRepository;
}
/**
* Display a listing of the resource.
*/
public function index()
{
return view('employee::index');
$data['employees'] = $this->employeeRepository->findAll();
return view('employee::index', $data);
}
/**
@ -28,9 +37,25 @@ class EmployeeController extends Controller
/**
* Store a newly created resource in storage.
*/
public function store(Request $request): RedirectResponse
public function store(Request $request)
{
//
$inputData = $request->only(['first_name', 'last_name']);
// try {
if ($request->hasFile('profile_pic')) {
$fileName = time() . '_' . $request->profile_pic->getClientOriginalName();
$filePath = $request->file('profile_pic')->storeAs('uploads', $fileName, 'public');
$inputData['profile_picture'] = time() . '_' . $request->profile_pic->getClientOriginalName();
// $fileModel->file_path = '/storage/' . $filePath;
// $fileModel->save();
}
$this->employeeRepository->create($inputData);
toastr()->success('Employee Created Succesfully');
// } catch (\Throwable $th) {
// toastr()->error($th->getMessage());
// }
return redirect()->route('employee.index');
}
/**
@ -38,6 +63,7 @@ class EmployeeController extends Controller
*/
public function show($id)
{
return view('employee::show');
}
@ -46,7 +72,9 @@ class EmployeeController extends Controller
*/
public function edit($id)
{
return view('employee::edit');
$data['title'] = 'Edit Employee';
$data['employee'] = $this->employeeRepository->getEmployeeById($id);
return view('employee::edit', $data);
}
/**
@ -54,7 +82,21 @@ class EmployeeController extends Controller
*/
public function update(Request $request, $id): RedirectResponse
{
//
$inputData = $request->only(['first_name', 'last_name']);
// try {
if ($request->hasFile('profile_pic')) {
$fileName = time() . '_' . $request->profile_pic->getClientOriginalName();
$filePath = $request->file('profile_pic')->storeAs('uploads', $fileName, 'public');
$inputData['profile_picture'] = time() . '_' . $request->profile_pic->getClientOriginalName();
}
$this->employeeRepository->update($id, $inputData);
toastr()->success('Employee Created Succesfully');
// } catch (\Throwable $th) {
// toastr()->error($th->getMessage());
// }
return redirect()->route('employee.index');
}
/**

View File

@ -0,0 +1,13 @@
<?php
namespace Modules\Employee\Models;
use Illuminate\Database\Eloquent\Model;
class Employee extends Model
{
protected $table = 'tbl_employees';
protected $primaryKey = 'id';
protected $guarded = [];
}

View File

@ -4,6 +4,8 @@ namespace Modules\Employee\Providers;
use Illuminate\Support\Facades\Blade;
use Illuminate\Support\ServiceProvider;
use Modules\Employee\Repositories\EmployeeInterface;
use Modules\Employee\Repositories\EmployeeRepository;
class EmployeeServiceProvider extends ServiceProvider
{
@ -29,6 +31,7 @@ class EmployeeServiceProvider extends ServiceProvider
*/
public function register(): void
{
$this->app->bind(EmployeeInterface::class, EmployeeRepository::class);
$this->app->register(RouteServiceProvider::class);
}
@ -56,7 +59,7 @@ class EmployeeServiceProvider extends ServiceProvider
*/
public function registerTranslations(): void
{
$langPath = resource_path('lang/modules/'.$this->moduleNameLower);
$langPath = resource_path('lang/modules/' . $this->moduleNameLower);
if (is_dir($langPath)) {
$this->loadTranslationsFrom($langPath, $this->moduleNameLower);
@ -72,7 +75,7 @@ class EmployeeServiceProvider extends ServiceProvider
*/
protected function registerConfig(): void
{
$this->publishes([module_path($this->moduleName, 'config/config.php') => config_path($this->moduleNameLower.'.php')], 'config');
$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);
}
@ -81,14 +84,14 @@ class EmployeeServiceProvider extends ServiceProvider
*/
public function registerViews(): void
{
$viewPath = resource_path('views/modules/'.$this->moduleNameLower);
$viewPath = resource_path('views/modules/' . $this->moduleNameLower);
$sourcePath = module_path($this->moduleName, 'resources/views');
$this->publishes([$sourcePath => $viewPath], ['views', $this->moduleNameLower.'-module-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','')));
$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);
}
@ -104,8 +107,8 @@ class EmployeeServiceProvider extends ServiceProvider
{
$paths = [];
foreach (config('view.paths') as $path) {
if (is_dir($path.'/modules/'.$this->moduleNameLower)) {
$paths[] = $path.'/modules/'.$this->moduleNameLower;
if (is_dir($path . '/modules/' . $this->moduleNameLower)) {
$paths[] = $path . '/modules/' . $this->moduleNameLower;
}
}

View File

@ -0,0 +1,12 @@
<?php
namespace Modules\Employee\Repositories;
interface EmployeeInterface
{
public function findAll();
public function getEmployeeById($employeeId);
public function delete($employeeId);
public function create($EmployeeDetails);
public function update($employeeId, array $newDetails);
}

View File

@ -0,0 +1,49 @@
<?php
namespace Modules\Employee\Repositories;
use Modules\Employee\Models\Employee;
class EmployeeRepository implements EmployeeInterface
{
public function findAll()
{
return Employee::get();
}
public function getEmployeeById($employeeId)
{
return Employee::findOrFail($employeeId);
}
public function delete($employeeId)
{
Employee::destroy($employeeId);
}
public function create($employeeDetails)
{
// dd($employeeDetails);
return Employee::create($employeeDetails);
}
public function update($employeeId, array $newDetails)
{
return Employee::whereId($employeeId)->update($newDetails);
}
// public function uploadImage($file)
// {
// if ($req->file()) {
// $fileName = time() . '_' . $req->file->getClientOriginalName();
// $filePath = $req->file('file')->storeAs('uploads', $fileName, 'public');
// $fileModel->name = time() . '_' . $req->file->getClientOriginalName();
// $fileModel->file_path = '/storage/' . $filePath;
// $fileModel->save();
// return back()
// ->with('success', 'File has been uploaded.')
// ->with('file', $fileName);
// }
// }
}