lasthourcloud/app/Http/Controllers/Api/ResourcesController.php

68 lines
2.1 KiB
PHP
Raw Normal View History

2024-03-07 10:35:00 +00:00
<?php
namespace App\Http\Controllers\Api;
use App\Http\Controllers\Controller;
use App\Models\Project;
use Illuminate\Http\Request;
2024-07-09 08:45:10 +00:00
use OpenApi\Attributes as OA;
2024-03-07 10:35:00 +00:00
2024-07-01 14:26:50 +00:00
class ResourcesController extends Controller
2024-03-07 10:35:00 +00:00
{
2024-07-09 08:45:10 +00:00
#[OA\Get(
summary: 'List',
description: 'Get all resources.',
path: '/resources',
security: [
['bearerAuth' => []],
],
tags: ['Resources'],
responses: [
new OA\Response(
response: 200,
description: 'Get all resources',
content: new OA\JsonContent(
type: 'string',
example: 'Content is very complex. Will be implemented later.',
),
),
new OA\Response(
response: 401,
ref: '#/components/responses/401',
),
new OA\Response(
response: 400,
ref: '#/components/responses/400',
),
]
)]
2024-03-07 10:35:00 +00:00
public function resources(Request $request)
{
2024-07-01 14:26:50 +00:00
$teamId = getTeamIdFromToken();
2024-03-07 10:35:00 +00:00
if (is_null($teamId)) {
2024-07-01 14:26:50 +00:00
return invalidTokenResponse();
2024-03-07 10:35:00 +00:00
}
$projects = Project::where('team_id', $teamId)->get();
$resources = collect();
$resources->push($projects->pluck('applications')->flatten());
$resources->push($projects->pluck('services')->flatten());
foreach (collect(DATABASE_TYPES) as $db) {
$resources->push($projects->pluck(str($db)->plural(2))->flatten());
}
2024-03-07 10:35:00 +00:00
$resources = $resources->flatten();
$resources = $resources->map(function ($resource) {
$payload = $resource->toArray();
if ($resource->getMorphClass() === 'App\Models\Service') {
$payload['status'] = $resource->status();
} else {
$payload['status'] = $resource->status;
}
$payload['type'] = $resource->type();
2024-06-10 20:43:34 +00:00
return $payload;
});
2024-06-10 20:43:34 +00:00
2024-07-03 11:13:38 +00:00
return response()->json(serializeApiResponse($resources));
2024-03-07 10:35:00 +00:00
}
}