organization_id !== $organization->getKey()) { throw new AuthorizationException('Tag does not belong to organization'); } } /** * Get clients * * @return ClientCollection * * @throws AuthorizationException * * @operationId getClients */ public function index(Organization $organization, ClientIndexRequest $request): ClientCollection { $this->checkPermission($organization, 'clients:view'); $clientsQuery = Client::query() ->whereBelongsTo($organization, 'organization') ->orderBy('created_at', 'desc'); $filterArchived = $request->getFilterArchived(); if ($filterArchived === 'true') { $clientsQuery->whereNotNull('archived_at'); } elseif ($filterArchived === 'false') { $clientsQuery->whereNull('archived_at'); } $clients = $clientsQuery->paginate(config('app.pagination_per_page_default')); return new ClientCollection($clients); } /** * Create client * * @throws AuthorizationException * * @operationId createClient */ public function store(Organization $organization, ClientStoreRequest $request): ClientResource { $this->checkPermission($organization, 'clients:create'); $client = new Client; $client->name = $request->input('name'); $client->organization()->associate($organization); $client->save(); return new ClientResource($client); } /** * Update client * * @throws AuthorizationException * * @operationId updateClient */ public function update(Organization $organization, Client $client, ClientUpdateRequest $request): ClientResource { $this->checkPermission($organization, 'clients:update', $client); $client->name = $request->input('name'); if ($request->has('is_archived')) { $client->archived_at = $request->getIsArchived() ? Carbon::now() : null; } $client->save(); return new ClientResource($client); } /** * Delete client * * @throws AuthorizationException|EntityStillInUseApiException * * @operationId deleteClient */ public function destroy(Organization $organization, Client $client): JsonResponse { $this->checkPermission($organization, 'clients:delete', $client); if ($client->projects()->exists()) { throw new EntityStillInUseApiException('client', 'project'); } $client->delete(); return response()->json(null, 204); } }