ActionController.php 2.3 KB

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