TaskResource.php 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. <?php
  2. namespace App\Http\Resources;
  3. use App\Http\Api\MdRender;
  4. use App\Http\Api\ProjectApi;
  5. use App\Http\Api\StudioApi;
  6. use App\Http\Api\TaskApi;
  7. use App\Http\Api\UserApi;
  8. use App\Models\TaskAssignee;
  9. use Illuminate\Contracts\Support\Arrayable;
  10. use Illuminate\Http\Request;
  11. use Illuminate\Http\Resources\Json\JsonResource;
  12. use Illuminate\Support\Str;
  13. class TaskResource extends JsonResource
  14. {
  15. /**
  16. * Transform the resource into an array.
  17. *
  18. * @param Request $request
  19. * @return array|Arrayable|\JsonSerializable
  20. */
  21. public function toArray($request)
  22. {
  23. $htmlRender = new MdRender([
  24. 'mode' => 'read',
  25. 'format' => 'react',
  26. 'footnote' => true,
  27. 'origin' => $request->input('origin', true),
  28. 'paragraph' => $request->input('paragraph', false),
  29. ]);
  30. $data = [
  31. 'id' => $this->id,
  32. 'title' => $this->title,
  33. 'description' => $this->description,
  34. 'type' => $this->type,
  35. 'category' => $this->category,
  36. 'progress' => $this->progress,
  37. 'parent_id' => $this->parent_id,
  38. 'parent' => TaskApi::getById($this->parent_id),
  39. 'roles' => $this->roles,
  40. 'executor_id' => $this->executor_id,
  41. 'executor_relation_task_id' => $this->executor_relation_task_id,
  42. 'executor_relation_task' => TaskApi::getById($this->executor_relation_task_id),
  43. 'pre_task' => TaskApi::getPreTasks($this->id),
  44. 'next_task' => TaskApi::getNextTasks($this->id),
  45. 'is_milestone' => $this->is_milestone,
  46. 'project_id' => $this->project_id,
  47. 'project' => ProjectApi::getById($this->project_id),
  48. 'owner_id' => $this->owner_id,
  49. 'owner' => StudioApi::getById($this->owner_id),
  50. 'editor_id' => $this->editor_id,
  51. 'editor' => UserApi::getByUuid($this->editor_id),
  52. 'order' => $this->order,
  53. 'status' => $this->status,
  54. 'created_at' => $this->created_at,
  55. 'updated_at' => $this->updated_at,
  56. 'started_at' => $this->started_at,
  57. 'finished_at' => $this->finished_at,
  58. ];
  59. $assignees = TaskAssignee::where('task_id', $this->id)->select('assignee_id')->get();
  60. if (count($assignees) > 0) {
  61. $assignees_id = [];
  62. foreach ($assignees as $key => $value) {
  63. $assignees_id[] = $value->assignee_id;
  64. }
  65. $data['assignees_id'] = $assignees_id;
  66. $data['assignees'] = UserApi::getListByUuid($assignees_id);
  67. }
  68. if (! empty($this->description)) {
  69. $data['html'] = $htmlRender->convert($this->description, []);
  70. }
  71. if (Str::isUuid($this->executor_id)) {
  72. $data['executor'] = UserApi::getByUuid($this->executor_id);
  73. }
  74. return $data;
  75. }
  76. }