visuddhinanda 2 سال پیش
والد
کامیت
f0d023e210

+ 104 - 0
app/Http/Controllers/RecentController.php

@@ -0,0 +1,104 @@
+<?php
+
+namespace App\Http\Controllers;
+
+use App\Models\Recent;
+use Illuminate\Http\Request;
+use App\Http\Resources\RecentResource;
+use App\Http\Api\AuthApi;
+use Illuminate\Support\Str;
+
+class RecentController extends Controller
+{
+    /**
+     * Display a listing of the resource.
+     *
+     * @return \Illuminate\Http\Response
+     */
+    public function index(Request $request)
+    {
+        //
+        switch ($request->view) {
+            case 'user':
+                $table = Recent::where('user_uid',$request->get('id'));
+                break;
+            default:
+                return $this->error('known view');
+                break;
+        }
+
+        $table->orderBy($request->get('order','created_at'),$request->get('dir','desc'));
+        $count = $table->count();
+        $table->skip($request->get("offset",0))
+              ->take($request->get('limit',1000));
+
+        $result = $table->get();
+		return $this->ok(["rows"=>RecentResource::collection($result),"count"=>$count]);
+    }
+
+    /**
+     * Store a newly created resource in storage.
+     *
+     * @param  \Illuminate\Http\Request  $request
+     * @return \Illuminate\Http\Response
+     */
+    public function store(Request $request)
+    {
+        $user = AuthApi::current($request);
+        if(!$user){
+            return $this->error(__('auth.failed'),[],401);
+        }
+
+        $validated = $request->validate([
+            'type' => 'required',
+            'article_id' => 'required',
+        ]);
+
+        $row = Recent::firstOrNew([
+            "type"=>$request->get("type"),
+            "article_id"=>$request->get("article_id"),
+            "user_uid"=>$user['user_uid'],
+        ],[
+            "id"=>Str::uuid(),
+        ]);
+        $row->param = $request->get("param",null);
+        $row->save();
+        return $this->ok(new RecentResource($row));
+    }
+
+    /**
+     * Display the specified resource.
+     *
+     * @param  \App\Models\Recent  $recent
+     * @return \Illuminate\Http\Response
+     */
+    public function show(Recent $recent)
+    {
+        //
+        return $this->ok(new RecentResource($recent));
+
+    }
+
+    /**
+     * Update the specified resource in storage.
+     *
+     * @param  \Illuminate\Http\Request  $request
+     * @param  \App\Models\Recent  $recent
+     * @return \Illuminate\Http\Response
+     */
+    public function update(Request $request, Recent $recent)
+    {
+        //
+    }
+
+    /**
+     * Remove the specified resource from storage.
+     *
+     * @param  \App\Models\Recent  $recent
+     * @return \Illuminate\Http\Response
+     */
+    public function destroy(Recent $recent)
+    {
+        //
+    }
+}

+ 62 - 0
app/Http/Resources/RecentResource.php

@@ -0,0 +1,62 @@
+<?php
+
+namespace App\Http\Resources;
+
+use Illuminate\Http\Resources\Json\JsonResource;
+use App\Http\Api\ChannelApi;
+use App\Models\Sentence;
+
+class RecentResource 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 = [
+            'id' => $this->id,
+            'type' => $this->type,
+            'article_id' => $this->article_id,
+            'param' => $this->param,
+            'updated_at' => $this->updated_at,
+        ];
+        switch ($this->type) {
+            case 'article':
+                $data['title'] = Article::where('uid',$this->article_id)->value('title');
+                break;
+            case 'chapter':
+                $para = explode('-',$this->article_id);
+                if(!empty($this->param)){
+                    $param = json_decode($this->param,true);
+                    if(isset($param['channel'])){
+                        $channelId = explode('_',$param['channel'])[0];
+                    }
+                }
+                $paliChannel = ChannelApi::getSysChannel('_System_Pali_VRI_');
+                if(!isset($channelId)){
+                    $channelId = $paliChannel;
+                }
+
+                $title = Sentence::where('book_id',$para[0])
+                                        ->where('paragraph',$para[1])
+                                        ->where('channel_uid',$channelId)
+                                        ->value('content');
+                if(empty($title)){
+                    $title = Sentence::where('book_id',$para[0])
+                                        ->where('paragraph',$para[1])
+                                        ->where('channel_uid',$paliChannel)
+                                        ->value('content');
+                }
+                $data['title'] = $title;
+                break;
+            default:
+                $data['title'] = $this->article_id;
+                break;
+        }
+
+        return $data;
+    }
+}

+ 16 - 0
app/Models/Recent.php

@@ -0,0 +1,16 @@
+<?php
+
+namespace App\Models;
+
+use Illuminate\Database\Eloquent\Factories\HasFactory;
+use Illuminate\Database\Eloquent\Model;
+
+class Recent extends Model
+{
+    use HasFactory;
+    protected $casts = [
+        'id' => 'string'
+    ];
+	protected $fillable = ['id','type','article_id','user_uid'];
+
+}

+ 37 - 0
database/migrations/2023_07_02_110904_create_recents_table.php

@@ -0,0 +1,37 @@
+<?php
+
+use Illuminate\Database\Migrations\Migration;
+use Illuminate\Database\Schema\Blueprint;
+use Illuminate\Support\Facades\Schema;
+
+class CreateRecentsTable extends Migration
+{
+    /**
+     * Run the migrations.
+     *
+     * @return void
+     */
+    public function up()
+    {
+        Schema::create('recents', function (Blueprint $table) {
+            $table->uuid("id")->primary();
+            $table->string('type',63)->index();
+            $table->string('article_id',255)->index();
+            $table->json('param')->nullable();
+            $table->uuid("user_uid")->index();
+            $table->timestamps();
+            $table->index('created_at');
+            $table->index('updated_at');
+        });
+    }
+
+    /**
+     * Reverse the migrations.
+     *
+     * @return void
+     */
+    public function down()
+    {
+        Schema::dropIfExists('recents');
+    }
+}

+ 39 - 0
tests/Feature/RecentTest.php

@@ -0,0 +1,39 @@
+<?php
+
+namespace Tests\Feature;
+
+use Illuminate\Foundation\Testing\RefreshDatabase;
+use Illuminate\Foundation\Testing\WithFaker;
+use Tests\TestCase;
+
+class RecentTest extends TestCase
+{
+    private $token = 'Bearer eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzUxMiJ9.eyJuYmYiOjE2NjgyMzE3MTksImV4cCI6MTY5OTc2NzcxOSwidWlkIjoiYmE1NDYzZjMtNzJkMS00NDEwLTg1OGUtZWFkZDEwODg0NzEzIiwiaWQiOiI0In0.LV4ItC5VCqXpbKIXT1zePcnfi-heCf3Df63w7qbXsT1i5KJtwJJC938CLgANjqwcQFa3lrR5TqvT1kkqD-Mmgg';
+
+    /**
+     * A basic feature test example.
+     *
+     * @return void
+     */
+    public function test_index()
+    {
+        $response = $this->get('/api/v2/recent?view=user&id=ba5463f3-72d1-4410-858e-eadd10884713');
+
+        $response->assertStatus(200);
+    }
+    public function test_store()
+    {
+        //testing store
+        $response = $this->withHeaders([
+            'Authorization' => $this->token,
+        ])->json('POST', '/api/v2/recent',
+                    [
+                        'type'=>'chapter',
+                        'article_id'=>'168-3',
+                        'user_uid'=>'ba5463f3-72d1-4410-858e-eadd10884713',
+                    ]);
+
+        $response->assertOk();
+
+    }
+}