visuddhinanda 9 месяцев назад
Родитель
Сommit
c89bc64091

+ 48 - 0
api-v8/app/Http/Requests/StoreDiscussionRequest.php

@@ -0,0 +1,48 @@
+<?php
+
+namespace App\Http\Requests;
+
+use Illuminate\Foundation\Http\FormRequest;
+use App\Http\Api\AuthApi;
+use Illuminate\Support\Facades\Log;
+
+class StoreDiscussionRequest extends FormRequest
+{
+    private $user;
+    /**
+     * Determine if the user is authorized to make this request.
+     *
+     * @return bool
+     */
+    public function authorize()
+    {
+        $user = AuthApi::current($this);
+        if (!$user) {
+            Log::warning('discussion store auth failed', ['request' => $this]);
+            return false;
+        }
+        $this->user = $user;
+        return true;
+    }
+
+    /**
+     * Get the validation rules that apply to the request.
+     *
+     * @return array
+     */
+    public function rules(): array
+    {
+        return [
+            'res_id' => 'required|string',
+            'res_type' => 'required|string',
+        ];
+    }
+
+    public function messages(): array
+    {
+        return [
+            'res_id.required' => 'res_type是必填项',
+            'res_type.required' => 'type是必填项',
+        ];
+    }
+}

+ 33 - 0
api-v8/app/Services/DiscussionService.php

@@ -0,0 +1,33 @@
+<?php
+
+namespace App\Services;
+
+use App\Models\Discussion;
+use Illuminate\Support\Facades\Hash;
+use App\Http\Api\Mq;
+use App\Http\Resources\DiscussionResource;
+
+class DiscussionService
+{
+    public function create(array $data): Discussion
+    {
+        if (isset($data['parent'])) {
+            $parentInfo = Discussion::find($data['parent']);
+            if (!$parentInfo) {
+                throw new \Exception('没有找到parent', 500);
+            }
+            $data['res_id '] = $parentInfo->res_id;
+            $data['res_type'] = $parentInfo->res_type;
+        }
+        $discussion = Discussion::create($data);
+        //更新parent children_count
+        if (isset($data['parent'])) {
+            $parentInfo->increment('children_count', 1);
+            $parentInfo->save();
+        }
+        if (isset($data['notification']) && $data['notification'] == 'true') {
+            Mq::publish('discussion', new DiscussionResource($discussion));
+        }
+        return $discussion;
+    }
+}