瀏覽代碼

add member_count

visuddhinanda 3 年之前
父節點
當前提交
d7e2106fc3
共有 1 個文件被更改,包括 23 次插入3 次删除
  1. 23 3
      app/Http/Resources/CourseResource.php

+ 23 - 3
app/Http/Resources/CourseResource.php

@@ -4,6 +4,10 @@ namespace App\Http\Resources;
 
 use Illuminate\Http\Resources\Json\JsonResource;
 use App\Http\Api\UserApi;
+use App\Http\Api\StudioApi;
+use App\Models\Collection;
+use App\Models\Channel;
+use App\Models\CourseMember;
 
 class CourseResource extends JsonResource
 {
@@ -15,21 +19,37 @@ class CourseResource extends JsonResource
      */
     public function toArray($request)
     {
-        return [
+        $data = [
             "id"=>$this->id,
             "title"=> $this->title,
             "subtitle"=> $this->subtitle,
             "teacher"=> UserApi::getById($this->teacher),
             "course_count"=>10,
-            "type"=> 1,
-            "anthology_id"=> '',
+            "member_count"=>CourseMember::where('course_id',$this->id)->count(),
+            "publicity"=> $this->publicity,
             "start_at"=> $this->start_at,
             "end_at"=> $this->end_at,
             "content"=> $this->content,
             "content_type"=> $this->content_type,
             "cover"=> $this->cover,
+            "channel_id"=>$this->channel_id,
             "created_at"=> $this->created_at,
             "updated_at"=> $this->updated_at,
         ];
+        $textbook = Collection::where('uid',$this->anthology_id)->select(['uid','title','owner'])->first();
+        if($textbook){
+            $data['anthology_id'] = $textbook->uid;
+            $data['anthology_title'] = $textbook->title;
+            $data['anthology_owner'] = StudioApi::getById($textbook->owner);
+        }
+        if(!empty($this->channel_id)){
+            $channel = Channel::where('uid',$this->channel_id)->select(['name','owner_uid'])->first();
+            if($channel){
+                $data['channel_name'] = $channel->name;
+                $data['channel_owner'] = StudioApi::getById($channel->owner_uid);
+            }
+        }
+
+        return $data;
     }
 }