Skip to content

Commit

Permalink
Add responses, small fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
JakubKermes committed Mar 20, 2024
1 parent 553c8c3 commit 4127a15
Show file tree
Hide file tree
Showing 7 changed files with 69 additions and 36 deletions.
9 changes: 5 additions & 4 deletions app/Http/Controllers/Api/Admin/ProviderController.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand All @@ -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);
Expand All @@ -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();
Expand Down Expand Up @@ -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();
}
}
19 changes: 10 additions & 9 deletions app/Http/Controllers/Api/AuthController.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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,
Expand All @@ -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."),
Expand All @@ -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,
]);
}
Expand All @@ -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(),
]);
}
Expand Down
29 changes: 20 additions & 9 deletions app/Http/Controllers/Api/CityOpinionController.php
Original file line number Diff line number Diff line change
Expand Up @@ -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!"),
]);
}
}
12 changes: 10 additions & 2 deletions app/Http/Controllers/Api/CityProviderController.php
Original file line number Diff line number Diff line change
Expand Up @@ -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."),
]);
}
}
13 changes: 11 additions & 2 deletions app/Http/Controllers/Api/CityWithoutAssignedCountryController.php
Original file line number Diff line number Diff line change
Expand Up @@ -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!"),
]);
}
}
22 changes: 12 additions & 10 deletions app/Http/Controllers/Api/FavoritesController.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();

Expand All @@ -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;
Expand All @@ -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)
Expand Down
1 change: 1 addition & 0 deletions lang/pl.json
Original file line number Diff line number Diff line change
Expand Up @@ -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.",
Expand Down

0 comments on commit 4127a15

Please sign in to comment.