ActionController.php 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. <?php
  2. namespace App\Http\Controllers\API;
  3. use App\Http\Controllers\Controller;
  4. use App\Http\Requests\API\Action\CommentRequest;
  5. use App\Models\Action;
  6. use App\Models\Enums\ActionObjectType;
  7. use App\Models\Enums\FileObjectType;
  8. use App\Models\Enums\ObjectAction;
  9. use App\Models\File;
  10. use App\Repositories\ActionRepository;
  11. use App\Repositories\ContainerFileRepository;
  12. use App\Services\File\FileAssociationService;
  13. use App\Services\File\ImageUrlService;
  14. use DOMDocument;
  15. use Illuminate\Http\Request;
  16. use Illuminate\Support\Facades\Auth;
  17. class ActionController extends Controller
  18. {
  19. public function history(string $objectType, string $objectId)
  20. {
  21. $actionObjectType = ActionObjectType::from($objectType);
  22. $model = $actionObjectType->modelBuilder()->where("company_id", Auth::user()->company_id)->findOrFail($objectId);
  23. $objectIds = [$objectId];
  24. if (ActionObjectType::CONTAINER_FILE->value == $objectType) {
  25. ContainerFileRepository::getAllVersionFiles($model)->each(function ($file) use (&$objectIds) {
  26. $objectIds[] = $file->id;
  27. });
  28. }
  29. return $this->success([
  30. 'data' => ActionRepository::actionWithHistory($actionObjectType, $objectIds),
  31. ]);
  32. }
  33. /**
  34. * action comment
  35. *
  36. * @param CommentRequest $request
  37. * @param string $objectType
  38. * @param string $objectId
  39. */
  40. public function comment(FileAssociationService $service, CommentRequest $request, string $objectType, string $objectId,ImageUrlService $imgService)
  41. {
  42. $actionObjectType = ActionObjectType::from($objectType);
  43. $comment=$request->get('comment');
  44. $newComment=$imgService->interceptImageUrl($comment);
  45. $object = $actionObjectType?->modelBuilderAllowed($objectId)
  46. ->where("company_id", Auth::user()->company_id)
  47. ->findOrFail($objectId);
  48. $projectId = match ($actionObjectType) {
  49. ActionObjectType::PROJECT => $objectId,
  50. ActionObjectType::TASK => $object->project_id,
  51. default => null
  52. };
  53. $service->check(
  54. $request->get("file_ids", []),
  55. FileObjectType::ACTION,
  56. );
  57. $action = ActionRepository::create(
  58. $objectId,
  59. $actionObjectType,
  60. ObjectAction::COMMENTED,
  61. projectId: $projectId,
  62. comment: $newComment,
  63. );
  64. $service->association($action->id);
  65. return $this->created();
  66. }
  67. /**
  68. * update comment
  69. *
  70. * @param CommentRequest $request
  71. * @param string $id
  72. * @return \Illuminate\Http\Response
  73. */
  74. public function updateComment(CommentRequest $request, string $id,ImageUrlService $imgService)
  75. {
  76. $action = Action::query()->findOrFail($id);
  77. $actionObjectType = ActionObjectType::from($action->object_type);
  78. $actionObjectType?->modelBuilderAllowed($action->object_id)
  79. ->where("company_id", Auth::user()->company_id)
  80. ->findOrFail($action->object_id);
  81. $comment=$request->get('comment');
  82. $newComment=$imgService->interceptImageUrl($comment);
  83. $action->comment = $newComment;
  84. $action->save();
  85. return $this->noContent();
  86. }
  87. public function lastDynamic(){
  88. return $this->success([
  89. 'data' => ActionRepository::allLatestDynamic()
  90. ]);
  91. }
  92. }