visuddhinanda пре 3 година
родитељ
комит
fd3764b55a

+ 116 - 0
app/Http/Controllers/SentSimController.php

@@ -0,0 +1,116 @@
+<?php
+
+namespace App\Http\Controllers;
+
+use App\Models\SentSim;
+use App\Models\PaliSentence;
+use Illuminate\Http\Request;
+use App\Http\Resources\SentSimResource;
+
+class SentSimController extends Controller
+{
+    /**
+     * Display a listing of the resource.
+     *
+     * @return \Illuminate\Http\Response
+     */
+    public function index(Request $request)
+    {
+        //
+        switch ($request->get('view')) {
+            case 'sentence':
+                $sentId = PaliSentence::where('book',$request->get('book'))
+                                ->where('paragraph',$request->get('paragraph'))
+                                ->where('word_begin',$request->get('start'))
+                                ->where('word_end',$request->get('end'))
+                                ->value('id');
+                if(!$sentId){
+                    return $this->error("no sent");
+                }
+                $table = SentSim::where('sent1',$sentId)
+                                ->where('sim',">",0.7)
+                                ->orderBy('sim','desc');
+                break;
+        }
+        $count = $table->count();
+        if(!empty($request->get('limit'))){
+            $offset = 0;
+            if(!empty($request->get("offset"))){
+                $offset = $request->get("offset");
+            }
+            $table->skip($offset)->take($request->get('limit'));
+        }
+        $result = $table->get();
+        if($result){
+            return $this->ok(["rows"=>SentSimResource::collection($result),"count"=>$count]);
+        }else{
+            return $this->error("no data");
+        }
+    }
+
+    /**
+     * Show the form for creating a new resource.
+     *
+     * @return \Illuminate\Http\Response
+     */
+    public function create()
+    {
+        //
+    }
+
+    /**
+     * Store a newly created resource in storage.
+     *
+     * @param  \Illuminate\Http\Request  $request
+     * @return \Illuminate\Http\Response
+     */
+    public function store(Request $request)
+    {
+        //
+    }
+
+    /**
+     * Display the specified resource.
+     *
+     * @param  \App\Models\SentSim  $sentSim
+     * @return \Illuminate\Http\Response
+     */
+    public function show(SentSim $sentSim)
+    {
+        //
+    }
+
+    /**
+     * Show the form for editing the specified resource.
+     *
+     * @param  \App\Models\SentSim  $sentSim
+     * @return \Illuminate\Http\Response
+     */
+    public function edit(SentSim $sentSim)
+    {
+        //
+    }
+
+    /**
+     * Update the specified resource in storage.
+     *
+     * @param  \Illuminate\Http\Request  $request
+     * @param  \App\Models\SentSim  $sentSim
+     * @return \Illuminate\Http\Response
+     */
+    public function update(Request $request, SentSim $sentSim)
+    {
+        //
+    }
+
+    /**
+     * Remove the specified resource from storage.
+     *
+     * @param  \App\Models\SentSim  $sentSim
+     * @return \Illuminate\Http\Response
+     */
+    public function destroy(SentSim $sentSim)
+    {
+        //
+    }
+}

+ 52 - 0
app/Http/Resources/SentSimResource.php

@@ -0,0 +1,52 @@
+<?php
+
+namespace App\Http\Resources;
+
+use Illuminate\Http\Resources\Json\JsonResource;
+use App\Models\PaliSentence;
+use App\Http\Api\StudioApi;
+use App\Http\Api\UserApi;
+use App\Http\Api\ChannelApi;
+use App\Http\Controllers\CorpusController;
+
+class SentSimResource extends JsonResource
+{
+    /**
+     * Transform the resource into an array.
+     *
+     * @param  \Illuminate\Http\Request  $request
+     * @return array|\Illuminate\Contracts\Support\Arrayable|\JsonSerializable
+     */
+    public function toArray($request)
+    {
+        //获取实际句子信息
+        $sent = PaliSentence::find($this->sent2);
+        $channelId = ChannelApi::getSysChannel('_System_Pali_VRI_');
+        $sentOrg = [
+            "id"=>$sent->id,
+            "book"=> $sent->book,
+            "para"=> $sent->paragraph,
+            "wordStart"=> $sent->word_begin,
+            "wordEnd"=> $sent->word_end,
+            "editor"=> StudioApi::getById(config("app.admin.root_uuid")),
+            "channel"=> ChannelApi::getById($channelId),
+            "content"=>$sent->text,
+            "html"=> "<span>{$sent->html}</span>",
+            "role"=>"member",
+            "created_at"=> $sent->created_at,
+            "updated_at"=> $sent->updated_at,
+        ];
+        $resCount = CorpusController::sentResCount($sent->book,$sent->paragraph,$sent->word_begin,$sent->word_end);
+        return [
+            "id" => "{$sent->book}-{$sent->paragraph}-{$sent->word_begin}-{$sent->word_end}",
+            "origin" =>  [$sentOrg],
+            "translation" =>  [],
+            "layout" =>   "column",
+            "tranNum" =>  $resCount['tranNum'],
+            "nissayaNum" =>  $resCount['nissayaNum'],
+            "commNum" =>  $resCount['commNum'],
+            "originNum" =>  $resCount['originNum'],
+            "simNum" =>  $resCount['simNum'],
+        ];
+    }
+}

+ 22 - 0
tests/Feature/SentSimTest.php

@@ -0,0 +1,22 @@
+<?php
+
+namespace Tests\Feature;
+
+use Illuminate\Foundation\Testing\RefreshDatabase;
+use Illuminate\Foundation\Testing\WithFaker;
+use Tests\TestCase;
+
+class SentSimTest extends TestCase
+{
+    /**
+     * A basic feature test example.
+     *
+     * @return void
+     */
+    public function test_index()
+    {
+        $response = $this->get('/api/v2/sent-sim?view=sentence&book=1&paragraph=555&start=2&end=5');
+
+        $response->assertStatus(200);
+    }
+}