|
@@ -0,0 +1,68 @@
|
|
|
+<?php
|
|
|
+
|
|
|
+namespace App\Http\Controllers\API;
|
|
|
+
|
|
|
+use App\Http\Controllers\Controller;
|
|
|
+use App\Http\Requests\API\Department\CreateOrUpdateRequest;
|
|
|
+use App\Http\Resources\API\DepartmentResource;
|
|
|
+use App\Models\Department;
|
|
|
+use Illuminate\Http\Request;
|
|
|
+use Illuminate\Support\Facades\Auth;
|
|
|
+
|
|
|
+class DepartmentController extends Controller
|
|
|
+{
|
|
|
+
|
|
|
+ public function index(Request $request)
|
|
|
+ {
|
|
|
+ $department=Department::filter($request->all())->where("parent_id",0)->with(['children'])->simplePaginate();
|
|
|
+
|
|
|
+ return DepartmentResource::collection($department);
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ public function store(CreateOrUpdateRequest $request)
|
|
|
+ {
|
|
|
+ $department=new Department();
|
|
|
+
|
|
|
+ $department->mergeFillable([
|
|
|
+ 'company_id'
|
|
|
+ ]);
|
|
|
+
|
|
|
+ $department->fill([
|
|
|
+ ...$request->all(),
|
|
|
+ 'company_id' => Auth::user()->company_id,
|
|
|
+ ]);
|
|
|
+
|
|
|
+ $department->save();
|
|
|
+
|
|
|
+ return $this->created();
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ public function show(string $id)
|
|
|
+ {
|
|
|
+ $department=Department::query()->findOrFail($id);
|
|
|
+
|
|
|
+ return new DepartmentResource($department);
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+ public function update(CreateOrUpdateRequest $request,string $id)
|
|
|
+ {
|
|
|
+ $department=Department::findOrFail($id);
|
|
|
+ $department->fill($request->all());
|
|
|
+ $department->save();
|
|
|
+ return $this->noContent();
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ public function destroy(string $id)
|
|
|
+ {
|
|
|
+ $department = Department::findOrFail($id);
|
|
|
+
|
|
|
+ $department->delete();
|
|
|
+
|
|
|
+ return $this->noContent();
|
|
|
+ }
|
|
|
+}
|