浏览代码

поддержка CharQuest_History в роутах, опциональный параметр table

ilg2005 4 月之前
父节点
当前提交
a058da61cd
共有 3 个文件被更改,包括 49 次插入20 次删除
  1. 44 12
      app/Http/Controllers/CharController.php
  2. 2 5
      app/Models/Char/CharQuest_History.php
  3. 3 3
      routes/api.php

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

@@ -9,6 +9,7 @@ use App\Models\Char\CharItem;
 use App\Models\Char\CharFellow;
 use App\Models\Char\CharQuest;
 use Illuminate\Support\Facades\Log;
+use App\Models\Char\CharQuest_History;
 
 class CharController extends Controller
 {
@@ -311,26 +312,41 @@ class CharController extends Controller
         $data = $request->except($protected);
 
         return $this->_updateAndRespond($item, $data, 'Item successfully updated.');
-    }    
-    
+    }
+
     /**
      * Получение квеста персонажа по char_id и RecId.
+     * Поддерживается выбор таблицы (CharQuest или CharQuest_History) через опциональный параметр $table.
      *
      * @param string $char_id
      * @param string $rec_id
+     * @param string|null $table
      * @return \Illuminate\Http\JsonResponse
      */
-    public function GetCharQuest(string $char_id, string $rec_id)
+
+    public function GetCharQuest(string $char_id, string $rec_id, ?string $table = null)
     {
+        // По умолчанию используем CharQuest
+        $table = $table ?? 'CharQuest';
+
+        // Проверяем допустимость таблицы для данной операции
+        $allowedQuestTables = ['CharQuest', 'CharQuest_History'];
+        if (!in_array($table, $allowedQuestTables, true)) {
+            return response()->json(['code' => -1, 'msg' => 'Table not allowed for this operation.'], 400);
+        }
+
         // Проверяем существование персонажа
         if (!CharBase::find($char_id)) {
             return response()->json(['code' => -2, 'msg' => 'Character not found.'], 404);
         }
 
-        // Ищем квест по Owner и RecId
-        $quest = CharQuest::where('Owner', $char_id)
-                          ->where('RecId', $rec_id)
-                          ->first();
+        // Получаем конфиг модели по таблице и делаем выборку по Owner и RecId
+        $config = $this->resolveTable($table);
+        $modelClass = $config['model'];
+
+        $quest = $modelClass::where('Owner', $char_id)
+            ->where('RecId', $rec_id)
+            ->first();
 
         if (!$quest) {
             return response()->json([
@@ -348,23 +364,38 @@ class CharController extends Controller
 
     /**
      * Обновление квеста персонажа по char_id и RecId.
+     * Поддерживается выбор таблицы (CharQuest или CharQuest_History) через опциональный параметр $table.
      *
      * @param \Illuminate\Http\Request $request
      * @param string $char_id
      * @param string $rec_id
+     * @param string|null $table
      * @return \Illuminate\Http\JsonResponse
      */
-    public function UpdateCharQuest(Request $request, string $char_id, string $rec_id)
+
+    public function UpdateCharQuest(Request $request, string $char_id, string $rec_id, ?string $table = null)
     {
+        // По умолчанию используем CharQuest
+        $table = $table ?? 'CharQuest';
+
+        // Проверяем допустимость таблицы для данной операции
+        $allowedQuestTables = ['CharQuest', 'CharQuest_History'];
+        if (!in_array($table, $allowedQuestTables, true)) {
+            return response()->json(['code' => -1, 'msg' => 'Table not allowed for this operation.'], 400);
+        }
+
         // Проверяем существование персонажа
         if (!CharBase::find($char_id)) {
             return response()->json(['code' => -2, 'msg' => 'Character not found.'], 404);
         }
 
-        // Ищем квест по Owner и RecId
-        $quest = CharQuest::where('Owner', $char_id)
-                          ->where('RecId', $rec_id)
-                          ->first();
+        // Получаем конфиг модели по таблице и делаем выборку по Owner и RecId
+        $config = $this->resolveTable($table);
+        $modelClass = $config['model'];
+
+        $quest = $modelClass::where('Owner', $char_id)
+            ->where('RecId', $rec_id)
+            ->first();
 
         if (!$quest) {
             return response()->json([
@@ -403,6 +434,7 @@ class CharController extends Controller
         'CharFellow' => ['key' => 'Owner', 'model' => CharFellow::class, 'pk' => 'FellowID'],
         'CharCashItem_OutputBox' => ['key' => 'Owner', 'model' => CharCashItem_OutputBox::class, 'pk' => 'ItemDBIndex'],
         'CharQuest' => ['key' => 'Owner', 'model' => CharQuest::class, 'pk' => 'RecId'],
+        'CharQuest_History' => ['key' => 'Owner', 'model' => CharQuest_History::class, 'pk' => 'RecId'],
     ];
 
 

+ 2 - 5
app/Models/Char/CharQuest_History.php

@@ -1,5 +1,4 @@
 <?php
-
 namespace App\Models\Char;
 
 use Illuminate\Database\Eloquent\Model;
@@ -11,16 +10,14 @@ class CharQuest_History extends Model
   protected $primaryKey = 'RecId'; // Используем RecId как первичный ключ
   public $incrementing = false; // RecId не автоинкрементный
   protected $dates = [];
-  protected $fillable =
-    [
+  protected $fillable = [
       "Owner",
       "Slot",
       "Deleted",
       "RecId",
       "AccomplishDate",
       "Count"
-    ];
+  ];
 
   public $timestamps = false;
-
 }

+ 3 - 3
routes/api.php

@@ -39,9 +39,9 @@ Route::group(['prefix' => 'api'], function () {
     Route::get('GetCharItem/{table}/{char_item_id}', 'CharController@GetCharItem');
     Route::post('UpdateCharItem/{table}/{char_item_id}', 'CharController@UpdateCharItem');
     
-    // Получение и обновление квеста по char_id и RecId
-    Route::get('GetCharQuest/{char_id}/{rec_id}', 'CharController@GetCharQuest');
-    Route::post('UpdateCharQuest/{char_id}/{rec_id}', 'CharController@UpdateCharQuest');    
+    // Получение и обновление квеста по char_id и RecId, {table?} — опционально, по умолчанию CharQuest    
+    Route::get('GetCharQuest/{char_id}/{rec_id}/{table?}', 'CharController@GetCharQuest');
+    Route::post('UpdateCharQuest/{char_id}/{rec_id}/{table?}', 'CharController@UpdateCharQuest');    
     
     Route::group(['prefix' => 'funcs_adm'], function () {
         Route::get('UpdateCharItemAccount/{CharID?}', 'ADMIN\AdminController@UpdateCharItemAccount');