guard()->user(); $list = OfficalBusiness::with(['workflow']) ->where('employee_id', $user->id) ->filter($request->all()) ->orderByDesc(WorkflowCheck::checkStatusSortBuilder(new OfficalBusiness())) ->orderBy('id', 'desc') ->paginate($request->input('per_page')); return OfficalBusinessResource::collection($list); } public function show($id) { $info = OfficalBusiness::with(['employee', 'store', 'workflow'])->findOrFail($id); return OfficalBusinessResource::make($info); } public function store(Request $request, OfficalBusinessService $service) { $user = $this->guard()->user(); $data = $request->all(); $data['employee_id'] = $user->id; try { DB::beginTransaction(); if (! $service->store($data)) { throw new RuntimeException($service->getError()); } $model = $service->getCurrentModel(); $workflow = WorkFlowService::make(); if (! $workflow->apply($model->workflow, $user)) { throw new RuntimeException($workflow->getError()); } DB::commit(); return response()->noContent(); } catch (\Exception $e) { DB::rollBack(); throw new RuntimeException($e->getMessage()); } } public function update($id, Request $request, OfficalBusinessService $service) { $user = $this->guard()->user(); $model = OfficalBusiness::where('employee_id', $user->id)->findOrFail($id); try { DB::beginTransaction(); if (! $service->update($id, $request->all())) { throw new RuntimeException($service->getError()); } $workflow = WorkFlowService::make(); if (! $workflow->apply($model->workflow, $user)) { throw new RuntimeException($workflow->getError()); } DB::commit(); return response()->noContent(); } catch (\Exception $e) { DB::rollBack(); throw new RuntimeException($e->getMessage()); } } public function destroy($id, OfficalBusinessService $service) { $user = $this->guard()->user(); $model = OfficalBusiness::where('employee_id', $user->id)->findOrFail($id); try { DB::beginTransaction(); if (! $service->delete($id)) { throw new RuntimeException($service->getError()); } DB::commit(); return response()->noContent(); } catch (\Exception $e) { DB::rollBack(); throw new RuntimeException($e->getMessage()); } } }