From f31926273c14a57225891787d0b6c38d0898d2ab Mon Sep 17 00:00:00 2001 From: Vahn Gomes Date: Fri, 21 Jul 2023 17:00:12 -0400 Subject: [PATCH] =?UTF-8?q?=F0=9F=94=A7=20fix(TextToSpeechController.php):?= =?UTF-8?q?=20import=20the=20Debug=20class=20from=20Symfony\Component\Erro?= =?UTF-8?q?rHandler=20to=20fix=20the=20missing=20import=20error?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit ✨ 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. --- .../API/TextToSpeechController.php | 65 +++++++++++++++++-- .../Parameters/TTS/DeleteParameters.php | 30 +++++++++ .../TTS/Delete/ErrorMissingIdResponse.php | 28 ++++++++ .../TTS/Delete/ErrorNotFoundResponse.php | 28 ++++++++ .../Delete/ErrorPasswordProtectedResponse.php | 28 ++++++++ .../Responses/TTS/Delete/SuccessResponse.php | 26 ++++++++ 6 files changed, 201 insertions(+), 4 deletions(-) create mode 100644 src/app/OpenApi/Parameters/TTS/DeleteParameters.php create mode 100644 src/app/OpenApi/Responses/TTS/Delete/ErrorMissingIdResponse.php create mode 100644 src/app/OpenApi/Responses/TTS/Delete/ErrorNotFoundResponse.php create mode 100644 src/app/OpenApi/Responses/TTS/Delete/ErrorPasswordProtectedResponse.php create mode 100644 src/app/OpenApi/Responses/TTS/Delete/SuccessResponse.php diff --git a/src/app/Http/Controllers/API/TextToSpeechController.php b/src/app/Http/Controllers/API/TextToSpeechController.php index 0a389e5..960f2cf 100644 --- a/src/app/Http/Controllers/API/TextToSpeechController.php +++ b/src/app/Http/Controllers/API/TextToSpeechController.php @@ -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] @@ -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 + ]); } } diff --git a/src/app/OpenApi/Parameters/TTS/DeleteParameters.php b/src/app/OpenApi/Parameters/TTS/DeleteParameters.php new file mode 100644 index 0000000..247ee2c --- /dev/null +++ b/src/app/OpenApi/Parameters/TTS/DeleteParameters.php @@ -0,0 +1,30 @@ +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()), + + ]; + } +} diff --git a/src/app/OpenApi/Responses/TTS/Delete/ErrorMissingIdResponse.php b/src/app/OpenApi/Responses/TTS/Delete/ErrorMissingIdResponse.php new file mode 100644 index 0000000..78a84e5 --- /dev/null +++ b/src/app/OpenApi/Responses/TTS/Delete/ErrorMissingIdResponse.php @@ -0,0 +1,28 @@ +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) + ); + } +} diff --git a/src/app/OpenApi/Responses/TTS/Delete/ErrorNotFoundResponse.php b/src/app/OpenApi/Responses/TTS/Delete/ErrorNotFoundResponse.php new file mode 100644 index 0000000..7b38185 --- /dev/null +++ b/src/app/OpenApi/Responses/TTS/Delete/ErrorNotFoundResponse.php @@ -0,0 +1,28 @@ +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) + ); + } +} diff --git a/src/app/OpenApi/Responses/TTS/Delete/ErrorPasswordProtectedResponse.php b/src/app/OpenApi/Responses/TTS/Delete/ErrorPasswordProtectedResponse.php new file mode 100644 index 0000000..a6101ac --- /dev/null +++ b/src/app/OpenApi/Responses/TTS/Delete/ErrorPasswordProtectedResponse.php @@ -0,0 +1,28 @@ +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) + ); + } +} diff --git a/src/app/OpenApi/Responses/TTS/Delete/SuccessResponse.php b/src/app/OpenApi/Responses/TTS/Delete/SuccessResponse.php new file mode 100644 index 0000000..eca7eec --- /dev/null +++ b/src/app/OpenApi/Responses/TTS/Delete/SuccessResponse.php @@ -0,0 +1,26 @@ +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)); + } +}