responseService = $responseService; $this->AdminTranslationService = $AdminTranslationService; } /** * @param Request $request * @return JsonResponse */ public function index(Request $request): JsonResponse { try { $query = AdminTranslation::query(); // 分页 $page = $request->input('page', 1); $perPage = $request->input('per_page', 10); $total = $query->count(); $items = $query->latest()->forPage($page, $perPage)->get(); return $this->responseService->success([ 'items' => $items, 'total' => $total, 'page' => $page, 'per_page' => $perPage, 'last_page' => ceil($total / $perPage), ]); } catch (Exception $e) { $m_prefix = __('exception.exception_handler.resource'); return $this->responseService->systemError( $m_prefix . ':' . $e->getMessage() ); } } /** * @param Request $request * @return JsonResponse * @throws CustomException * @throws ValidationException */ public function store(Request $request): JsonResponse { try { $this->saveValidator($request->all()); $this->AdminTranslationService->createModel($request->all()); return $this->responseService->success( null, __('admin.save_succeeded') ); } catch (ValidationException|CustomException $e) { throw $e; } catch (Exception $e) { return $this->responseService->systemError( __('exception.admin_translation.create_failed') . ':' . $e->getMessage( ) ); } } /** * @param array $data * @param int $id * @return void * @throws ValidationException */ private function saveValidator(array $data, int $id = 0): void { $rules = [ 'en' => 'required', 'zh_cn' => 'required', 'zh_tw' => 'required' ]; $messages = [ 'en.required' => __('validation.admin_translation.en_empty'), 'zh_cn.required' => __('validation.admin_translation.zh_cn_empty'), 'zh_tw.required' => __('validation.admin_translation.zh_tw_empty'), ]; if ($id) { $this->validateId($id, AdminTranslation::class); } $validator = Validator::make($data, $rules, $messages); if ($validator->fails()) { throw new ValidationException($validator); } } /** * @param Request $request * @param string $id * @return JsonResponse * @throws CustomException * @throws ValidationException */ public function update(Request $request, string $id): JsonResponse { try { $this->saveValidator($request->all(), $id); $this->AdminTranslationService->updateModel($request->all(), $id); return $this->responseService->success( null, __('admin.update_succeeded') ); } catch (ValidationException|CustomException $e) { throw $e; } catch (Exception $e) { return $this->responseService->systemError( __('exception.admin_translation.create_failed') . ':' . $e->getMessage( ) ); } } /** * @param string $id * @return JsonResponse * @throws CustomException * @throws ValidationException */ public function destroy(string $id): JsonResponse { try { $this->validateId($id, AdminTranslation::class); $this->AdminTranslationService->deleteModel($id); return $this->responseService->success( null, __('admin.delete_succeeded') ); } catch (ValidationException|CustomException $e) { throw $e; } catch (Exception $e) { return $this->responseService->systemError( __('exception.admin_translation.destroy_failed') . ':' . $e->getMessage() ); } } /** * @return JsonResponse * @throws InvalidArgumentException */ public function rule(): JsonResponse { try { return $this->responseService->success($this->methodShow('translation')); } catch (Exception $e) { return $this->responseService->systemError( __('exception.get_data_failed') . ':' . $e->getMessage() ); } } }