Skip to content

Commit

Permalink
✨ add API Endpoint to get all operators (#2626)
Browse files Browse the repository at this point in the history
  • Loading branch information
MrKrisKrisu authored May 30, 2024
1 parent f7a62ee commit 8496d0b
Show file tree
Hide file tree
Showing 11 changed files with 156 additions and 84 deletions.
4 changes: 4 additions & 0 deletions API_CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@ This is to help you keep track of the changes and to help you update your code a

## 2024-05-30

Added `GET /operator` endpoint to get a paginated list of all operators.

## 2024-05-30

Renamed `trainDuration` and `trainDistance` attributes to `totalDuration` and `totalDistance` in all `User` object.
(We have more than just trains.)

Expand Down
34 changes: 34 additions & 0 deletions app/Http/Controllers/API/v1/OperatorController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<?php

namespace App\Http\Controllers\API\v1;

use App\Http\Resources\OperatorResource;
use App\Models\HafasOperator;
use Illuminate\Http\Resources\Json\AnonymousResourceCollection;

class OperatorController extends Controller
{
/**
* @OA\Get(
* path="/operators",
* summary="Get a list of all operators.",
* tags={"Checkin"},
* @OA\Response(
* response=200,
* description="successful operation",
* @OA\JsonContent(
* @OA\Property(
* property="data",
* type="array",
* @OA\Items(ref="#/components/schemas/OperatorResource")
* )
* )
* )
* )
*
* @return AnonymousResourceCollection
*/
public function index(): AnonymousResourceCollection {
return OperatorResource::collection(HafasOperator::orderBy('name')->cursorPaginate(100));
}
}
9 changes: 3 additions & 6 deletions app/Http/Controllers/API/v1/ReportController.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,13 +25,10 @@ class ReportController extends Controller
* required=true,
* @OA\JsonContent(
* required={"subject_type", "subject_id", "reason"},
* @OA\Property(property="subject_type", type="string", enum={"Event", "Status", "User"},
* example="Status"),
* @OA\Property(property="subject_type", type="string", enum={"Event", "Status", "User"}, example="Status"),
* @OA\Property(property="subject_id", type="integer", example=1),
* @OA\Property(property="reason", type="string", enum={"inappropriate", "implausible", "spam",
* "illegal", "other"}, example="inappropriate"),
* @OA\Property(property="description", type="string", example="The status is inappropriate
* because...", nullable=true),
* @OA\Property(property="reason", type="string", enum={"inappropriate", "implausible", "spam", "illegal", "other"}, example="inappropriate"),
* @OA\Property(property="description", type="string", example="The status is inappropriate because...", nullable=true),
* ),
* ),
* @OA\Response(response=200, description="The report was successfully created."),
Expand Down
9 changes: 3 additions & 6 deletions app/Http/Controllers/API/v1/StatisticsController.php
Original file line number Diff line number Diff line change
Expand Up @@ -200,8 +200,7 @@ public function leaderboardForMonth(string $date): AnonymousResourceCollection {
* @OA\Items(
* @OA\Property(property="name", ref="#/components/schemas/TrainCategoryEnum"),
* @OA\Property(property="count", type="integer", example=11),
* @OA\Property(property="duration", type="integer", example=425, description="Duration in
* minutes"),
* @OA\Property(property="duration", type="integer", example=425, description="Duration in minutes"),
* )
* ),
* @OA\Property(
Expand All @@ -211,8 +210,7 @@ public function leaderboardForMonth(string $date): AnonymousResourceCollection {
* @OA\Items(
* @OA\Property(property="name", example="Gertruds Verkehrsgesellschaft mbH"),
* @OA\Property(property="count", type="integer", example=10),
* @OA\Property(property="duration", type="integer", example=424, description="Duration in
* minutes"),
* @OA\Property(property="duration", type="integer", example=424, description="Duration in minutes"),
* )
* ),
* @OA\Property(
Expand All @@ -222,8 +220,7 @@ public function leaderboardForMonth(string $date): AnonymousResourceCollection {
* @OA\Items(
* @OA\Property(property="date", type="string", example="2021-01-01T00:00:00.000Z"),
* @OA\Property(property="count", type="integer", example=10),
* @OA\Property(property="duration", type="integer", example=424, description="Duration in
* minutes"),
* @OA\Property(property="duration", type="integer", example=424, description="Duration in minutes"),
* )
* ),
* )
Expand Down
2 changes: 0 additions & 2 deletions app/Http/Controllers/API/v1/TripController.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,7 @@
use App\Http\Controllers\Backend\Transport\ManualTripCreator;
use App\Http\Resources\TripResource;
use App\Models\HafasOperator;
use App\Models\Trip;
use App\Models\Station;
use App\Models\Stopover;
use Carbon\Carbon;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;
Expand Down
16 changes: 12 additions & 4 deletions app/Http/Resources/OperatorResource.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,21 @@

use Illuminate\Http\Resources\Json\JsonResource;

/**
* @OA\Schema(
* schema="OperatorResource",
* @OA\Property(property="id", type="integer", example=1),
* @OA\Property(property="identifier", type="string", example="db-regio-ag-nord"),
* @OA\Property(property="name", type="string", example="DB Regio AG Nord")
* )
*/
class OperatorResource extends JsonResource
{
public function toArray($request)
{
public function toArray($request) {
return [
'identifier' => $this->hafas_id,
'name' => $this->name
'id' => $this->id,
'identifier' => $this->hafas_id, //TODO: rename to... i don't know, but not identifier
'name' => $this->name
];
}
}
34 changes: 0 additions & 34 deletions app/Virtual/Models/Operator.php

This file was deleted.

8 changes: 4 additions & 4 deletions database/factories/HafasOperatorFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,16 @@

namespace Database\Factories;

use App\Models\HafasOperator;
use Illuminate\Database\Eloquent\Factories\Factory;
use JetBrains\PhpStorm\ArrayShape;
use Illuminate\Support\Str;

class HafasOperatorFactory extends Factory
{
public function definition(): array {
$companyName = $this->faker->company;
return [
'hafas_id' => $this->faker->hexColor,
'name' => $this->faker->company
'hafas_id' => Str::slug($companyName, '_'),
'name' => $companyName,
];
}
}
2 changes: 2 additions & 0 deletions routes/api.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
use App\Http\Controllers\API\v1\IcsController;
use App\Http\Controllers\API\v1\LikesController;
use App\Http\Controllers\API\v1\NotificationsController;
use App\Http\Controllers\API\v1\OperatorController;
use App\Http\Controllers\API\v1\PrivacyPolicyController;
use App\Http\Controllers\API\v1\ReportController;
use App\Http\Controllers\API\v1\SessionController;
Expand Down Expand Up @@ -176,6 +177,7 @@
Route::put('station/{oldStationId}/merge/{newStationId}', [StationController::class, 'merge']); // currently admin/backend only

Route::apiResource('report', ReportController::class);
Route::apiResource('operator', OperatorController::class)->only(['index']);
});

Route::group(['middleware' => ['privacy-policy']], static function() {
Expand Down
77 changes: 49 additions & 28 deletions storage/api-docs/api-docs.json
Original file line number Diff line number Diff line change
Expand Up @@ -1247,6 +1247,35 @@
]
}
},
"/operators": {
"get": {
"tags": [
"Checkin"
],
"summary": "Get a list of all operators.",
"operationId": "bcfcf8686980cf0fcdc751b2e13fa4f7",
"responses": {
"200": {
"description": "successful operation",
"content": {
"application/json": {
"schema": {
"properties": {
"data": {
"type": "array",
"items": {
"$ref": "#/components/schemas/OperatorResource"
}
}
},
"type": "object"
}
}
}
}
}
}
},
"/static/privacy": {
"get": {
"tags": [
Expand Down Expand Up @@ -1360,7 +1389,7 @@
},
"description": {
"type": "string",
"example": "The status is inappropriate\n * because...",
"example": "The status is inappropriate because...",
"nullable": true
}
},
Expand Down Expand Up @@ -4542,6 +4571,23 @@
},
"type": "object"
},
"OperatorResource": {
"properties": {
"id": {
"type": "integer",
"example": 1
},
"identifier": {
"type": "string",
"example": "db-regio-ag-nord"
},
"name": {
"type": "string",
"example": "DB Regio AG Nord"
}
},
"type": "object"
},
"StationResource": {
"title": "Station",
"properties": {
Expand Down Expand Up @@ -5108,26 +5154,6 @@
"name": "Notification"
}
},
"Operator": {
"title": "Operator",
"description": "Operator of a mean of transport",
"properties": {
"identifier": {
"title": "identifier",
"type": "string",
"example": "sbb"
},
"name": {
"title": "name",
"type": "string",
"example": "SBB"
}
},
"type": "object",
"xml": {
"name": "Operator"
}
},
"PaginationPage": {
"title": "PaginationPage",
"description": "pagination links",
Expand Down Expand Up @@ -5479,14 +5505,9 @@
"$ref": "#/components/schemas/Stopover"
},
"operator": {
"oneOf": [
{
"$ref": "#/components/schemas/Operator"
}
],
"nullable": true,
"title": "operator",
"description": "Operator of the mean of transport"
"description": "Operator of the mean of transport",
"nullable": true
}
},
"type": "object",
Expand Down
45 changes: 45 additions & 0 deletions tests/Feature/APIv1/OperatorTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
<?php

namespace Tests\Feature\APIv1;

use App\Models\HafasOperator;
use App\Models\User;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Laravel\Passport\Passport;
use Tests\ApiTestCase;

class OperatorTest extends ApiTestCase
{

use RefreshDatabase;

public function testOperatorIndex(): void {
Passport::actingAs(User::factory()->create(), ['*']);

HafasOperator::factory()->count(3)->create();

$response = $this->get('/api/v1/operator');
$response->assertOk();
$response->assertJsonCount(3, 'data');
$response->assertJsonStructure([
'data' => [
'*' => [
'id',
'name',
]
],
'links' => [
'first',
'last',
'prev',
'next',
],
'meta' => [
'path',
'per_page',
'next_cursor',
'prev_cursor',
],
]);
}
}

0 comments on commit 8496d0b

Please sign in to comment.