ActionController.php 2.6 KB

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