visuddhinanda 2 лет назад
Родитель
Сommit
866542c7d1
2 измененных файлов с 136 добавлено и 0 удалено
  1. 110 0
      app/Http/Controllers/NavCSParaController.php
  2. 26 0
      app/Http/Resources/NavCSParaResource.php

+ 110 - 0
app/Http/Controllers/NavCSParaController.php

@@ -0,0 +1,110 @@
+<?php
+
+namespace App\Http\Controllers;
+
+use App\Models\WbwTemplate;
+use App\Models\PaliText;
+use App\Models\RelatedParagraph;
+use App\Http\Resources\NavCSParaResource;
+use Illuminate\Http\Request;
+
+class NavCSParaController extends Controller
+{
+    /**
+     * Display a listing of the resource.
+     *
+     * @return \Illuminate\Http\Response
+     */
+    public function index()
+    {
+        //
+    }
+
+    /**
+     * 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\WbwTemplate  $wbwTemplate
+     * @return \Illuminate\Http\Response
+     */
+    public function show(string $paraNumber)
+    {
+        //99-5-37
+        $id = explode('-',$paraNumber);
+        if(count($id) !== 3){
+            return $this->error('参数错误。参数应为3 实际得到'.count($id),400,400);
+        }
+        //查询段落起始
+        $para = PaliText::where('book',$id[0])
+                        ->where('paragraph',$id[1])
+                        ->first();
+        if(!$para){
+            return $this->error('没有找到段落起始'.$id,404,404);
+        }
+        $begin = $id[1];
+        $end = (int)$id[1] + $para->chapter_len;
+        $curr = WbwTemplate::where('book',$id[0])
+                                ->where('style','paranum')
+                                ->where('word',$id[2])
+                                ->whereBetween('paragraph',[$begin,$end])
+                                ->select('book','paragraph')->first();
+        if(!$curr){
+            return $this->error('没有找到段落'.$id,404,404);
+        }
+        $data = [];
+        $data['curr'] = new NavCSParaResource($curr);
+        $next = WbwTemplate::where('book',$id[0])
+                ->where('style','paranum')
+                ->where('word',(int)$id[2]+1)
+                ->whereBetween('paragraph',[$begin,$end])
+                ->select('book','paragraph')->first();
+        if($next){
+            $data['next'] = new NavCSParaResource($next);
+            $data['end'] = $next->paragraph -1;
+        }else{
+            $data['end'] = $end;
+        }
+        $prev = WbwTemplate::where('book',$id[0])
+                            ->where('style','paranum')
+                            ->where('word',$id[2]-1)
+                            ->whereBetween('paragraph',[$begin,$end])
+                            ->select('book','paragraph')->first();
+        if($prev){
+            $data['prev'] = new NavCSParaResource($prev);
+        }
+        return $this->ok($data);
+    }
+
+    /**
+     * Update the specified resource in storage.
+     *
+     * @param  \Illuminate\Http\Request  $request
+     * @param  \App\Models\WbwTemplate  $wbwTemplate
+     * @return \Illuminate\Http\Response
+     */
+    public function update(Request $request, WbwTemplate $wbwTemplate)
+    {
+        //
+    }
+
+    /**
+     * Remove the specified resource from storage.
+     *
+     * @param  \App\Models\WbwTemplate  $wbwTemplate
+     * @return \Illuminate\Http\Response
+     */
+    public function destroy(WbwTemplate $wbwTemplate)
+    {
+        //
+    }
+}

+ 26 - 0
app/Http/Resources/NavCSParaResource.php

@@ -0,0 +1,26 @@
+<?php
+
+namespace App\Http\Resources;
+
+use Illuminate\Http\Resources\Json\JsonResource;
+use App\Models\PaliText;
+
+class NavCSParaResource extends JsonResource
+{
+    /**
+     * Transform the resource into an array.
+     *
+     * @param  \Illuminate\Http\Request  $request
+     * @return array|\Illuminate\Contracts\Support\Arrayable|\JsonSerializable
+     */
+    public function toArray($request)
+    {
+        $data = [];
+        $data['content'] = PaliText::where('book',$this->book)
+                                ->where('paragraph',$this->paragraph)
+                                ->value('text');
+        $data['book'] = $this->book;
+        $data['start'] = $this->paragraph;
+        return $data;
+    }
+}