Skip to content

Commit

Permalink
Merge branch 'main' into #90-send-e-mail-when-provider-is-added-torem…
Browse files Browse the repository at this point in the history
…oved-from-favorite-city
  • Loading branch information
JakubKermes authored Apr 17, 2024
2 parents 2d7a3a5 + 39e2e60 commit 67874a9
Show file tree
Hide file tree
Showing 32 changed files with 1,055 additions and 28 deletions.
1 change: 1 addition & 0 deletions api.json

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions app/Console/Kernel.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ protected function schedule(Schedule $schedule): void
$service = new DataImporterService();
$service->run("server");
})->monthly();
$schedule->command("sanctum:prune-expired --hours=24")->daily();
}

protected function commands(): void
Expand Down
27 changes: 27 additions & 0 deletions app/Http/Controllers/Api/Admin/CityAlternativeNameController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?php

declare(strict_types=1);

namespace App\Http\Controllers\Api\Admin;

use App\Http\Controllers\Controller;
use App\Http\Requests\CityAlternativeNameRequest;
use App\Models\CityAlternativeName;
use Illuminate\Http\JsonResponse;

class CityAlternativeNameController extends Controller
{
public function store(CityAlternativeNameRequest $request): JsonResponse
{
CityAlternativeName::query()->create($request->validated());

return response()->json(["message" => __("City alternative name created successfully.")], 201);
}

public function destroy(CityAlternativeName $cityAlternativeName): JsonResponse
{
$cityAlternativeName->delete();

return response()->json(["message" => __("City alternative name deleted successfully.")]);
}
}
67 changes: 67 additions & 0 deletions app/Http/Controllers/Api/Admin/CityController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
<?php

declare(strict_types=1);

namespace App\Http\Controllers\Api\Admin;

use App\Http\Controllers\Controller;
use App\Http\Requests\CityRequest;
use App\Http\Resources\CityResource;
use App\Http\Resources\CityWithoutAssignedCountryResource;
use App\Http\Resources\CountryResource;
use App\Http\Resources\ProviderResource;
use App\Models\City;
use App\Models\CityWithoutAssignedCountry;
use App\Models\Country;
use App\Models\Provider;
use Illuminate\Http\JsonResponse;

class CityController extends Controller
{
public function index(): JsonResponse
{
$cities = City::query()
->with("cityAlternativeNames", "cityProviders", "country")
->orderByProvidersCount()
->searchCityNames()
->orderByName()
->orderByCountry()
->orderByTimeRange()
->orderByEmptyCoordinates()
->paginate(15)
->withQueryString();

$providers = Provider::all();
$countries = Country::all();

$citiesWithoutAssignedCountry = CityWithoutAssignedCountry::all();

return response()->json([
"cities" => CityResource::collection($cities),
"providers" => ProviderResource::collection($providers),
"countries" => CountryResource::collection($countries),
"citiesWithoutAssignedCountry" => CityWithoutAssignedCountryResource::collection($citiesWithoutAssignedCountry),
]);
}

public function store(CityRequest $request): JsonResponse
{
City::query()->create($request->validated());

return response()->json(["message" => __("City created successfully.")], 201);
}

public function update(CityRequest $request, City $city): JsonResponse
{
$city->update($request->validated());

return response()->json(["message" => __("City updated successfully.")]);
}

public function destroy(City $city): JsonResponse
{
$city->delete();

return response()->json(["message" => __("City deleted successfully.")]);
}
}
49 changes: 49 additions & 0 deletions app/Http/Controllers/Api/Admin/CountryController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
<?php

declare(strict_types=1);

namespace App\Http\Controllers\Api\Admin;

use App\Http\Controllers\Controller;
use App\Http\Requests\CountryRequest;
use App\Http\Resources\CountryResource;
use App\Models\Country;
use Illuminate\Http\JsonResponse;

class CountryController extends Controller
{
public function index(): JsonResponse
{
$countries = Country::query()
->search("name")
->orderByName()
->orderByTimeRange()
->paginate(15)
->withQueryString();

return response()->json([
"countries" => CountryResource::collection($countries),
]);
}

public function store(CountryRequest $request): JsonResponse
{
Country::query()->create($request->validated());

return response()->json(["message" => __("Country created successfully.")], 201);
}

public function update(CountryRequest $request, Country $country): JsonResponse
{
$country->update($request->validated());

return response()->json(["message" => __("Country updated successfully.")]);
}

public function destroy(Country $country): JsonResponse
{
$country->delete();

return response()->json(["message" => __("Country deleted successfully.")]);
}
}
32 changes: 32 additions & 0 deletions app/Http/Controllers/Api/Admin/ImportInfoController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<?php

declare(strict_types=1);

namespace App\Http\Controllers\Api\Admin;

use App\Http\Controllers\Controller;
use App\Http\Resources\ImportInfoResource;
use App\Models\Code;
use App\Models\ImportInfo;
use App\Models\Provider;
use Illuminate\Http\JsonResponse;

class ImportInfoController extends Controller
{
public function index(): JsonResponse
{
$importInfo = ImportInfo::query()
->with("importInfoDetails")
->orderByDesc("created_at")
->paginate(15)
->withQueryString();
$codes = Code::all();
$providers = Provider::all();

return response()->json([
"importInfo" => ImportInfoResource::collection($importInfo),
"codes" => $codes,
"providers" => $providers,
]);
}
}
94 changes: 94 additions & 0 deletions app/Http/Controllers/Api/Admin/ProviderController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
<?php

declare(strict_types=1);

namespace App\Http\Controllers\Api\Admin;

use App\Http\Controllers\Controller;
use App\Http\Requests\ProviderRequest;
use App\Http\Resources\ProviderResource;
use App\Models\Provider;
use Illuminate\Http\JsonResponse;
use Illuminate\Support\Facades\File;
use Illuminate\Support\Facades\Storage;
use Symfony\Component\HttpFoundation\File\UploadedFile;

class ProviderController extends Controller
{
public const ITEMS_PER_PAGE = 15;

public function index(): JsonResponse
{
$providers = Provider::query()
->search("name")
->orderByName()
->orderByTimeRange()
->paginate(self::ITEMS_PER_PAGE)
->withQueryString();

return response()->json([
"providers" => ProviderResource::collection($providers),
]);
}

public function store(ProviderRequest $request): JsonResponse
{
Provider::query()->create($request->validated());

$fileName = $this->getFilename($request->name, $request->file("file"));
$fileContents = $request->file("file")->get();

Storage::disk("public")->put("providers/" . $fileName, $fileContents);

return response()->json(["message" => __("Provider created successfully.")], 201);
}

public function update(ProviderRequest $request, Provider $provider): JsonResponse
{
$provider->update($request->validated());

$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();

if (file_exists($resourceImagePath)) {
file_put_contents($resourceImagePath, $imageContents);
Storage::put($storageImagePath, file_get_contents($imageContents));
} else {
Storage::put($storageImagePath, file_get_contents($imageContents));
}

return response()->json(["message" => __("Provider updated successfully.")]);
}

public function destroy(Provider $provider): JsonResponse
{
$provider->delete();
$imagePath = storage_path("app/public/providers/" . strtolower($provider["name"]) . ".png");
File::delete($imagePath);

return response()->json(["message" => __("Provider deleted successfully.")]);
}

public function showLogo(string $filename): JsonResponse
{
$imagePath = storage_path("app/public/providers/" . $filename);

if (!file_exists($imagePath)) {
$imagePath = storage_path("app/public/providers/unknown.png");
}

$imageData = base64_encode(file_get_contents($imagePath));

return response()->json([
"image" => $imageData,
"mime_type" => "image/png",
]);
}

private function getFilename(string $name, UploadedFile $file): string
{
return strtolower($name) . "." . $file->getClientOriginalExtension();
}
}
116 changes: 116 additions & 0 deletions app/Http/Controllers/Api/AuthController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,116 @@
<?php

declare(strict_types=1);

namespace App\Http\Controllers\Api;

use App\Http\Controllers\Controller;
use App\Http\Requests\LoginRequest;
use App\Http\Requests\RegisterRequest;
use App\Models\User;
use Exception;
use Illuminate\Contracts\Auth\Authenticatable;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Hash;
use Illuminate\Support\Str;
use Laravel\Socialite\Facades\Socialite;
use Symfony\Component\HttpFoundation\Response;

class AuthController extends Controller
{
public function store(RegisterRequest $request): JsonResponse
{
$user = User::create([
"name" => $request->input("name"),
"email" => $request->input("email"),
"password" => Hash::make($request->input("password")),
]);

return response()->json([
"message" => __("User created."),
]);
}

public function login(LoginRequest $request): JsonResponse
{
$remember = $request->boolean("remember", false);

if (Auth::attempt([
"email" => $request->email,
"password" => $request->password,
], $remember)) {
$user = Auth::user();
$user_id = (string)Auth::id();

$token_abilities = $this->getUserAbilities($user);

$token = $user->createToken($user_id, $token_abilities)->plainTextToken;

return response()->json([
$token_abilities,
"access_token" => $token,
]);
}

return response()->json([
"message" => __("Invalid credentials."),
], Response::HTTP_UNAUTHORIZED);
}

public function logout(Request $request): JsonResponse
{
$request->user()->currentAccessToken()->delete();

return response()->json([
"message" => __("Logged out."),
]);
}

public function redirectToProvider(string $provider): JsonResponse
{
$redirect_url = Socialite::driver($provider)->stateless()->redirect()->getTargetUrl();

return response()->json([
"redirect_url" => $redirect_url,
]);
}

public function handleProviderRedirect(string $provider): JsonResponse
{
try {
$user = Socialite::driver($provider)->user();

$user = User::firstOrCreate([
"email" => $user->getEmail(),
], [
"name" => $user->getName(),
"password" => Hash::make(Str::password(8)),
]);
$token_abilities = $this->getUserAbilities($user);

$user_id = $user->id->toString();
$token = $user->createToken($user_id, $token_abilities)->plainTextToken;

return response()->json([
"access_token" => $token,
]);
} catch (Exception $e) {
return response()->json([
"message" => __("Login failed."),
]);
}
}

private function getUserAbilities(Authenticatable $user): array
{
$abilities = [];

if ($user->isAdmin()) {
$abilities[] = "HasAdminRole";
}

return $abilities;
}
}
Loading

0 comments on commit 67874a9

Please sign in to comment.