diff --git a/app/Http/Controllers/AuthorsController.php b/app/Http/Controllers/AuthorsController.php index 884d4dbf..951fba50 100644 --- a/app/Http/Controllers/AuthorsController.php +++ b/app/Http/Controllers/AuthorsController.php @@ -4,6 +4,7 @@ namespace App\Http\Controllers; +use App\Http\Resources\AuthorResource; use App\Models\WorkflowActor; use Inertia\Inertia; use Inertia\Response; @@ -15,21 +16,8 @@ public function __construct() {} public function show(): Response { $actors = WorkflowActor::with("workflowJobs")->get(); - $data = []; + $data = AuthorResource::collection($actors); - foreach ($actors as $actor) { - $data[] = [ - "id" => $actor->id, - "name" => $actor->name, - "github_id" => $actor->github_id, - "avatar_url" => $actor->avatar_url, - "minutes" => $actor->totalMinutes, - "price" => $actor->totalPrice, - ]; - } - - return Inertia::render("Authors", [ - "data" => $data, - ]); + return Inertia::render("Authors", ["data" => $data]); } } diff --git a/app/Http/Controllers/RepositoriesController.php b/app/Http/Controllers/RepositoriesController.php index f52a4817..853c8cb3 100644 --- a/app/Http/Controllers/RepositoriesController.php +++ b/app/Http/Controllers/RepositoriesController.php @@ -4,6 +4,7 @@ namespace App\Http\Controllers; +use App\Http\Resources\RepositoryResource; use App\Models\Repository; use Inertia\Inertia; use Inertia\Response; @@ -14,19 +15,8 @@ public function __construct() {} public function show(): Response { - $repositories = Repository::with("workflowJobs")->with("organization")->get(); - $data = []; - - foreach ($repositories as $repo) { - $data[] = [ - "id" => $repo->id, - "name" => $repo->name, - "organization" => $repo->organization->name, - "avatar_url" => $repo->organization->avatar_url, - "minutes" => $repo->totalMinutes, - "price" => $repo->totalPrice, - ]; - } + $repositories = Repository::with(["workflowJobs", "organization"])->get(); + $data = RepositoryResource::collection($repositories); return Inertia::render("Repositories", ["data" => $data]); } diff --git a/app/Http/Controllers/TableController.php b/app/Http/Controllers/TableController.php index ccec1fa3..5d3bcf89 100644 --- a/app/Http/Controllers/TableController.php +++ b/app/Http/Controllers/TableController.php @@ -4,6 +4,7 @@ namespace App\Http\Controllers; +use App\Http\Resources\WorkflowJobResource; use App\Models\WorkflowJob; use Inertia\Inertia; use Inertia\Response; @@ -14,27 +15,9 @@ public function __construct() {} public function show(): Response { - $jobs = WorkflowJob::with("workflowRun.repository.organization")->with("workflowRun.workflowActor")->get(); - $data = []; + $jobs = WorkflowJob::with(["workflowRun.repository.organization", "workflowRun.workflowActor"])->get(); + $data = WorkflowJobResource::collection($jobs); - foreach ($jobs as $job) { - $data[] = [ - "id" => $job->workflowRun->id, - "date" => $job->workflowRun->github_created_at, - "organization" => $job->workflowRun->repository->organization->name, - "repository" => $job->workflowRun->repository->name, - "repository_id" => $job->workflowRun->repository->id, - "minutes" => $job->minutes, - "price_per_minute" => $job->price_per_unit, - "total_price" => $job->price, - "workflow" => $job->fullName, - "os" => $job->runner_os, - "actor" => $job->workflowRun->workflowActor, - ]; - } - - return Inertia::render("Table", [ - "data" => $data, - ]); + return Inertia::render("Table", ["data" => $data]); } } diff --git a/app/Http/Resources/AuthorResource.php b/app/Http/Resources/AuthorResource.php new file mode 100644 index 00000000..850c4176 --- /dev/null +++ b/app/Http/Resources/AuthorResource.php @@ -0,0 +1,26 @@ + + */ + public function toArray(Request $request): array + { + return [ + "id" => $this->id, + "name" => $this->name, + "github_id" => $this->github_id, + "avatar_url" => $this->avatar_url, + "minutes" => $this->totalMinutes, + "price" => $this->totalPrice, + ]; + } +} diff --git a/app/Http/Resources/RepositoryResource.php b/app/Http/Resources/RepositoryResource.php new file mode 100644 index 00000000..97582fb5 --- /dev/null +++ b/app/Http/Resources/RepositoryResource.php @@ -0,0 +1,26 @@ + + */ + public function toArray(Request $request): array + { + return [ + "id" => $this->id, + "name" => $this->name, + "organization" => $this->organization->name, + "avatar_url" => $this->organization->avatar_url, + "minutes" => $this->totalMinutes, + "price" => $this->totalPrice, + ]; + } +} diff --git a/app/Http/Resources/WorkflowJobResource.php b/app/Http/Resources/WorkflowJobResource.php new file mode 100644 index 00000000..d5b54c25 --- /dev/null +++ b/app/Http/Resources/WorkflowJobResource.php @@ -0,0 +1,31 @@ + + */ + public function toArray(Request $request): array + { + return [ + "id" => $this->workflowRun->id, + "date" => $this->workflowRun->github_created_at, + "organization" => $this->workflowRun->repository->organization->name, + "repository" => $this->workflowRun->repository->name, + "repository_id" => $this->workflowRun->repository->id, + "minutes" => $this->minutes, + "price_per_minute" => $this->price_per_unit, + "total_price" => $this->price, + "workflow" => $this->fullName, + "os" => $this->runner_os, + "actor" => $this->workflowRun->workflowActor, + ]; + } +} diff --git a/app/Providers/AppServiceProvider.php b/app/Providers/AppServiceProvider.php index dfdc1c7a..fcd238e8 100755 --- a/app/Providers/AppServiceProvider.php +++ b/app/Providers/AppServiceProvider.php @@ -4,6 +4,7 @@ namespace App\Providers; +use Illuminate\Http\Resources\Json\JsonResource; use Illuminate\Support\ServiceProvider; class AppServiceProvider extends ServiceProvider @@ -14,5 +15,6 @@ public function register(): void public function boot(): void { + JsonResource::withoutWrapping(); } }