visuddhinanda 1 yıl önce
ebeveyn
işleme
c750395af7

+ 85 - 0
api-v8/app/Http/Controllers/SentenceAttachmentController.php

@@ -0,0 +1,85 @@
+<?php
+
+namespace App\Http\Controllers;
+
+use App\Http\Requests\StoreSentenceAttachmentRequest;
+use App\Http\Requests\UpdateSentenceAttachmentRequest;
+use Illuminate\Http\Request;
+use App\Models\SentenceAttachment;
+use App\Http\Resources\SentenceAttachmentResource;
+
+class SentenceAttachmentController extends Controller
+{
+    /**
+     * Display a listing of the resource.
+     *
+     * @return \Illuminate\Http\Response
+     */
+    public function index(Request $request)
+    {
+        switch ($request->view) {
+            case 'sentence':
+                $table = SentenceAttachment::where('sentence_id', $request->get('id'));
+                break;
+            default:
+                return $this->error('known view');
+                break;
+        }
+
+        $table->orderBy($request->get('order', 'updated_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" => SentenceAttachmentResource::collection($result),
+            "count" => $count
+        ]);
+    }
+
+    /**
+     * Store a newly created resource in storage.
+     *
+     * @param  \App\Http\Requests\StoreSentenceAttachmentRequest  $request
+     * @return \Illuminate\Http\Response
+     */
+    public function store(Request $request)
+    {
+        //
+    }
+
+    /**
+     * Display the specified resource.
+     *
+     * @param  \App\Models\SentenceAttachment  $sentenceAttachment
+     * @return \Illuminate\Http\Response
+     */
+    public function show(Request $sentenceAttachment)
+    {
+        //
+    }
+
+    /**
+     * Update the specified resource in storage.
+     *
+     * @param  \App\Http\Requests\UpdateSentenceAttachmentRequest  $request
+     * @param  \App\Models\SentenceAttachment  $sentenceAttachment
+     * @return \Illuminate\Http\Response
+     */
+    public function update(Request $request, SentenceAttachment $sentenceAttachment)
+    {
+        //
+    }
+
+    /**
+     * Remove the specified resource from storage.
+     *
+     * @param  \App\Models\SentenceAttachment  $sentenceAttachment
+     * @return \Illuminate\Http\Response
+     */
+    public function destroy(SentenceAttachment $sentenceAttachment)
+    {
+        //
+    }
+}

+ 30 - 0
api-v8/app/Http/Requests/StoreSentenceAttachmentRequest.php

@@ -0,0 +1,30 @@
+<?php
+
+namespace App\Http\Requests;
+
+use Illuminate\Foundation\Http\FormRequest;
+
+class StoreSentenceAttachmentRequest extends FormRequest
+{
+    /**
+     * Determine if the user is authorized to make this request.
+     *
+     * @return bool
+     */
+    public function authorize()
+    {
+        return false;
+    }
+
+    /**
+     * Get the validation rules that apply to the request.
+     *
+     * @return array
+     */
+    public function rules()
+    {
+        return [
+            //
+        ];
+    }
+}

+ 30 - 0
api-v8/app/Http/Requests/UpdateSentenceAttachmentRequest.php

@@ -0,0 +1,30 @@
+<?php
+
+namespace App\Http\Requests;
+
+use Illuminate\Foundation\Http\FormRequest;
+
+class UpdateSentenceAttachmentRequest extends FormRequest
+{
+    /**
+     * Determine if the user is authorized to make this request.
+     *
+     * @return bool
+     */
+    public function authorize()
+    {
+        return false;
+    }
+
+    /**
+     * Get the validation rules that apply to the request.
+     *
+     * @return array
+     */
+    public function rules()
+    {
+        return [
+            //
+        ];
+    }
+}

+ 43 - 0
api-v8/app/Http/Resources/SentenceAttachmentResource.php

@@ -0,0 +1,43 @@
+<?php
+
+namespace App\Http\Resources;
+
+use Illuminate\Http\Resources\Json\JsonResource;
+use Illuminate\Support\Facades\Http;
+use Illuminate\Support\Facades\Log;
+use App\Models\Attachment;
+use Illuminate\Support\Facades\Storage;
+use Illuminate\Support\Facades\App;
+
+class SentenceAttachmentResource extends JsonResource
+{
+    /**
+     * Transform the resource into an array.
+     *
+     * @param  \Illuminate\Http\Request  $request
+     * @return array|\Illuminate\Contracts\Support\Arrayable|\JsonSerializable
+     */
+    public function toArray($request)
+    {
+        $url = config('app.url') . '/api/v2/attachment/' . $this->attachment_id;
+        Log::info($url);
+        //$response = Http::get($url);
+
+
+        $data = [
+            'uid' => $this->uid,
+            'sentence_id' => $this->sentence_id,
+            'attachment_id' => $this->attachment_id,
+            'attachment' => [],
+            'editor_id' => $this->editor_id,
+        ];
+        $res = Attachment::find($this->attachment_id);
+        $filename = $res->bucket . '/' . $res->name;
+        if (App::environment('local')) {
+            $data['attachment']['url'] = Storage::url($filename);
+        } else {
+            $data['attachment']['url'] = Storage::temporaryUrl($filename, now()->addDays(2));
+        }
+        return $data;
+    }
+}

+ 11 - 0
api-v8/app/Models/SentenceAttachment.php

@@ -0,0 +1,11 @@
+<?php
+
+namespace App\Models;
+
+use Illuminate\Database\Eloquent\Factories\HasFactory;
+use Illuminate\Database\Eloquent\Model;
+
+class SentenceAttachment extends Model
+{
+    use HasFactory;
+}

+ 35 - 0
api-v8/database/migrations/2025_02_16_075250_create_sentence_attachments_table.php

@@ -0,0 +1,35 @@
+<?php
+
+use Illuminate\Database\Migrations\Migration;
+use Illuminate\Database\Schema\Blueprint;
+use Illuminate\Support\Facades\Schema;
+
+class CreateSentenceAttachmentsTable extends Migration
+{
+    /**
+     * Run the migrations.
+     *
+     * @return void
+     */
+    public function up()
+    {
+        Schema::create('sentence_attachments', function (Blueprint $table) {
+            $table->id();
+            $table->uuid('uid')->unique();
+            $table->uuid('sentence_id')->index();
+            $table->uuid('attachment_id')->index();
+            $table->uuid('editor_id')->index();
+            $table->timestamps();
+        });
+    }
+
+    /**
+     * Reverse the migrations.
+     *
+     * @return void
+     */
+    public function down()
+    {
+        Schema::dropIfExists('sentence_attachments');
+    }
+}

+ 35 - 0
dashboard-v4/dashboard/src/components/template/SentEdit/SentAttachment.tsx

@@ -0,0 +1,35 @@
+import { useEffect, useState } from "react";
+import {
+  IResAttachmentData,
+  IResAttachmentListResponse,
+} from "../../api/Attachments";
+import { get } from "../../../request";
+
+interface IWidget {
+  sentenceId?: string;
+}
+const SentAttachment = ({ sentenceId }: IWidget) => {
+  const [Attachments, setAttachments] = useState<IResAttachmentData[]>();
+  useEffect(() => {
+    if (!sentenceId) {
+      return;
+    }
+    const url = `/v2/sentence-attachment?view=sentence&id=${sentenceId}`;
+    console.debug("api request", url);
+    get<IResAttachmentListResponse>(url).then((json) => {
+      console.debug("api response", json);
+      if (json.ok) {
+        setAttachments(json.data.rows);
+      }
+    });
+  }, [sentenceId]);
+  return (
+    <>
+      {Attachments?.map((item, id) => {
+        return <img key={id} src={item.attachment.url} alt="img" />;
+      })}
+    </>
+  );
+};
+
+export default SentAttachment;