Skip to content

Commit

Permalink
🔧 fix(TextToSpeechController.php): import the Debug class from Symfon…
Browse files Browse the repository at this point in the history
…y\Component\ErrorHandler to fix the missing import error

✨ feat(TextToSpeechController.php): add support for deleting ATIS audio files by implementing the delete method in the TextToSpeechController. The method takes in the ID of the ATIS audio file and an optional password for authentication. It performs the following steps:
- Validates the request and checks if the ID is provided
- Checks if the ATIS audio file exists
- Checks if the file requires a password and verifies the password if provided
- Deletes the file from the server
- Deletes the database entry for the file
- Returns a JSON response indicating the success or failure of the deletion operation

📝 chore(DeleteParameters.php): create a new class DeleteParameters in the OpenApi\Parameters\TTS namespace to define the query parameters for the delete operation. The parameters include 'id' (the ID of the ATIS audio file) and 'password' (the password for the file if it is protected).

📝 chore(ErrorMissingIdResponse.php): create a new class ErrorMissingIdResponse in the OpenApi\Responses\TTS\Delete namespace to define the response schema for the case when the ID parameter is missing. The response includes 'status', 'message', 'code', and 'data' properties.

📝 chore(ErrorNotFoundResponse.php): create a new class ErrorNotFoundResponse in the OpenApi\Responses\TTS\Delete namespace to define the response schema for the case when the ATIS audio file is not found. The response includes 'status', 'message', 'code', and 'data' properties.

📝 chore(ErrorPasswordProtectedResponse.php): create a new class ErrorPasswordProtectedResponse in the OpenApi\Responses\TTS\Delete namespace to define the response schema for the case when the ATIS audio file is password protected and the provided password is incorrect or missing. The response includes 'status', 'message', 'code', and 'data' properties.

📝 chore(SuccessResponse.php): create a new class SuccessResponse in the OpenApi\Responses\TTS\Delete namespace to define the response schema for the case when the ATIS audio file is successfully deleted. The response includes 'status', 'message', 'code', and 'data' properties.
  • Loading branch information
Vahn Gomes authored and Vahn Gomes committed Jul 21, 2023
1 parent d31c99a commit f319262
Show file tree
Hide file tree
Showing 6 changed files with 201 additions and 4 deletions.
65 changes: 61 additions & 4 deletions src/app/Http/Controllers/API/TextToSpeechController.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Storage;
use Symfony\Component\ErrorHandler\Debug;
use Vyuldashev\LaravelOpenApi\Attributes as OpenApi;

#[OpenApi\PathItem]
Expand Down Expand Up @@ -246,12 +247,68 @@ public function generate(Request $request): JsonResponse
*
* @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(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::disk('local')->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
]);
}
}
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()),

];
}
}
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)
);
}
}
26 changes: 26 additions & 0 deletions src/app/OpenApi/Responses/TTS/Delete/SuccessResponse.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?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 SuccessResponse extends ResponseFactory implements Reusable
{
public function build(): Response
{
$response = Schema::object()->properties(
Schema::string('status')->example('success'),
Schema::string('message')->example('ATIS audio file deleted successfully.'),
Schema::integer('code')->example(200),
Schema::object('data')->nullable()
);

return Response::create('DeleteTTSSuccess')
->description('Success response')
->content(MediaType::json()->schema($response));
}
}

0 comments on commit f319262

Please sign in to comment.