ActionController.php 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  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. */
  32. public function comment(FileAssociationService $service, CommentRequest $request, string $objectType, string $objectId,ImageUrlService $imgService)
  33. {
  34. $actionObjectType = ActionObjectType::from($objectType);
  35. $comment=$request->get('comment');
  36. $newComment=$imgService->interceptImageUrl($comment);
  37. $object = $actionObjectType?->modelBuilderAllowed($objectId)
  38. ->where("company_id", Auth::user()->company_id)
  39. ->findOrFail($objectId);
  40. $projectId = match ($actionObjectType) {
  41. ActionObjectType::PROJECT => $objectId,
  42. ActionObjectType::TASK => $object->project_id,
  43. default => null
  44. };
  45. $service->check(
  46. $request->get("file_ids", []),
  47. FileObjectType::ACTION,
  48. );
  49. $action = ActionRepository::create(
  50. $objectId,
  51. $actionObjectType,
  52. ObjectAction::COMMENTED,
  53. projectId: $projectId,
  54. comment: $newComment,
  55. );
  56. $service->association($action->id);
  57. return $this->created();
  58. }
  59. /**
  60. * update comment
  61. *
  62. * @param CommentRequest $request
  63. * @param string $id
  64. * @return \Illuminate\Http\Response
  65. */
  66. public function updateComment(CommentRequest $request, string $id,ImageUrlService $imgService)
  67. {
  68. $action = Action::query()->findOrFail($id);
  69. $actionObjectType = ActionObjectType::from($action->object_type);
  70. $actionObjectType?->modelBuilderAllowed($action->object_id)
  71. ->where("company_id", Auth::user()->company_id)
  72. ->findOrFail($action->object_id);
  73. $comment=$request->get('comment');
  74. $newComment=$imgService->interceptImageUrl($comment);
  75. $action->comment = $newComment;
  76. $action->save();
  77. return $this->noContent();
  78. }
  79. public function lastDynamic(){
  80. return $this->success([
  81. 'data' => ActionRepository::allLatestDynamic()
  82. ]);
  83. }
  84. }