CourseResource.php 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. <?php
  2. namespace App\Http\Resources;
  3. use Illuminate\Http\Resources\Json\JsonResource;
  4. use App\Http\Api\UserApi;
  5. use App\Http\Api\StudioApi;
  6. use App\Models\Collection;
  7. use App\Models\Channel;
  8. use App\Models\CourseMember;
  9. use App\Http\Api\AuthApi;
  10. class CourseResource extends JsonResource
  11. {
  12. /**
  13. * Transform the resource into an array.
  14. *
  15. * @param \Illuminate\Http\Request $request
  16. * @return array|\Illuminate\Contracts\Support\Arrayable|\JsonSerializable
  17. */
  18. public function toArray($request)
  19. {
  20. $data = [
  21. "id"=>$this->id,
  22. "title"=> $this->title,
  23. "subtitle"=> $this->subtitle,
  24. "summary"=> $this->summary,
  25. "teacher"=> UserApi::getById($this->teacher),
  26. "course_count"=>10,
  27. "member_count"=>CourseMember::where('course_id',$this->id)->count(),
  28. "publicity"=> $this->publicity,
  29. "start_at"=> $this->start_at,
  30. "end_at"=> $this->end_at,
  31. "content"=> $this->content,
  32. "content_type"=> $this->content_type,
  33. "cover"=> $this->cover,
  34. "channel_id"=>$this->channel_id,
  35. "join"=> $this->join,
  36. "request_exp"=> $this->request_exp,
  37. "created_at"=> $this->created_at,
  38. "updated_at"=> $this->updated_at,
  39. ];
  40. $textbook = Collection::where('uid',$this->anthology_id)->select(['uid','title','owner'])->first();
  41. if($textbook){
  42. $data['anthology_id'] = $textbook->uid;
  43. $data['anthology_title'] = $textbook->title;
  44. $data['anthology_owner'] = StudioApi::getById($textbook->owner);
  45. }
  46. if(!empty($this->channel_id)){
  47. $channel = Channel::where('uid',$this->channel_id)->select(['name','owner_uid'])->first();
  48. if($channel){
  49. $data['channel_name'] = $channel->name;
  50. $data['channel_owner'] = StudioApi::getById($channel->owner_uid);
  51. }
  52. }
  53. if($request->get('view')==="study"){
  54. $user = AuthApi::current($request);
  55. if(!$user){
  56. return $this->error(__('auth.failed'));
  57. }
  58. $course_member = CourseMember::where('user_id',$user["user_uid"])
  59. ->where('course_id',$this->id)
  60. ->select('status')
  61. ->first();
  62. if($course_member){
  63. $data['my_status'] = $course_member->status;
  64. }
  65. }else{
  66. //计算待审核
  67. $data['count_progressing'] = CourseMember::where('course_id',$this->id)
  68. ->where('status',"progressing")
  69. ->count();
  70. }
  71. return $data;
  72. }
  73. }