Skip to content

Commit

Permalink
Merge pull request #30 from Codycody31/main
Browse files Browse the repository at this point in the history
Update build tag and introduce ATIS audio file functionality
  • Loading branch information
Vahn Gomes authored Jul 21, 2023
2 parents 2557430 + bd3edde commit 4f15e2f
Show file tree
Hide file tree
Showing 15 changed files with 477 additions and 72 deletions.
2 changes: 1 addition & 1 deletion .woodpecker.yml
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ pipeline:
repo: insidiousfiddler/redbeards-atis-generator
dockerfile: Dockerfile
platforms: *platforms
tag: [latest, "${CI_COMMIT_TAG#v}"]
tag: [latest, "${CI_COMMIT_TAG}"]
when:
event: tag

Expand Down
133 changes: 118 additions & 15 deletions src/app/Http/Controllers/API/TextToSpeechController.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,16 @@
use App\Http\Controllers\Controller;
use App\Models\ATISAudioFile;
use App\OpenApi\Parameters\GetAirportParameters;
use App\OpenApi\Parameters\GetTextToSpeechParameters;
use App\OpenApi\RequestBodies\TTS\GenerateRequestBody;
use App\OpenApi\RequestBodies\TTS\GetTextToSpeechRequestBody;
use App\OpenApi\Responses\TTS\ErrorGeneratingResponse;
use App\OpenApi\Responses\TTS\ErrorRequestConflictResponse;
use App\OpenApi\Responses\TTS\ErrorValidatingIcaoResponse;
use App\OpenApi\Responses\TTS\ErrorWithVoiceAPIResponse;
use App\OpenApi\Responses\TTS\SuccessResponse;
use App\OpenApi\Responses\TTS\GetTextToSpeech\ErrorGetTextToSpeechResponse;
use App\OpenApi\Responses\TTS\GetTextToSpeech\SuccessResponse as GetTextToSpeechSuccessResponse;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Storage;
Expand All @@ -25,23 +29,65 @@ class TextToSpeechController extends Controller
*
* Gets a link to a mp3 text-to-speech file for an airport and returns it in a JSON response.
*
* @param string $icao The ICAO code of the airport to generate the TTS for.
* @param Request $request
* @return void
* @return JsonResponse
*/
#[OpenApi\Operation(tags: ['Text to Speech'])]
#[OpenApi\Parameters(factory: GetAirportParameters::class)]
public function index(string $icao, Request $request): void
#[OpenApi\Parameters(factory: GetTextToSpeechParameters::class)]
#[OpenApi\Response(factory: ErrorValidatingIcaoResponse::class, statusCode: 400)]
#[OpenApi\Response(factory: ErrorGetTextToSpeechResponse::class, statusCode: 404)]
#[OpenApi\Response(factory: GetTextToSpeechSuccessResponse::class, statusCode: 200)]
public function index(Request $request): JsonResponse
{
// TODO: Get mp3 atis file and return link and id.
// Get the request parameters
$id = $request->id;
$icao = $request->icao;

// Validate the request
if (
!isset($icao) ||
!Helpers::validateIcao($icao)
) {
return response()->json([
'status' => 'error',
'message' => 'Invalid ICAO code.',
'code' => 400,
'data' => null
]);
}

// Get the ATIS audio file
$atis_file = ATISAudioFile::where('icao', $icao)->where('id', $id)->first();

// Check if the ATIS audio file exists
if ($atis_file == null || !$atis_file->exists()) {
return response()->json([
'status' => 'error',
'message' => 'ATIS audio file not found.',
'code' => 404,
'data' => null
]);
}

// Return the response
return response()->json([
'status' => 'success',
'message' => 'ATIS audio file found.',
'code' => 200,
'data' => [
'id' => $atis_file->id,
'name' => $atis_file->file_name,
'url' => $atis_file->url,
'expires_at' => $atis_file->expires_at,
]
]);
}

/**
* Generate Airport TTS.
*
* Generates a mp3 text-to-speech file for an airport and returns a link to it.
*
* @param string $icao The ICAO code of the airport to generate the TTS for.
* @param Request $request
* @return JsonResponse
*/
Expand All @@ -53,8 +99,13 @@ public function index(string $icao, Request $request): void
#[OpenApi\Response(factory: ErrorWithVoiceAPIResponse::class, statusCode: 500)]
#[OpenApi\Response(factory: ErrorGeneratingResponse::class, statusCode: 422)]
#[OpenApi\Response(factory: SuccessResponse::class, statusCode: 200)]
public function generate(string $icao, Request $request): JsonResponse
public function generate(Request $request): JsonResponse
{
// Get the request parameters
$icao = $request->icao;
$atis = $request->atis;
$ident = $request->ident;

// Validate the request
if (!Helpers::validateIcao($icao)) {
return response()->json([
Expand All @@ -65,9 +116,6 @@ public function generate(string $icao, Request $request): JsonResponse
]);
}

$atis = $request->atis;
$ident = $request->ident;

// Check if the request has the required parameters, not using request()->validate() for now.
if (!isset($atis) || !isset($ident) || !isset($icao)) {
return response()->json([
Expand Down Expand Up @@ -195,14 +243,69 @@ public function generate(string $icao, Request $request): JsonResponse
*
* Deletes a mp3 text-to-speech file for an airport.
*
* @param string $icao The ICAO code of the airport to generate the TTS for.
* @param Request $request
* @return void
* @return JsonResponse
*/
#[OpenApi\Operation(tags: ['Text to Speech'])]
#[OpenApi\Parameters(factory: GetAirportParameters::class)]
public function delete(string $icao, Request $request): void
#[OpenApi\Parameters(factory: \App\OpenApi\Parameters\TTS\DeleteParameters::class)]
#[OpenApi\Response(factory: \App\OpenApi\Responses\TTS\Delete\ErrorMissingIdResponse::class, statusCode: 400)]
#[OpenApi\Response(factory: \App\OpenApi\Responses\TTS\Delete\ErrorNotFoundResponse::class, statusCode: 404)]
#[OpenApi\Response(factory: \App\OpenApi\Responses\TTS\Delete\ErrorPasswordProtectedResponse::class, statusCode: 401)]
#[OpenApi\Response(factory: \App\OpenApi\Responses\TTS\Delete\SuccessResponse::class, statusCode: 200)]
public function delete(Request $request): JsonResponse
{
// TODO: Delete mp3 atis file.
// Get the request parameters
$id = isset($request->id) ? $request->id : null;
$password = isset($request->password) ? $request->password : null;

// Validate the request
if (!isset($id)) {
return response()->json([
'status' => 'error',
'message' => 'You must provide an ATIS ID.',
'code' => 400,
'data' => null
]);
}

// Check if the ATIS audio file exists
$atis_file = ATISAudioFile::where('id', $id)->first();

// Check if the ATIS audio file exists
if ($atis_file == null || !$atis_file->exists()) {
return response()->json([
'status' => 'error',
'message' => 'ATIS audio file not found.',
'code' => 404,
'data' => null
]);
}

// Check if the file requires a password to delete
if ($atis_file->password != null) {
// Check if the password is correct
if (!isset($password) || $password != $atis_file->password) {
return response()->json([
'status' => 'error',
'message' => 'Incorrect password.',
'code' => 401,
'data' => null
]);
}
}

// Delete the file from the server
Storage::delete('public/atis/' . $id . '/' . $atis_file->file_name);

// Delete the database entry
$atis_file->delete();

// Return the response
return response()->json([
'status' => 'success',
'message' => 'ATIS audio file deleted successfully.',
'code' => 200,
'data' => null
]);
}
}
33 changes: 33 additions & 0 deletions src/app/OpenApi/Parameters/GetTextToSpeechParameters.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?php

namespace App\OpenApi\Parameters;

use GoldSpecDigital\ObjectOrientedOAS\Objects\Parameter;
use GoldSpecDigital\ObjectOrientedOAS\Objects\Schema;
use Vyuldashev\LaravelOpenApi\Factories\ParametersFactory;

class GetTextToSpeechParameters extends ParametersFactory
{
/**
* @return Parameter[]
*/
public function build(): array
{
return [

Parameter::query()
->name('icao')
->description('ICAO code of the airport')
->required(true)
->schema(Schema::string())
->example('KJAX'),
Parameter::query()
->name('id')
->description('ID of the ATIS audio file')
->required(true)
->schema(Schema::string())
->example('1'),

];
}
}
30 changes: 30 additions & 0 deletions src/app/OpenApi/Parameters/TTS/DeleteParameters.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<?php

namespace App\OpenApi\Parameters\TTS;

use GoldSpecDigital\ObjectOrientedOAS\Objects\Parameter;
use GoldSpecDigital\ObjectOrientedOAS\Objects\Schema;
use Vyuldashev\LaravelOpenApi\Factories\ParametersFactory;

class DeleteParameters extends ParametersFactory
{
/**
* @return Parameter[]
*/
public function build(): array
{
return [

Parameter::query()
->name('id')
->description('The ID of the ATIS audio file.')
->required()
->schema(Schema::string()),
Parameter::query()
->name('password')
->description('The password for the ATIS audio file if it is protected.')
->schema(Schema::string()),

];
}
}
31 changes: 31 additions & 0 deletions src/app/OpenApi/RequestBodies/TTS/GetTextToSpeechRequestBody.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?php

namespace App\OpenApi\RequestBodies\TTS;

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

class GetTextToSpeechRequestBody extends RequestBodyFactory
{
public function build(): RequestBody
{
$response = Schema::object()->properties(
Schema::string('id')
->example('1')
->description('The ID of the ATIS audio file.')
->required(),
Schema::string('icao')
->example('KJAX')
->description('The ICAO code of the airport.')
->required(),
);

return RequestBody::create('GetTextToSpeech')
->description('Get TTS file for an airport.')
->content(
MediaType::json()->schema($response)
);
}
}
28 changes: 28 additions & 0 deletions src/app/OpenApi/Responses/TTS/Delete/ErrorMissingIdResponse.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php

namespace App\OpenApi\Responses\TTS\Delete;

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 ErrorMissingIdResponse extends ResponseFactory implements Reusable
{
public function build(): Response
{
$response = Schema::object()->properties(
Schema::string('status')->example('error'),
Schema::string('message')->example('You must provide an ATIS ID.'),
Schema::integer('code')->example(400),
Schema::object('data')->nullable()
);

return Response::create('TTSErrorMissingId')
->description('Missing ATIS ID')
->content(
MediaType::json()->schema($response)
);
}
}
28 changes: 28 additions & 0 deletions src/app/OpenApi/Responses/TTS/Delete/ErrorNotFoundResponse.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php

namespace App\OpenApi\Responses\TTS\Delete;

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 ErrorNotFoundResponse extends ResponseFactory implements Reusable
{
public function build(): Response
{
$response = Schema::object()->properties(
Schema::string('status')->example('error'),
Schema::string('message')->example('ATIS audio file not found.'),
Schema::integer('code')->example(404),
Schema::object('data')->nullable()
);

return Response::create('TTSErrorNotFound')
->description('Cannot find the ATIS audio file.')
->content(
MediaType::json()->schema($response)
);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php

namespace App\OpenApi\Responses\TTS\Delete;

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 ErrorPasswordProtectedResponse extends ResponseFactory implements Reusable
{
public function build(): Response
{
$response = Schema::object()->properties(
Schema::string('status')->example('error'),
Schema::string('message')->example('Password is missing or incorrect.'),
Schema::integer('code')->example(401),
Schema::object('data')->nullable()
);

return Response::create('ErrorPasswordProtectedResponse')
->description('Password is missing or incorrect.')
->content(
MediaType::json()->schema($response)
);
}
}
Loading

0 comments on commit 4f15e2f

Please sign in to comment.