ActionController.php 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  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\Repositories\ActionRepository;
  10. use App\Services\File\FileAssociationService;
  11. use App\Services\File\ImageUrlService;
  12. use DOMDocument;
  13. use Illuminate\Http\Request;
  14. use Illuminate\Support\Facades\Auth;
  15. class ActionController extends Controller
  16. {
  17. public function history(string $objectType, string $objectId)
  18. {
  19. $actionObjectType = ActionObjectType::from($objectType);
  20. $actionObjectType->modelBuilder()->where("company_id", Auth::user()->company_id)->findOrFail($objectId);
  21. return $this->success([
  22. 'data' => ActionRepository::actionWithHistory($actionObjectType, $objectId),
  23. ]);
  24. }
  25. /**
  26. * action comment
  27. *
  28. * @param CommentRequest $request
  29. * @param string $objectType
  30. * @param string $objectId
  31. * @return \Illuminate\Http\Responseassociation
  32. */
  33. public function comment(FileAssociationService $service, CommentRequest $request, string $objectType, string $objectId,ImageUrlService $imgService)
  34. {
  35. $actionObjectType = ActionObjectType::from($objectType);
  36. $comment=$request->get('comment');
  37. $newComment=$imgService->interceptImageUrl($comment);
  38. $object = $actionObjectType?->modelBuilderAllowed($objectId)
  39. ->where("company_id", Auth::user()->company_id)
  40. ->findOrFail($objectId);
  41. $projectId = match ($actionObjectType) {
  42. ActionObjectType::PROJECT => $objectId,
  43. ActionObjectType::TASK => $object->project_id,
  44. default => null
  45. };
  46. $action = ActionRepository::create(
  47. $objectId,
  48. $actionObjectType,
  49. ObjectAction::COMMENTED,
  50. projectId: $projectId,
  51. comment: $newComment,
  52. );
  53. $service->association(
  54. $request->get("file_ids", []),
  55. $action->id,
  56. FileObjectType::ACTION
  57. );
  58. return $this->created();
  59. }
  60. /**
  61. * update comment
  62. *
  63. * @param CommentRequest $request
  64. * @param string $id
  65. * @return \Illuminate\Http\Response
  66. */
  67. public function updateComment(CommentRequest $request, string $id,ImageUrlService $imgService)
  68. {
  69. $action = Action::query()->findOrFail($id);
  70. $actionObjectType = ActionObjectType::from($action->object_type);
  71. $actionObjectType?->modelBuilderAllowed($action->object_id)
  72. ->where("company_id", Auth::user()->company_id)
  73. ->findOrFail($action->object_id);
  74. $comment=$request->get('comment');
  75. $newComment=$imgService->interceptImageUrl($comment);
  76. $action->comment = $newComment;
  77. $action->save();
  78. return $this->noContent();
  79. }
  80. }