first change
This commit is contained in:
93
Modules/Employee/app/Repositories/EmployeeRepository.php
Normal file
93
Modules/Employee/app/Repositories/EmployeeRepository.php
Normal file
@@ -0,0 +1,93 @@
|
||||
<?php
|
||||
|
||||
namespace Modules\Employee\Repositories;
|
||||
|
||||
use Illuminate\Http\Request;
|
||||
use Modules\Employee\Interfaces\EmployeeInterface;
|
||||
use Modules\Employee\Models\Employee;
|
||||
|
||||
class EmployeeRepository implements EmployeeInterface
|
||||
{
|
||||
public function findAll($request, callable $query = null, bool $paginate = false, int $limit = 10)
|
||||
{
|
||||
$baseQuery = Employee::query();
|
||||
|
||||
if ($request->filled('search')) {
|
||||
$baseQuery->whereAny(
|
||||
[
|
||||
'first_name',
|
||||
'middle_name',
|
||||
'last_name',
|
||||
],
|
||||
'LIKE',
|
||||
"%{$request->search}%"
|
||||
);
|
||||
}
|
||||
|
||||
if ($request->filled('email')) {
|
||||
$baseQuery->where('email', 'LIKE', "%{$$request->email}%");
|
||||
}
|
||||
|
||||
if ($query) {
|
||||
$query($baseQuery);
|
||||
}
|
||||
|
||||
if ($paginate) {
|
||||
return $baseQuery->paginate($limit);
|
||||
}
|
||||
|
||||
return $baseQuery->get();
|
||||
}
|
||||
|
||||
public function findById($id, callable $query = null)
|
||||
{
|
||||
$baseQuery = Employee::query();
|
||||
|
||||
if (is_callable($query)) {
|
||||
$query($baseQuery);
|
||||
}
|
||||
|
||||
return $baseQuery->where('id', $id)->firstOrFail();
|
||||
|
||||
}
|
||||
|
||||
public function delete($id)
|
||||
{
|
||||
$employee = $this->findById($id);
|
||||
$employee->delete();
|
||||
return $employee;
|
||||
}
|
||||
|
||||
public function create(array $data)
|
||||
{
|
||||
$employee = Employee::create($data);
|
||||
return $employee;
|
||||
}
|
||||
|
||||
public function update($id, array $data)
|
||||
{
|
||||
$employee = $this->findById($id);
|
||||
$employee->update($data);
|
||||
return $employee;
|
||||
}
|
||||
|
||||
public function pluck(callable $query = null)
|
||||
{
|
||||
$baseQuery = Employee::query();
|
||||
|
||||
if (is_callable($query)) {
|
||||
$query($baseQuery);
|
||||
}
|
||||
|
||||
return $baseQuery->get()->mapWithKeys(function ($employee) {
|
||||
return [$employee->id => $employee->full_name];
|
||||
});
|
||||
}
|
||||
|
||||
public function terminate($id){
|
||||
$employee = $this->findById($id);
|
||||
$employee->update(["status" => "terminated"]);
|
||||
return $employee;
|
||||
}
|
||||
|
||||
}
|
Reference in New Issue
Block a user