68 lines
1.6 KiB
PHP
68 lines
1.6 KiB
PHP
<?php
|
|
|
|
namespace Modules\Employee\Services;
|
|
|
|
use Illuminate\Support\Facades\DB;
|
|
use Modules\Employee\Models\Department;
|
|
|
|
class DepartmentService
|
|
{
|
|
|
|
public function getAllDepartments(Request $request, callable $query = null, $paginate = false, $limit = 10)
|
|
{
|
|
$baseQuery = Department::query();
|
|
|
|
if (is_callable($query)) {
|
|
$query($baseQuery);
|
|
}
|
|
|
|
if ($paginate) {
|
|
return $baseQuery->paginate($limit);
|
|
}
|
|
|
|
return $baseQuery->get();
|
|
}
|
|
|
|
public function storeDepartment(array $departmentData): Department
|
|
{
|
|
return DB::transaction(function () use ($departmentData) {
|
|
$department = Department::create($departmentData);
|
|
|
|
return $department;
|
|
});
|
|
}
|
|
|
|
public function getDepartmentById(int $id)
|
|
{
|
|
return Department::findOrFail($id);
|
|
}
|
|
|
|
public function updateDepartment(int $id, array $departmentData)
|
|
{
|
|
$department = $this->getDepartmentById($id);
|
|
|
|
return DB::transaction(function () use ($department, $departmentData) {
|
|
$department->update($departmentData);
|
|
return $department;
|
|
});
|
|
}
|
|
|
|
public function deleteDepartment(int $id)
|
|
{
|
|
return DB::transaction(function () use ($id) {
|
|
$department = $this->getDepartmentById($id);
|
|
$department->delete();
|
|
return $department;
|
|
});
|
|
}
|
|
|
|
public function pluck(callable $query = null)
|
|
{
|
|
$baseQuery = Department::query();
|
|
if (is_callable($query)) {
|
|
$query($baseQuery);
|
|
}
|
|
return $baseQuery->pluck('title', 'id');
|
|
}
|
|
}
|