-
Notifications
You must be signed in to change notification settings - Fork 1
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
New Proposal for FAQ Multilingual request and response #94 #109
Conversation
…ng-backend into feature/LanguageSwitcher
…ng-backend into feature/LanguageSwitcher
Delete setApiLocales
Quality Gate failedFailed conditions 22.2% Duplication on New Code (required ≤ 3%) See analysis details on SonarCloud Catch issues before they fail your Quality Gate with our IDE extension SonarLint |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
the App model namespace is duplicated
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Resolved
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Excellent work, @FranEnLaNube To reduce the amount of 'return' in your functions, I recommend taking advantage of the various error handling classes that Laravel offers. Instead of multiple 'return', you can use different classes like Response, JsonResponse and other built-in methods to handle and respond to specific errors in a more structured way. This simplifies the flow of your code and makes it easier to maintain.
- Using HttpResponseException to return an HTTP response in case of an error:
throw new HttpResponseException(response()->json(['error' => 'Something went wrong'], 400));
- Using ValidationException to handle validation errors:
throw ValidationException::withMessages(['field' => 'Error validating field']);
- Using ModelNotFoundException to handle exceptions when a model is not found:
throw new ModelNotFoundException('The requested model was not found');
Tests: the multilanguage tests do not pass, I don't know if you have the settings for the tests to pass locally.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
the trycatch block lacks a catch or finally, then there are 2 returns in a row, only the first return will be executed, the second one will be ignored
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Oh! that's true! I didn't realize that! Cause I didn't touch those files. The changes come from the freaking pint ...
I think this branch needs a biiig analysis...
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
yes, that branch would have to pull from the develop branch or create a new branch from the develop branch and paste your changes. Although now I have noticed that it has no conflicts
I've created a new PR, #110 , so I'll close this. |
Description
Changes
Swagger annotations
Following the instructions from #64 of @GabrielaMaureira , I changed the comments in dummy functions in the folder
App\Annotations\OpenApi
as implemented by @androsrivas in #91php artisan l5-swagger:generate
as is said in the documentationapp\Annotations\OpenApi\AnnotationsInfo.php
@OA\Server
. It's also possible to add a second one.app\Annotations\OpenApi\AnnotationsTags.php
controllersAnnotations\faqsAnnotations\AnnotationsFaqs.php
show()
with two endpoints. This is possible by adding two @OA Get statements for the same dummy methodshow()
in this case./faqs/{id}
to show the JSON with all the available translations./faqs/{id}?language
to show the FAQ already translated in a specific language if available.app\Annotations\OpenApi\controllersAnnotations\usersAnnotations\AnnotationsUsers.php
app\Annotations\OpenApi\modelsAnnotations\faqAnnotations\AnnotationsFaq.php
AnnotationsFaq {}
.AnnotationsFaqTranslation {}
to have the FAQ translations fields.AnnotationsFaqStored {}
to have the Schema for when a FAQ is stored.faqController.php
index()
show()
use Astrotomic\Translatable\Validation\RuleFactory;
use \Astrotomic\Translatable\Locales;use \Astrotomic\Translatable\Locales;
.store()
RuleFactory
we are able to send one like this.RuleFactory
is used to validate all the language inputs in one shot, we don't need it any more.$dataWithLocaleKey = [$language => $validatedData];
And using the result array as input in the
$faq = Faq::create($dataWithLocaleKey)->setDefaultLocale($locale);
setDefaultLocale()
'title' => ['required','string', 'max:255', 'unique:faq_translations,title'],
update()
create()
method.update()
method in Laravel doesn't update if we leave the field empty, I added the restriction to get an error in that case.RuleFactory
off from here.destroy()
setLocale.php
App::setLocale(config('app.locale'));
inside theelse {}
in L26 it's not necessary because a fallback is configured on theconfig/app.php
file. I don't know if same happens withSession::put('locale', config('app.locale'));
which doesn't have a default value.config\l5-swagger.php
'title' => env('APP_NAME', 'ITA-Landing-API')
.env
it'll take the URL set on it, if neither is set, it'll take LocalHost and concatenate /api to it.'operations_sort' => env('L5_SWAGGER_OPERATIONS_SORT', null),
database\factories\FaqFactory.php
database\seeders\DatabaseSeeder.php
setApiLocale.php
setLocale.php
is used instead.config/app.php
'locale' => 'ca'
, it was in'en'
'fallback_locale' => 'ca'
, Set to Catalan, also was in'en'
'supported_locales' => ['ca', 'es', 'en'],
'faker_locale' => 'ca',
'ca'
.config\translatable.php
'en'
to the available languages (locales).'fallback_locale' => false,
'en'
. Changing it to false make that in case the asked translation is not available it returns anything.'ca'
returns that translation.'locales'
'format' => \Astrotomic\Translatable\Validation\RuleFactory::FORMAT_ARRAY,
resources\lang\ca\api.php
translation_not_found
,translation_key_not_available
,faq_translation_updated
andfaq_translation_deleted
'es'
y'en'
.routes/api.php
use Illuminate\Http\Request;
Route::middleware(['SetLocale'])->group(function () {}
because\App\Http\Middleware\SetLocale::class
, is already set inapp/Http/Kernel.php
, L39:protected $middlewareGroups = []
so it's not necessary to have it in this file.{language?}
to FAQ show, store , update and destroy methods to be able do call a particular language or not depending on the parameter is present or not.->prefix('faqs')
and->prefix('apps')
as it was asked in [BE] Apps Crud #54 #58.To Do, pending tasks:
UserController.php
We should refactor and translate the responses for login.$validatedData
faqTests
, if later we add a user with admin role.GET
method, so it fails, because login needs to be aPOST
one.app\Http\Middleware\Authenticate.php
file.Some more interesting info