From 4127a15b5202e098af60c87249eba0796351b91a Mon Sep 17 00:00:00 2001 From: JakubKermes Date: Wed, 20 Mar 2024 14:09:19 +0100 Subject: [PATCH] Add responses, small fixes --- .../Api/Admin/ProviderController.php | 9 +++--- app/Http/Controllers/Api/AuthController.php | 19 ++++++------ .../Controllers/Api/CityOpinionController.php | 29 +++++++++++++------ .../Api/CityProviderController.php | 12 ++++++-- .../CityWithoutAssignedCountryController.php | 13 +++++++-- .../Controllers/Api/FavoritesController.php | 22 +++++++------- lang/pl.json | 1 + 7 files changed, 69 insertions(+), 36 deletions(-) diff --git a/app/Http/Controllers/Api/Admin/ProviderController.php b/app/Http/Controllers/Api/Admin/ProviderController.php index ccbb65a6..e9387b43 100644 --- a/app/Http/Controllers/Api/Admin/ProviderController.php +++ b/app/Http/Controllers/Api/Admin/ProviderController.php @@ -11,6 +11,7 @@ use Illuminate\Http\JsonResponse; use Illuminate\Support\Facades\File; use Illuminate\Support\Facades\Storage; +use Symfony\Component\HttpFoundation\File\UploadedFile; class ProviderController extends Controller { @@ -34,7 +35,7 @@ public function store(ProviderRequest $request): void { Provider::query()->create($request->validated()); - $fileName = $this->getFilename($request); + $fileName = $this->getFilename($request->name, $request->file("file")); $fileContents = $request->file("file")->get(); Storage::disk("public")->put("providers/" . $fileName, $fileContents); @@ -44,7 +45,7 @@ public function update(ProviderRequest $request, Provider $provider): void { $provider->update($request->validated()); - $imageName = $this->getFilename($request); + $imageName = $this->getFilename($request->name, $request->file("file")); $storageImagePath = storage_path("app/public/providers/" . $imageName); $resourceImagePath = resource_path("providers/" . $imageName); $imageContents = $request->file("file")->get(); @@ -80,8 +81,8 @@ public function showLogo(string $filename): JsonResponse ]); } - public function getFilename(ProviderRequest $request): string + private function getFilename(string $name, UploadedFile $file): string { - return strtolower($request["name"]) . "." . $request->file("file")->getClientOriginalExtension(); + return strtolower($name) . "." . $file->getClientOriginalExtension(); } } diff --git a/app/Http/Controllers/Api/AuthController.php b/app/Http/Controllers/Api/AuthController.php index ca71b1a2..005b287d 100644 --- a/app/Http/Controllers/Api/AuthController.php +++ b/app/Http/Controllers/Api/AuthController.php @@ -9,6 +9,7 @@ use App\Http\Requests\RegisterRequest; use App\Models\User; use Illuminate\Http\JsonResponse; +use Illuminate\Http\Request; use Illuminate\Support\Facades\Auth; use Illuminate\Support\Facades\Hash; use Illuminate\Support\Str; @@ -39,13 +40,13 @@ public function login(LoginRequest $request): JsonResponse "password" => $request->password, ], $remember)) { $user = Auth::user(); - $user_id = Auth::id(); + $user_id = (string)Auth::id(); $token_abilities = []; if ($user->isAdmin()) { $token_abilities = ["HasAdminRole"]; } - $token = $user->createToken($user_id . "-AuthToken", $token_abilities)->plainTextToken; + $token = $user->createToken($user_id, $token_abilities)->plainTextToken; return response()->json([ $token_abilities, @@ -58,9 +59,9 @@ public function login(LoginRequest $request): JsonResponse ], Response::HTTP_UNAUTHORIZED); } - public function logout(): JsonResponse + public function logout(Request $request): JsonResponse { - \auth()->user()->tokens()->delete(); + $request->user()->currentAccessToken()->delete(); return response()->json([ "message" => __("Logged out."), @@ -71,7 +72,7 @@ public function redirectToProvider(string $provider): JsonResponse { $redirect_url = Socialite::driver($provider)->stateless()->redirect()->getTargetUrl(); - return \response()->json([ + return response()->json([ "redirect_url" => $redirect_url, ]); } @@ -92,14 +93,14 @@ public function handleProviderRedirect(string $provider): JsonResponse if ($user->hasRole("admin")) { $token_abilities = ["HasAdminRole"]; } - $user_id = $user->id; - $token = $user->createToken($user_id . "-Socialite-AuthToken", $token_abilities)->plainTextToken; + $user_id = $user->id->toString(); + $token = $user->createToken($user_id, $token_abilities)->plainTextToken; - return JsonResponse::create([ + return response()->json([ "access_token" => $token, ]); } catch (\Exception $e) { - return JsonResponse::create([ + return response()->json([ "message" => $e->getMessage(), ]); } diff --git a/app/Http/Controllers/Api/CityOpinionController.php b/app/Http/Controllers/Api/CityOpinionController.php index d10ffd5c..5ca0da58 100644 --- a/app/Http/Controllers/Api/CityOpinionController.php +++ b/app/Http/Controllers/Api/CityOpinionController.php @@ -7,28 +7,39 @@ use App\Http\Controllers\Controller; use App\Http\Requests\CityOpinionRequest; use App\Models\CityOpinion; -use Illuminate\Support\Facades\Auth; +use Illuminate\Http\JsonResponse; class CityOpinionController extends Controller { - public function store(CityOpinionRequest $request): void + public function store(CityOpinionRequest $request): JsonResponse { - $opinion = $request->only(["rating", "content", "city_id"]); - $opinion["user_id"] = Auth::id(); - + $opinion = $request->validated(); + $opinion["user_id"] = $request->user()->id; CityOpinion::query()->create($opinion); + + return response()->json([ + "message" => __("Opinion added successfully."), + ]); } - public function update(CityOpinionRequest $request, CityOpinion $cityOpinion): void + public function update(CityOpinionRequest $request, CityOpinion $cityOpinion): JsonResponse { - $opinion = $request->only(["rating", "content", "city_id"]); - $opinion["user_id"] = Auth::id(); + $opinion = $request->validated(); + $opinion["user_id"] = $request->user()->id; $cityOpinion->update($opinion); + + return response()->json([ + "message" => __("Opinion edited successfully."), + ]); } - public function destroy(CityOpinion $cityOpinion): void + public function destroy(CityOpinion $cityOpinion): JsonResponse { $cityOpinion->delete(); + + return response()->json([ + "message" => __("Opinion removed successfully!"), + ]); } } diff --git a/app/Http/Controllers/Api/CityProviderController.php b/app/Http/Controllers/Api/CityProviderController.php index d55f4ffb..bba7d543 100644 --- a/app/Http/Controllers/Api/CityProviderController.php +++ b/app/Http/Controllers/Api/CityProviderController.php @@ -46,13 +46,21 @@ public function index(): JsonResponse ]); } - public function update(CityProviderService $service, CityProviderRequest $request, City $city): void + public function update(CityProviderService $service, CityProviderRequest $request, City $city): JsonResponse { $service->updateProvider($request->providerNames, $city); + + return response()->json([ + "message" => __("City providers updated successfully."), + ]); } - public function runImporters(DataImporterService $service): void + public function runImporters(DataImporterService $service): JsonResponse { $service->run(); + + return response()->json([ + "message" => __("Importers started."), + ]); } } diff --git a/app/Http/Controllers/Api/CityWithoutAssignedCountryController.php b/app/Http/Controllers/Api/CityWithoutAssignedCountryController.php index 775da0f4..a3068ecd 100644 --- a/app/Http/Controllers/Api/CityWithoutAssignedCountryController.php +++ b/app/Http/Controllers/Api/CityWithoutAssignedCountryController.php @@ -6,16 +6,25 @@ use App\Http\Controllers\Controller; use App\Models\CityWithoutAssignedCountry; +use Illuminate\Http\JsonResponse; class CityWithoutAssignedCountryController extends Controller { - public function destroy(CityWithoutAssignedCountry $city): void + public function destroy(CityWithoutAssignedCountry $city): JsonResponse { $city->delete(); + + return response()->json([ + "message" => __("City removed successfully!"), + ]); } - public function destroyAll(): void + public function destroyAll(): JsonResponse { CityWithoutAssignedCountry::query()->delete(); + + return response()->json([ + "message" => __("All cities removed successfully!"), + ]); } } diff --git a/app/Http/Controllers/Api/FavoritesController.php b/app/Http/Controllers/Api/FavoritesController.php index 61d786f9..9b9a9bc1 100644 --- a/app/Http/Controllers/Api/FavoritesController.php +++ b/app/Http/Controllers/Api/FavoritesController.php @@ -11,14 +11,12 @@ use App\Models\Provider; use Illuminate\Http\JsonResponse; use Illuminate\Http\Request; -use Illuminate\Session\Store as Session; -use Illuminate\Support\Facades\Auth; class FavoritesController extends Controller { - public function index(): JsonResponse + public function index(Request $request): JsonResponse { - $user = Auth::user(); + $user = $request->user(); $favoriteCities = $user->favorites()->with(["city.country", "city.cityProviders"])->get(); @@ -32,7 +30,7 @@ public function index(): JsonResponse ]); } - public function store(Request $request, Session $session): void + public function store(Request $request): JsonResponse { $cityId = $request->input("city_id"); $userId = $request->user()?->id; @@ -42,16 +40,20 @@ public function store(Request $request, Session $session): void ); if ($favorite->wasRecentlyCreated) { - $session->flash("message", "City added to favorites."); - } else { - $favorite->delete(); - $session->flash("message", "City removed from favorites."); + return response()->json([ + "message" => "City added to favorites.", + ]); } + $favorite->delete(); + + return response()->json([ + "message" => "City removed from favorites.", + ]); } public function check(Request $request, int $cityId): bool { - $userId = $request->user()?->id; + $userId = $request->user()->id; return Favorites::where("user_id", $userId) ->where("city_id", $cityId) diff --git a/lang/pl.json b/lang/pl.json index 0910749d..6d91b8c6 100644 --- a/lang/pl.json +++ b/lang/pl.json @@ -141,6 +141,7 @@ "Cities with no country assigned": "Miasta bez przypisanego kraju", "Cities with no coordinates assigned": "Miasta bez przypisanych współrzędnych", "Delete all cities with no country assigned": "Usuń wszystkie miasta bez przypisanego kraju", + "All cities removed successfully!" : "Wszystkie miasta usunięte pomyślnie!", "Opinion added successfully.": "Opinia dodana pomyślnie.", "There was an error adding your opinion.": "Wystąpił błąd podczas dodawania opinii.", "Opinion edited successfully.": "Opinia zaktualizowana pomyślnie.",