Jelajahi Sumber

правки в модели (pk) + новые эндпоинты для получения/обновления квеста

ilg2005 4 bulan lalu
induk
melakukan
f6913f5f02
3 mengubah file dengan 77 tambahan dan 3 penghapusan
  1. 69 1
      app/Http/Controllers/CharController.php
  2. 3 1
      app/Models/Char/CharQuest.php
  3. 5 1
      routes/api.php

+ 69 - 1
app/Http/Controllers/CharController.php

@@ -8,6 +8,7 @@ use App\Models\Char\CharCashItem_OutputBox;
 use App\Models\Char\CharItem;
 use App\Models\Char\CharFellow;
 use App\Models\Char\CharQuest;
+use Illuminate\Support\Facades\Log;
 
 class CharController extends Controller
 {
@@ -312,6 +313,72 @@ class CharController extends Controller
         return $this->_updateAndRespond($item, $data, 'Item successfully updated.');
     }    
     
+    /**
+     * Получение квеста персонажа по char_id и RecId.
+     *
+     * @param string $char_id
+     * @param string $rec_id
+     * @return \Illuminate\Http\JsonResponse
+     */
+    public function GetCharQuest(string $char_id, string $rec_id)
+    {
+        // Проверяем существование персонажа
+        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();
+
+        if (!$quest) {
+            return response()->json([
+                'code' => -3,
+                'msg' => 'Quest not found for this character.',
+            ], 404);
+        }
+
+        return response()->json([
+            'code' => 0,
+            'msg' => 'Quest successfully received.',
+            'data' => $quest,
+        ], 200);
+    }
+
+    /**
+     * Обновление квеста персонажа по char_id и RecId.
+     *
+     * @param \Illuminate\Http\Request $request
+     * @param string $char_id
+     * @param string $rec_id
+     * @return \Illuminate\Http\JsonResponse
+     */
+    public function UpdateCharQuest(Request $request, string $char_id, string $rec_id)
+    {
+        // Проверяем существование персонажа
+        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();
+
+        if (!$quest) {
+            return response()->json([
+                'code' => -3,
+                'msg' => 'Quest not found for this character.',
+            ], 404);
+        }
+
+        // Защищаем поля Owner и RecId от изменения
+        $protected = ['Owner', 'RecId'];
+        $data = $request->except($protected);
+
+        return $this->_updateAndRespond($quest, $data, 'Quest successfully updated.');
+    }
 
     //------------------------------ ПРИВАТНЫЕ МЕТОДЫ -----------------------------------------
     /**
@@ -335,7 +402,7 @@ class CharController extends Controller
         'CharItem' => ['key' => 'Owner', 'model' => CharItem::class, 'pk' => 'CharItemID'],
         'CharFellow' => ['key' => 'Owner', 'model' => CharFellow::class, 'pk' => 'FellowID'],
         'CharCashItem_OutputBox' => ['key' => 'Owner', 'model' => CharCashItem_OutputBox::class, 'pk' => 'ItemDBIndex'],
-        'CharQuest' => ['key' => 'Owner', 'model' => CharQuest::class],
+        'CharQuest' => ['key' => 'Owner', 'model' => CharQuest::class, 'pk' => 'RecId'],
     ];
 
 
@@ -375,6 +442,7 @@ class CharController extends Controller
         try {
             $ok = $model->update($filteredData);
         } catch (\Throwable $e) {
+            Log::error('Database error while updating record:', ['error' => $e->getMessage()]);
             return response()->json([
                 'code' => -7,
                 'msg' => 'Database error while updating record.',

+ 3 - 1
app/Models/Char/CharQuest.php

@@ -10,7 +10,9 @@ use App\Models\Char\CharBase;
 class CharQuest extends Model
 {
     protected $connection = "Char";
-    protected $table = "Table_CharQuest";    
+    protected $table = "Table_CharQuest";
+    protected $primaryKey = 'RecId'; // Используем RecId как первичный ключ
+    public $incrementing = false; // RecId не автоинкрементный
     protected $dates = [];
     protected $fillable =
             [

+ 5 - 1
routes/api.php

@@ -37,7 +37,11 @@ Route::group(['prefix' => 'api'], function () {
 
     // Получение и обновление предмета по char_item_id в нужной таблице
     Route::get('GetCharItem/{table}/{char_item_id}', 'CharController@GetCharItem');
-    Route::post('UpdateCharItem/{table}/{char_item_id}', 'CharController@UpdateCharItem');    
+    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');    
     
     Route::group(['prefix' => 'funcs_adm'], function () {
         Route::get('UpdateCharItemAccount/{CharID?}', 'ADMIN\AdminController@UpdateCharItemAccount');