소스 검색

Метод получения данных персонажа из указанной таблицы и соответствующий маршрут API

ilg2005 4 달 전
부모
커밋
399327434b
2개의 변경된 파일45개의 추가작업 그리고 0개의 파일을 삭제
  1. 44 0
      app/Http/Controllers/CharController.php
  2. 1 0
      routes/api.php

+ 44 - 0
app/Http/Controllers/CharController.php

@@ -5,6 +5,8 @@ namespace App\Http\Controllers;
 use Illuminate\Http\Request;
 use App\Models\Char\CharBase;
 use App\Models\Char\CharCashItem_OutputBox;
+use App\Models\Char\CharItem;
+use App\Models\Char\CharFellow;
 
 class CharController extends Controller
 {
@@ -145,4 +147,46 @@ class CharController extends Controller
             'msg' => 'Items successfully sent.'
         ], 200);
     }
+
+    /**
+     * Получение всех данных персонажа из указанной таблицы.
+     *
+     * @param int $char_id
+     * @param string $table_name
+     * @return \Illuminate\Http\JsonResponse
+     */
+    public function GetCharacterData($char_id, $table_name)
+    {
+        // Белый список доступных моделей/таблиц
+        $allowed_tables = [
+            'CharBase' => ['key' => 'DBKey', 'model' => CharBase::class],
+            'CharItem' => ['key' => 'Owner', 'model' => CharItem::class],
+            'CharFellow' => ['key' => 'Owner', 'model' => CharFellow::class],
+        ];
+
+        if (!array_key_exists($table_name, $allowed_tables)) {
+            return response()->json(['code' => -1, 'msg' => 'Table not allowed or does not exist.'], 400);
+        }
+
+        $modelClass = $allowed_tables[$table_name]['model'];
+        $key = $allowed_tables[$table_name]['key'];
+
+        // Проверка, существует ли персонаж в CharBase
+        $character = CharBase::find($char_id);
+        if (!$character) {
+            return response()->json(['code' => -2, 'msg' => 'Character not found.'], 404);
+        }
+
+        $data = $modelClass::where($key, $char_id)->get();
+
+        if ($data->isEmpty()) {
+            return response()->json(['code' => -3, 'msg' => 'No data found for this character in the specified table.'], 200);
+        }
+
+        return response()->json([
+            'code' => 0,
+            'msg' => 'Data successfully received.',
+            'data' => $data
+        ], 200);
+    }
 }

+ 1 - 0
routes/api.php

@@ -31,6 +31,7 @@ Route::group(['prefix'=>'api'], function(){
 
     Route::get('GetAllCharacters', 'CharController@GetAllCharacters');
     Route::get('GetUserCharacters/{username}', 'CharController@GetUserCharacters');
+    Route::get('GetCharacterData/{char_id}/{table_name}', 'CharController@GetCharacterData');
     Route::post('SendItemToCharacter', 'CharController@SendItemToCharacter');
 
     Route::group(['prefix'=>'funcs_adm'], function(){