visuddhinanda 2 лет назад
Родитель
Сommit
9068deb7d6

+ 127 - 0
app/Http/Controllers/NotificationController.php

@@ -0,0 +1,127 @@
+<?php
+
+namespace App\Http\Controllers;
+
+use Illuminate\Http\Request;
+use Illuminate\Support\Str;
+use App\Models\Notification;
+use App\Http\Api\AuthApi;
+use App\Http\Resources\NotificationResource;
+
+class NotificationController extends Controller
+{
+    /**
+     * Display a listing of the resource.
+     *
+     * @return \Illuminate\Http\Response
+     */
+    public function index(Request $request)
+    {
+        //
+        $user = AuthApi::current($request);
+        if(!$user){
+            Log::error('notification auth failed {request}',['request'=>$request]);
+            return $this->error(__('auth.failed'),401,401);
+        }
+        switch ($request->get('view')) {
+            case 'to':
+                $table = Notification::where('to',$user['user_uid']);
+                $unread = Notification::where('to',$user['user_uid'])
+                                    ->where('status','unread')->count();
+                break;
+        }
+
+        if($request->has('status')){
+            $table = $table->whereIn('status',explode(',',$request->get('status')) );
+        }
+        $count = $table->count();
+
+        $table = $table->orderBy($request->get('order','created_at'),$request->get('dir','desc'));
+
+        $table = $table->skip($request->get("offset",0))
+                    ->take($request->get('limit',10));
+
+        $result = $table->get();
+
+        return $this->ok(
+            [
+            "rows"=>NotificationResource::collection($result),
+            "count"=>$count,
+            'unread'=>$unread,
+            ]);
+    }
+
+    /**
+     * 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){
+            Log::error('notification auth failed {request}',['request'=>$request]);
+            return $this->error(__('auth.failed'),401,401);
+        }
+        $new = new Notification;
+        $new->id = Str::uuid();
+        $new->from = $user['user_uid'];
+        $new->to = $request->get('to');
+        $new->url = $request->get('url');
+        $new->content = $request->get('content');
+        $new->res_type = $request->get('res_type');
+        $new->res_id = $request->get('res_id');
+        $new->save();
+
+        return $this->ok(new NotificationResource($new));
+    }
+
+    /**
+     * Display the specified resource.
+     *
+     * @param  Notification $notification
+     * @return \Illuminate\Http\Response
+     */
+    public function show(Notification $notification)
+    {
+        //
+        return $this->ok(new NotificationResource($notification));
+    }
+
+    /**
+     * Update the specified resource in storage.
+     *
+     * @param  \Illuminate\Http\Request  $request
+     * @param  Notification $notification
+     * @return \Illuminate\Http\Response
+     */
+    public function update(Request $request, Notification $notification)
+    {
+        //
+        $notification->status = $request->get('status','read');
+        $notification->save();
+        $unread = Notification::where('to',$notification->to)
+                            ->where('status','unread')
+                            ->count();
+        return $this->ok(['unread'=>$unread]);
+    }
+
+    /**
+     * Remove the specified resource from storage.
+     *
+     * @param  Notification $notification
+     * @return \Illuminate\Http\Response
+     */
+    public function destroy(Notification $notification)
+    {
+        //
+        $notification->delete();
+        if($notification->trashed()){
+            return $this->ok('ok');
+        }else{
+            return $this->error('fail',500,500);
+        }
+    }
+}

+ 33 - 0
app/Http/Resources/NotificationResource.php

@@ -0,0 +1,33 @@
+<?php
+
+namespace App\Http\Resources;
+
+use Illuminate\Http\Resources\Json\JsonResource;
+use App\Http\Api\UserApi;
+
+class NotificationResource 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,
+            "from"=> UserApi::getByUuid($this->from),
+            "to"=> UserApi::getByUuid($this->to),
+            "url"=> $this->url,
+            "content"=> $this->content,
+            "content_type"=> $this->content_type,
+            "res_type"=> $this->res_type,
+            "res_id"=> $this->res_id,
+            "status"=> $this->status,
+            "created_at"=> $this->created_at,
+            "updated_at"=> $this->updated_at,
+        ];
+        return $data;
+    }
+}

+ 16 - 0
app/Models/Notification.php

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

+ 40 - 0
database/migrations/2023_12_17_033134_create_notifications_table.php

@@ -0,0 +1,40 @@
+<?php
+
+use Illuminate\Database\Migrations\Migration;
+use Illuminate\Database\Schema\Blueprint;
+use Illuminate\Support\Facades\Schema;
+
+class CreateNotificationsTable extends Migration
+{
+    /**
+     * Run the migrations.
+     *
+     * @return void
+     */
+    public function up()
+    {
+        Schema::create('notifications', function (Blueprint $table) {
+            $table->uuid('id')->primary();
+            $table->uuid('from')->index();
+            $table->uuid('to')->index();
+            $table->string('url',1024)->nullable();
+            $table->text('content')->nullable()->index();
+            $table->string('content_type',16)->index()->default('markdown');
+            $table->string('res_type',32)->index();
+            $table->uuid('res_id')->index();
+            $table->string('status',32)->index()->default('unread');
+            $table->softDeletes();
+            $table->timestamps();
+        });
+    }
+
+    /**
+     * Reverse the migrations.
+     *
+     * @return void
+     */
+    public function down()
+    {
+        Schema::dropIfExists('notifications');
+    }
+}

+ 35 - 0
database/migrations/2023_12_17_112858_add_uid_in_sent_prs.php

@@ -0,0 +1,35 @@
+<?php
+
+use Illuminate\Database\Migrations\Migration;
+use Illuminate\Database\Schema\Blueprint;
+use Illuminate\Support\Facades\Schema;
+
+class AddUidInSentPrs extends Migration
+{
+    /**
+     * Run the migrations.
+     *
+     * @return void
+     */
+    public function up()
+    {
+        Schema::table('sent_prs', function (Blueprint $table) {
+            //
+            $table->uuid('uid')->default(DB::raw('uuid_generate_v1mc()'));
+
+        });
+    }
+
+    /**
+     * Reverse the migrations.
+     *
+     * @return void
+     */
+    public function down()
+    {
+        Schema::table('sent_prs', function (Blueprint $table) {
+            //
+            $table->dropColumn('uid');
+        });
+    }
+}