소스 검색

:construction: create

visuddhinanda 3 년 전
부모
커밋
ff9b4e7a76

+ 188 - 0
app/Http/Controllers/NissayaEndingController.php

@@ -0,0 +1,188 @@
+<?php
+
+namespace App\Http\Controllers;
+
+use App\Models\NissayaEnding;
+use App\Models\Relation;
+use Illuminate\Http\Request;
+use App\Http\Resources\NissayaEndingResource;
+use App\Http\Api\AuthApi;
+use Illuminate\Support\Facades\App;
+
+class NissayaEndingController extends Controller
+{
+    /**
+     * Display a listing of the resource.
+     *
+     * @return \Illuminate\Http\Response
+     */
+    public function index(Request $request)
+    {
+        //
+        $table = NissayaEnding::select(['id','ending','lang','relation','editor_id','updated_at']);
+        if(($request->has('search'))){
+            $table->where('ending', 'like', $request->get('search')."%");
+        }
+        if(!empty($request->get('order')) && !empty($request->get('dir'))){
+            $table->orderBy($request->get('order'),$request->get('dir'));
+        }else{
+            $table->orderBy('updated_at','desc');
+        }
+        $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"=>NissayaEndingResource::collection($result),"count"=>$count]);
+		}else{
+			return $this->error("没有查询到数据");
+		}
+    }
+
+
+    /**
+     * 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'));
+        }
+        //TODO 判断权限
+        $validated = $request->validate([
+            'ending' => 'required',
+            'lang' => 'required',
+            'relation' => 'required'
+        ]);
+        $new = new NissayaEnding;
+        $new->ending = $validated['ending'];
+        $new->lang = $validated['lang'];
+        $new->relation = $validated['relation'];
+        $new->editor_id = $user['user_uid'];
+        $new->save();
+        return $this->ok(new NissayaEndingResource($new));
+    }
+
+    /**
+     * Display the specified resource.
+     *
+     * @param  \App\Models\NissayaEnding  $nissayaEnding
+     * @return \Illuminate\Http\Response
+     */
+    public function show(NissayaEnding $nissayaEnding)
+    {
+        //
+        return $this->ok(new NissayaEndingResource($nissayaEnding));
+
+    }
+
+    public function nissaya_card(Request $request)
+    {
+        //
+        App::setLocale($request->get('lang'));
+
+        $myEnding = NissayaEnding::where('ending',$request->get('ending'))
+                                ->groupBy('relation')
+                                ->select('relation')->get();
+        if(count($myEnding) === 0){
+            return $this->ok("# no record\n".$request->get('ending'));
+        }
+
+        $relations = Relation::whereIn('name',$myEnding)->get();
+
+        if(count($relations) === 0){
+            return $this->ok("# no relation\n".$request->get('ending'));
+        }
+        $output = "# 缅文语尾\n\n";
+        $output .= "|格位|含义|翻译建议|关系|关系|\n";
+        $output .= "|-|-|-|-|-|\n";
+        foreach ($relations as $key => $relation) {
+            if(empty($relation->case)){
+                $output .= "|-|-|-|-|{$relation->name}|\n";
+                continue;
+            }
+            $cases = json_decode($relation->case);
+            foreach ($cases as $key => $case) {
+                # code...
+                $output .= "|".__("grammar.".$case);
+                $output .= "|";
+                //本地语言用法
+                $output .= "|";
+                $localLangs = NissayaEnding::where('relation',$relation['name'])
+                                    ->where('lang',$request->get('lang'))->get();
+                foreach ($localLangs as $localLang) {
+                    # code...
+                    $output .= $localLang->ending.",";
+                }
+                $output .= "|".__("grammar.relations.{$relation['name']}.label");
+                $output .= "|".strtoupper($relation['name']);
+                $output .= "|\n";
+            }
+        }
+
+        return $this->ok($output);
+
+    }
+
+    /**
+     * Update the specified resource in storage.
+     *
+     * @param  \Illuminate\Http\Request  $request
+     * @param  \App\Models\NissayaEnding  $nissayaEnding
+     * @return \Illuminate\Http\Response
+     */
+    public function update(Request $request, NissayaEnding $nissayaEnding)
+    {
+        //
+        $user = AuthApi::current($request);
+        if(!$user){
+            return $this->error(__('auth.failed'));
+        }
+        //查询是否重复
+        if(NissayaEnding::where('ending',$request->get('ending'))
+                 ->where('lang',$request->get('lang'))
+                 ->where('relation',$request->get('relation'))
+                 ->exists()){
+            return $this->error(__('validation.exists',['name']));
+        }
+        $nissayaEnding->ending = $request->get('ending');
+        $nissayaEnding->lang = $request->get('lang');
+        $nissayaEnding->relation = $request->get('relation');
+        $nissayaEnding->editor_id = $user['user_uid'];
+        $nissayaEnding->save();
+        return $this->ok(new NissayaEndingResource($nissayaEnding));
+
+    }
+
+    /**
+     * Remove the specified resource from storage.
+     *
+     * @param  \Illuminate\Http\Request  $request
+     * @param  \App\Models\NissayaEnding  $nissayaEnding
+     * @return \Illuminate\Http\Response
+     */
+    public function destroy(Request $request,NissayaEnding $nissayaEnding)
+    {
+        //
+        $user = AuthApi::current($request);
+        if(!$user){
+            return $this->error(__('auth.failed'));
+        }
+        //TODO 判断当前用户是否有权限
+        $delete = 0;
+        $delete = $nissayaEnding->delete();
+
+        return $this->ok($delete);
+    }
+}

+ 148 - 0
app/Http/Controllers/RelationController.php

@@ -0,0 +1,148 @@
+<?php
+
+namespace App\Http\Controllers;
+
+use App\Models\Relation;
+use Illuminate\Http\Request;
+use App\Http\Resources\RelationResource;
+use App\Http\Api\AuthApi;
+
+class RelationController extends Controller
+{
+    /**
+     * Display a listing of the resource.
+     *
+     * @return \Illuminate\Http\Response
+     */
+    public function index(Request $request)
+    {
+        //
+        $table = Relation::select(['id','name','case','to','editor_id','updated_at','created_at']);
+        if(($request->has('search'))){
+            $table->where('name', 'like', $request->get('search')."%");
+        }
+        if(!empty($request->get('order')) && !empty($request->get('dir'))){
+            $table->orderBy($request->get('order'),$request->get('dir'));
+        }else{
+            $table->orderBy('updated_at','desc');
+        }
+        $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"=>RelationResource::collection($result),"count"=>$count]);
+		}else{
+			return $this->error("没有查询到数据");
+		}
+    }
+
+
+    /**
+     * 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'));
+        }
+        //TODO 判断权限
+        $validated = $request->validate([
+            'name' => 'required',
+        ]);
+        $case = $request->get('case','');
+        $new = new Relation;
+        $new->name = $validated['name'];
+        if($request->has('case')){
+            $new->case = json_encode($request->get('case'),JSON_UNESCAPED_UNICODE);
+        }else{
+            $new->case = null;
+        }
+        if($request->has('to')){
+            $new->to = json_encode($request->get('to'),JSON_UNESCAPED_UNICODE);
+        }else{
+            $new->to = null;
+        }
+        $new->editor_id = $user['user_uid'];
+        $new->save();
+        return $this->ok(new RelationResource($new));
+
+    }
+
+    /**
+     * Display the specified resource.
+     *
+     * @param  \App\Models\Relation  $relation
+     * @return \Illuminate\Http\Response
+     */
+    public function show(Relation $relation)
+    {
+        //
+        return $this->ok(new RelationResource($relation));
+
+    }
+
+
+    /**
+     * Update the specified resource in storage.
+     *
+     * @param  \Illuminate\Http\Request  $request
+     * @param  \App\Models\Relation  $relation
+     * @return \Illuminate\Http\Response
+     */
+    public function update(Request $request, Relation $relation)
+    {
+        //
+        $user = AuthApi::current($request);
+        if(!$user){
+            return $this->error(__('auth.failed'));
+        }
+
+        $relation->name = $request->get('name');
+        if($request->has('case')){
+            $relation->case = json_encode($request->get('case'),JSON_UNESCAPED_UNICODE);
+        }else{
+            $relation->case = null;
+        }
+        if($request->has('to')){
+            $relation->to = json_encode($request->get('to'),JSON_UNESCAPED_UNICODE);
+        }else{
+            $relation->to = null;
+        }
+        $relation->editor_id = $user['user_uid'];
+        $relation->save();
+        return $this->ok(new RelationResource($relation));
+    }
+
+    /**
+     * Remove the specified resource from storage.
+     *
+     * @param  \Illuminate\Http\Request  $request
+     * @param  \App\Models\Relation  $relation
+     * @return \Illuminate\Http\Response
+     */
+    public function destroy(Request $request,Relation $relation)
+    {
+        //
+        $user = AuthApi::current($request);
+        if(!$user){
+            return $this->error(__('auth.failed'));
+        }
+        //TODO 判断当前用户是否有权限
+        $delete = 0;
+        $delete = $relation->delete();
+
+        return $this->ok($delete);
+    }
+}

+ 28 - 0
app/Http/Resources/NissayaEndingResource.php

@@ -0,0 +1,28 @@
+<?php
+
+namespace App\Http\Resources;
+
+use Illuminate\Http\Resources\Json\JsonResource;
+use App\Http\Api\UserApi;
+
+class NissayaEndingResource extends JsonResource
+{
+    /**
+     * Transform the resource into an array.
+     *
+     * @param  \Illuminate\Http\Request  $request
+     * @return array|\Illuminate\Contracts\Support\Arrayable|\JsonSerializable
+     */
+    public function toArray($request)
+    {
+        return [
+            "id"=>$this->id,
+            "ending"=> $this->ending,
+            "lang"=> $this->lang,
+            "relation"=> $this->relation,
+            "editor"=> UserApi::getById($this->editor_id),
+            "created_at"=> $this->created_at,
+            "updated_at"=> $this->updated_at,
+        ];
+    }
+}

+ 28 - 0
app/Http/Resources/RelationResource.php

@@ -0,0 +1,28 @@
+<?php
+
+namespace App\Http\Resources;
+
+use Illuminate\Http\Resources\Json\JsonResource;
+use App\Http\Api\UserApi;
+
+class RelationResource extends JsonResource
+{
+    /**
+     * Transform the resource into an array.
+     *
+     * @param  \Illuminate\Http\Request  $request
+     * @return array|\Illuminate\Contracts\Support\Arrayable|\JsonSerializable
+     */
+    public function toArray($request)
+    {
+        return [
+            "id"=>$this->id,
+            "name"=> $this->name,
+            "case"=> json_decode($this->case),
+            "to"=> json_decode($this->to),
+            "editor"=> UserApi::getById($this->editor_id),
+            "created_at"=> $this->created_at,
+            "updated_at"=> $this->updated_at,
+        ];
+    }
+}

+ 14 - 0
app/Models/NissayaEnding.php

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

+ 16 - 0
app/Models/Relation.php

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

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

@@ -0,0 +1,35 @@
+<?php
+
+use Illuminate\Database\Migrations\Migration;
+use Illuminate\Database\Schema\Blueprint;
+use Illuminate\Support\Facades\Schema;
+
+class CreateNissayaEndingsTable extends Migration
+{
+    /**
+     * Run the migrations.
+     *
+     * @return void
+     */
+    public function up()
+    {
+        Schema::create('nissaya_endings', function (Blueprint $table) {
+            $table->uuid('id')->primary()->default(DB::raw('uuid_generate_v1mc()'));
+            $table->string('ending',256)->index();
+            $table->string('lang',16)->index();
+            $table->string('relation',32)->index();
+            $table->uuid('editor_id');
+            $table->timestamps();
+        });
+    }
+
+    /**
+     * Reverse the migrations.
+     *
+     * @return void
+     */
+    public function down()
+    {
+        Schema::dropIfExists('nissaya_endings');
+    }
+}

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

@@ -0,0 +1,35 @@
+<?php
+
+use Illuminate\Database\Migrations\Migration;
+use Illuminate\Database\Schema\Blueprint;
+use Illuminate\Support\Facades\Schema;
+
+class CreateRelationsTable extends Migration
+{
+    /**
+     * Run the migrations.
+     *
+     * @return void
+     */
+    public function up()
+    {
+        Schema::create('relations', function (Blueprint $table) {
+            $table->uuid('id')->primary()->default(DB::raw('uuid_generate_v1mc()'));
+            $table->string('name',256)->unique();
+            $table->json('case')->nullable();
+            $table->json('to')->nullable();
+            $table->uuid('editor_id');
+            $table->timestamps();
+        });
+    }
+
+    /**
+     * Reverse the migrations.
+     *
+     * @return void
+     */
+    public function down()
+    {
+        Schema::dropIfExists('relations');
+    }
+}

+ 62 - 0
resources/lang/zh-Hans/grammar.php

@@ -0,0 +1,62 @@
+<?php
+
+return [
+
+    /*
+    |--------------------------------------------------------------------------
+    | site Language Lines
+    |--------------------------------------------------------------------------
+    |
+    |
+    */
+
+    'nom' => '主格',
+    'acc' => '宾格',
+    'gen' => '属格',
+    'dat' => '为格',
+    'inst' => '工具格',
+    'abl' => '来源格',
+    'loc' => '处格',
+    'voc' => '呼格',
+    "relations.iad.label" => "同类修饰",
+    "relations.asv.label" => "施动者➡动词",
+    "relations.aov.label" => "受动者➡动词",
+    "relations.daso-p.label" => "主语➡系动词",
+    "relations.daso-s.label" => "表语➡系动词",
+    "relations.nio.label" => "被描述(主)➡定性(表)",
+    "relations.nid.label" => "待命名➡命名",
+    "relations.dasd-p.label" => "<命名>主➡系",
+    "relations.dasd-s.label" => "<命名>表➡系",
+    "relations.dao-p.label" => "被动语态双宾语-首要",
+    "relations.dao-s.label" => "被动语态双宾语-次要",
+    "relations.iov.label" => "受动者➡动词",
+    "relations.dio-p.label" => "双宾语<主要>➡动词",
+    "relations.dio-s.label" => "双宾语<次要>➡动词",
+    "relations.dis-p.label" => "主➡系(被动)",
+    "relations.dis-s.label" => "表➡系(被动)",
+    "relations.stc.label" => "时空连续 ➡ 持续动作",
+    "relations.adv.label" => "动词修饰词 ➡ 动词",
+    "relations.imp.label" => "方式 ➡ 动词",
+    "relations.soe.label" => "<带连词>伴随关系",
+    "relations.soi.label" => "<无连词>伴随关系",
+    "relations.isv.label" => "<非主格>施动者 ➡ 动词",
+    "relations.cau.label" => "因 ➡ 果/归因",
+    "relations.iov-c.label" => "被使役宾语 ➡ 动词",
+    "relations.adj.label" => "名词的形容 ➡ 被形容",
+    "relations.rec.label" => "接收者 ➡ 授予",
+    "relations.pur.label" => "目的 ➡ 动词",
+    "relations.det.label" => "出发地 ➡ 出发",
+    "relations.coc.label" => "区分比较",
+    "relations.pos.label" => "所有 ➡ 被所有",
+    "relations.coi.label" => "包含[全集] ➡ 被包含[子集]元素/集合 ➡ 个体元素",
+    "relations.lov.label" => "容器 ➡ 动词/容纳 ➡ 被容纳",
+    "relations.mot.label" => "表现 ➡ 有表现",
+    "relations.whp.label" => "整体 ➡ 局部",
+    "relations.def.label" => "特征限定",
+    "relations.ac.label" => "绝对从句",
+    "relations.avc.label" => "绝对语态从句",
+    "relations.qus.label" => "引号内 ➡ 引号",
+    "relations.qum.label" => "引号 ➡ 引号外",
+    "relations.enu.label" => "罗列 ➡ 破折号",
+    "relations.enm.label" => "破折号 ➡ 被罗列",
+];

+ 77 - 0
tests/Feature/NissayaEndingTest.php

@@ -0,0 +1,77 @@
+<?php
+
+namespace Tests\Feature;
+
+use Illuminate\Foundation\Testing\RefreshDatabase;
+use Illuminate\Foundation\Testing\WithFaker;
+use Tests\TestCase;
+use App\Models\NissayaEnding;
+
+class NissayaEndingTest 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/nissaya-ending?search=a');
+
+        $response->assertStatus(200);
+    }
+
+    public function test_store()
+    {
+        //testing store
+        $response = $this->withHeaders([
+            'Authorization' => $this->token,
+        ])->json('POST', '/api/v2/nissaya-ending',
+                    [
+                        'ending'=>'test1',
+                        'lang'=>'my',
+                        'relation'=>'isv',
+                    ]);
+
+        $response->assertOk();
+    }
+
+    public function test_show()
+    {
+        //testing store
+        $id = NissayaEnding::value('id');
+        $response = $this->get("/api/v2/nissaya-ending/{$id}");
+
+        $response->assertStatus(200);
+    }
+
+    public function test_update()
+    {
+        //testing store
+        sleep(1);
+        $id = NissayaEnding::value('id');
+        $response = $this->withHeaders([
+            'Authorization' => $this->token,
+        ])->json('PUT', "/api/v2/nissaya-ending/{$id}",
+                    [
+                        'ending'=>'ending_update',
+                        'lang'=>'my',
+                        'relation'=>'isv',
+                    ]);
+
+        $response->assertOk();
+    }
+
+    public function test_delete(){
+
+        //testing delete
+        $id = NissayaEnding::value("id");
+        $response = $this->withHeaders([
+            'Authorization' => $this->token,
+        ])->delete("/api/v2/nissaya-ending/{$id}");
+
+        $response->assertOk();
+    }
+}

+ 86 - 0
tests/Feature/RelationTest.php

@@ -0,0 +1,86 @@
+<?php
+
+namespace Tests\Feature;
+
+use Illuminate\Foundation\Testing\RefreshDatabase;
+use Illuminate\Foundation\Testing\WithFaker;
+use Tests\TestCase;
+use App\Models\Relation;
+
+class RelationTest 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/relation?search=a');
+
+        $response->assertStatus(200);
+    }
+
+    public function test_store()
+    {
+        //testing store
+        $response = $this->withHeaders([
+            'Authorization' => $this->token,
+        ])->json('POST', '/api/v2/relation',
+                    [
+                        'name'=>'isv',
+                        'case'=>['gen'],
+                    ]);
+
+        $response->assertOk();
+
+        $response = $this->withHeaders([
+            'Authorization' => $this->token,
+        ])->json('POST', '/api/v2/relation',
+                    [
+                        'name'=>'iov',
+                    ]);
+
+        $response->assertOk();
+    }
+
+    public function test_show()
+    {
+        //testing store
+        $id = Relation::value('id');
+        $response = $this->get("/api/v2/relation/{$id}");
+
+        $response->assertStatus(200);
+    }
+
+    public function test_update()
+    {
+        //testing store
+        sleep(1);
+        $id = Relation::value('id');
+        $response = $this->withHeaders([
+            'Authorization' => $this->token,
+        ])->json('PUT', "/api/v2/relation/{$id}",
+                [
+                    'name'=>'iov_2',
+                    'case'=>['inst'],
+                    'to'=>['inst'],
+                ]);
+
+        $response->assertOk();
+    }
+
+    public function test_delete(){
+
+        //testing delete
+        $id = Relation::value("id");
+        $response = $this->withHeaders([
+            'Authorization' => $this->token,
+        ])->delete("/api/v2/relation/{$id}");
+
+        $response->assertOk();
+    }
+
+}