Skip to content

Commit

Permalink
move data processing to resources
Browse files Browse the repository at this point in the history
  • Loading branch information
AmonDeShir committed Aug 7, 2024
1 parent cb8784f commit 6ebf293
Show file tree
Hide file tree
Showing 7 changed files with 95 additions and 49 deletions.
18 changes: 3 additions & 15 deletions app/Http/Controllers/AuthorsController.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

namespace App\Http\Controllers;

use App\Http\Resources\AuthorResource;
use App\Models\WorkflowActor;
use Inertia\Inertia;
use Inertia\Response;
Expand All @@ -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]);
}
}
16 changes: 3 additions & 13 deletions app/Http/Controllers/RepositoriesController.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

namespace App\Http\Controllers;

use App\Http\Resources\RepositoryResource;
use App\Models\Repository;
use Inertia\Inertia;
use Inertia\Response;
Expand All @@ -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]);
}
Expand Down
25 changes: 4 additions & 21 deletions app/Http/Controllers/TableController.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

namespace App\Http\Controllers;

use App\Http\Resources\WorkflowJobResource;
use App\Models\WorkflowJob;
use Inertia\Inertia;
use Inertia\Response;
Expand All @@ -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]);
}
}
26 changes: 26 additions & 0 deletions app/Http/Resources/AuthorResource.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?php

declare(strict_types=1);

namespace App\Http\Resources;

use Illuminate\Http\Request;
use Illuminate\Http\Resources\Json\JsonResource;

class AuthorResource extends JsonResource
{
/**
* @return array<string, mixed>
*/
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,
];
}
}
26 changes: 26 additions & 0 deletions app/Http/Resources/RepositoryResource.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?php

declare(strict_types=1);

namespace App\Http\Resources;

use Illuminate\Http\Request;
use Illuminate\Http\Resources\Json\JsonResource;

class RepositoryResource extends JsonResource
{
/**
* @return array<string, mixed>
*/
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,
];
}
}
31 changes: 31 additions & 0 deletions app/Http/Resources/WorkflowJobResource.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?php

declare(strict_types=1);

namespace App\Http\Resources;

use Illuminate\Http\Request;
use Illuminate\Http\Resources\Json\JsonResource;

class WorkflowJobResource extends JsonResource
{
/**
* @return array<string, mixed>
*/
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,
];
}
}
2 changes: 2 additions & 0 deletions app/Providers/AppServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

namespace App\Providers;

use Illuminate\Http\Resources\Json\JsonResource;
use Illuminate\Support\ServiceProvider;

class AppServiceProvider extends ServiceProvider
Expand All @@ -14,5 +15,6 @@ public function register(): void

public function boot(): void
{
JsonResource::withoutWrapping();
}
}

0 comments on commit 6ebf293

Please sign in to comment.