Skip to content

Commit

Permalink
Merge pull request #41 from VMGWARE/2-feature-pdc-pre-departure-clear…
Browse files Browse the repository at this point in the history
…ance-generator-option

Feature: Add PDCController with a generate method to generate Pre-Departure Clearances (PDC)
  • Loading branch information
Codycody31 authored Sep 15, 2023
2 parents 3328e61 + eb6c95c commit e24cc58
Show file tree
Hide file tree
Showing 5 changed files with 236 additions and 0 deletions.
77 changes: 77 additions & 0 deletions src/app/Http/Controllers/API/PDCController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
<?php

namespace App\Http\Controllers\API;

use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Validator;
use Vyuldashev\LaravelOpenApi\Attributes as OpenApi;
use App\Custom\Helpers;

#[OpenApi\PathItem]
class PDCController extends Controller
{

/**
* Generate a Pre-Departure Clearance (PDC) for a flight.
*
* This endpoint generates a PDC for a flight based on the provided parameters.
*
* @param Request $request An instance of the HTTP request which should contain both 'icao' and 'id' parameters.
* @return JsonResponse A JSON response. It can be:
* 1. A success response with the generated PDC.
* 2. An error response indicating an error with the request.
*/
#[OpenApi\Operation(tags: ['Miscellaneous'])]
#[OpenApi\RequestBody(factory: \App\OpenApi\RequestBodies\PDC\GenerateRequestBody::class)]
#[OpenApi\Response(factory: \App\OpenApi\Responses\PDC\FailureResponse::class, statusCode: 400)]
#[OpenApi\Response(factory: \App\OpenApi\Responses\PDC\SuccessResponse::class, statusCode: 200)]
public function generate(Request $request)
{
// Define validation rules for the request data
$rules = [
'callsign' => 'required|string',
'departure' => 'required|string',
'arrival' => 'required|string',
'route' => 'required|string',
'aircraft' => 'required|string',
'altitude' => 'required|string',
'squawk' => 'required|string',
'departure_time' => 'required|date_format:Y-m-d\TH:i:s\Z',
'remarks' => 'nullable|string',
];

// Validate the request data
$validator = Validator::make($request->all(), $rules);

// Check if validation fails
if ($validator->fails()) {
return Helpers::response('Validation failed', ['error' => $validator->errors()], 400);
}

// Get request data
$callsign = $request->input('callsign');
$departure = $request->input('departure');
$arrival = $request->input('arrival');
$route = $request->input('route');
$aircraft = $request->input('aircraft');
$altitude = $request->input('altitude');
$squawk = $request->input('squawk');
$departure_time = $request->input('departure_time');
$remarks = $request->input('remarks');

// Build PDC
$pdc = "PDC FOR $callsign\n";
$pdc .= "DEPARTING $departure\n";
$pdc .= "ARRIVING $arrival\n";
$pdc .= "VIA $route\n";
$pdc .= "AIRCRAFT $aircraft\n";
$pdc .= "ALTITUDE $altitude\n";
$pdc .= "SQUAWK $squawk\n";
$pdc .= "DEPARTURE TIME $departure_time\n";
$pdc .= "REMARKS $remarks\n";
$pdc .= "END PDC";

return Helpers::response('PDC generated successfully', ['pdc' => $pdc], 200);
}
}
51 changes: 51 additions & 0 deletions src/app/OpenApi/RequestBodies/PDC/GenerateRequestBody.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
<?php

namespace App\OpenApi\RequestBodies\PDC;

use GoldSpecDigital\ObjectOrientedOAS\Objects\MediaType;
use GoldSpecDigital\ObjectOrientedOAS\Objects\RequestBody;
use GoldSpecDigital\ObjectOrientedOAS\Objects\Schema;
use Vyuldashev\LaravelOpenApi\Factories\RequestBodyFactory;

class GenerateRequestBody extends RequestBodyFactory
{
public function build(): RequestBody
{
// Define the schema for the request body
$response = Schema::object()->properties(
Schema::string('callsign')
->example('AAL123')
->description('The callsign of the flight.'),
Schema::string('departure')
->example('KJAX')
->description('The departure airport ICAO code.'),
Schema::string('arrival')
->example('KDFW')
->description('The arrival airport ICAO code.'),
Schema::string('route')
->example('JAX5 J14 MEI J17 MEM J6 PLESS J180 CYN')
->description('The route of the flight.'),
Schema::string('aircraft')
->example('B737')
->description('The aircraft type.'),
Schema::string('altitude')
->example('FL350')
->description('The altitude of the flight.'),
Schema::string('squawk')
->example('1234')
->description('The squawk code of the flight.'),
Schema::string('departure_time')
->example('2023-09-15T14:00:00Z')
->description('The departure time of the flight in ISO 8601 format.'),
Schema::string('remarks')
->example('VIP onboard, special meal requests')
->description('Any remarks for the flight.'),
);

return RequestBody::create('GeneratePDC')
->description('Generate a Pre-Departure Clearance (PDC) for a flight.')
->content(
MediaType::json()->schema($response)
);
}
}
62 changes: 62 additions & 0 deletions src/app/OpenApi/Responses/PDC/FailureResponse.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
<?php

namespace App\OpenApi\Responses\PDC;

use GoldSpecDigital\ObjectOrientedOAS\Objects\MediaType;
use GoldSpecDigital\ObjectOrientedOAS\Objects\Response;
use GoldSpecDigital\ObjectOrientedOAS\Objects\Schema;
use Vyuldashev\LaravelOpenApi\Factories\ResponseFactory;
use Vyuldashev\LaravelOpenApi\Contracts\Reusable;

class FailureResponse extends ResponseFactory implements Reusable
{
public function build(): Response
{
$response = Schema::object()->properties(
Schema::string('status')
->example('error')
->enum('success', 'error')
->description('The status of the response indicating the success or failure of the operation.'),
Schema::string('message')
->example('Validation failed')
->description('A human-readable message providing additional information about the status.'),
Schema::integer('code')
->example(200)
->description('An optional status code for the response. It typically indicates the HTTP status code.'),
Schema::object('data')->properties(
Schema::object('error')->properties(
Schema::string('callsign')
->example('The callsign field is required.')
->description('The error message for the callsign field.'),
Schema::string('departure')
->example('The departure field is required.')
->description('The error message for the departure field.'),
Schema::string('arrival')
->example('The arrival field is required.')
->description('The error message for the arrival field.'),
Schema::string('route')
->example('The route field is required.')
->description('The error message for the route field.'),
Schema::string('aircraft')
->example('The aircraft field is required.')
->description('The error message for the aircraft field.'),
Schema::string('altitude')
->example('The altitude field is required.')
->description('The error message for the altitude field.'),
Schema::string('squawk')
->example('The squawk field is required.')
->description('The error message for the squawk field.'),
Schema::string('departure_time')
->example('The departure time field is required.')
->description('The error message for the departure time field.'),
)
)
);

return Response::create('FailedGeneratePDC')
->description('Failed to generate a Pre-Departure Clearance (PDC) for a flight.')
->content(
MediaType::json()->schema($response)
);
}
}
39 changes: 39 additions & 0 deletions src/app/OpenApi/Responses/PDC/SuccessResponse.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
<?php

namespace App\OpenApi\Responses\PDC;

use GoldSpecDigital\ObjectOrientedOAS\Objects\MediaType;
use GoldSpecDigital\ObjectOrientedOAS\Objects\Response;
use GoldSpecDigital\ObjectOrientedOAS\Objects\Schema;
use Vyuldashev\LaravelOpenApi\Factories\ResponseFactory;
use Vyuldashev\LaravelOpenApi\Contracts\Reusable;

class SuccessResponse extends ResponseFactory implements Reusable
{
public function build(): Response
{
$response = Schema::object()->properties(
Schema::string('status')
->example('success')
->enum('success', 'error')
->description('The status of the response indicating the success or failure of the operation.'),
Schema::string('message')
->example('PDC generated successfully')
->description('A human-readable message providing additional information about the status.'),
Schema::integer('code')
->example(200)
->description('An optional status code for the response. It typically indicates the HTTP status code.'),
Schema::object('data')->properties(
Schema::string('pdc')
->example('PDC FOR AAL123\nDEPARTING KJAX\nARRIVING KDFW\nVIA JAX5 J14 MEI J17 MEM J6 PLESS J180 CYN\nAIRCRAFT B737\nALTITUDE FL350\nSQUAWK 1234\nDEPARTURE TIME 2023-09-15T14:00:00Z\nREMARKS VIP onboard, special meal requests\nEND PDC')
->description('The generated PDC.'),
)
);

return Response::create('GeneratePDC')
->description('Generate a Pre-Departure Clearance (PDC) for a flight.')
->content(
MediaType::json()->schema($response)
);
}
}
7 changes: 7 additions & 0 deletions src/routes/api.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
use App\Http\Controllers\API\HealthCheckController;
use App\Http\Controllers\API\AirportController;
use App\Http\Controllers\API\TextToSpeechController;
use App\Http\Controllers\API\PDCController;
use Illuminate\Support\Facades\Route;

/*
Expand Down Expand Up @@ -50,4 +51,10 @@
// Delete atis audio file
Route::delete('/', [TextToSpeechController::class, 'delete']);
});

// PDC Generator
Route::prefix('pdc')->group(function () {
// Generate PDC
Route::post('/', [PDCController::class, 'generate'])->name('api.pdc.generate');
});
});

0 comments on commit e24cc58

Please sign in to comment.