Skip to content

Commit

Permalink
Create migrations
Browse files Browse the repository at this point in the history
  • Loading branch information
JakubKermes committed Jan 7, 2024
1 parent 7a08f13 commit f6ab32e
Show file tree
Hide file tree
Showing 6 changed files with 158 additions and 82 deletions.
40 changes: 28 additions & 12 deletions app/Http/Controllers/RulesController.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,28 +4,44 @@

namespace App\Http\Controllers;

use App\Http\Resources\CityResource;
use App\Http\Resources\CountryResource;
use App\Http\Resources\ProviderResource;
use App\Models\City;
use App\Models\CityProvider;
use App\Models\Country;
use App\Models\Provider;
use Inertia\Inertia;
use Inertia\Response;

class RulesController
{
public function getRules($city, $country): string
public function getRules(): void
{
$rules = "rules in $city $country";

return $rules;
}

public function index() : Response
public function index(): array
{
return Inertia::render("Rules/Index", [
"country" => $country,
"city" => $city,
]);
$cities = CityResource::collection(
City::with("cityAlternativeNames", "cityProviders", "country")
->has("cityProviders")
->whereHas("cityProviders", function ($query): void {
$query->whereNotNull("latitude")->whereNotNull("longitude");
})
->get()
->sortBy("name")
->sortByDesc(fn(City $city): int => $city->cityProviders->count()),
);

$providers = ProviderResource::collection(Provider::all()->sortBy("name"));
$countries = Country::whereHas("cities.cityProviders")
->with(["cities.cityAlternativeNames", "cities.cityProviders"])
->get()
->sortBy("name");

$countries = CountryResource::collection($countries);

return [
"cities" => $cities,
"providers" => $providers,
"countries" => $countries,
];
}
}
66 changes: 0 additions & 66 deletions app/Http/RulesImporter.php

This file was deleted.

31 changes: 31 additions & 0 deletions app/Models/Rules.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?php

declare(strict_types=1);

namespace App\Models;

use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;

class Rules extends Model
{
use HasFactory;

protected $table = "rules_for_cities";
protected $fillable = [
"city_id",
"country_id",
"rulesENG",
"rulesPL",
];

public function city()
{
return $this->belongsTo(City::class);
}

public function country()
{
return $this->belongsTo(Country::class);
}
}
59 changes: 59 additions & 0 deletions app/Services/OpenAIService.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
<?php

declare(strict_types=1);

namespace App\Http;

use App\Models\City;
use Exception;
use OpenAI;

class OpenAIService
{
public function getRules()
{
$responseENG = "Sorry, we couldn't find any rules for this city.";
$responsePL = "Przepraszamy, nie udało nam się znaleźć żadnych zasad dla tego miasta.";

try {
$client = OpenAI::client(env("OPENAI_API_KEY"));

$cities = City::all();
$data = [];

foreach ($cities as $city) {
$data[] = ["city" => $city, "country" => $city->country, "rulesENG" => $responseENG, "rulesPL" => $responsePL];

return $data;
}

foreach ($data as $i) {
$promptENG = "What are the legal limitations for riding electric scooters in " . $i->name . ", " . $i->country->name . "? Contain information about: max speed, helmet requirements, allowed ABV, passengers, other relevant details. Be formal, end with a legal disclaimer.";
$promptPL = "Jakie są prawa dotyczące jazdy na hulajnogach elektrycznych w " . $i["city"] . ", " . $i["country"] . "? Zawrzyj informacje o: maksymalnej prędkości, potrzebie kasku, dozwolonym alkoholu we krwi, pasażerach, inne. Bądź formalny, zakończ oświadczeniem prawnym.";

$responseENG = $client->chat()->create([
"model" => "gpt-3.5-turbo",
"messages" => [
[
"role" => "user",
"content" => $promptENG,
],
],
]);
$responsePL = $client->chat()->create([
"model" => "gpt-3.5-turbo",
"messages" => [
[
"role" => "user",
"content" => $promptPL,
],
],
]);

$i["rulesENG"] = $responseENG["choices"][0]["text"];
$i["rulesPL"] = $responsePL["choices"][0]["text"];
}
} catch (Exception $e) {
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
<?php

declare(strict_types=1);

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

return new class() extends Migration {
public function up(): void
{
Schema::create("rules_for_cities", function (Blueprint $table): void {
$table->id();
$table->timestamps();

$table->unsignedBigInteger("city_id");
$table->foreign("city_id")
->references("id")
->on("cities")
->onDelete("cascade");

$table->unsignedBigInteger("country_id");
$table->foreign("country_id")
->references("id")
->on("countries")
->onDelete("cascade");

$table->string("rulesENG");
$table->string("rulesPL;");
});
}

public function down(): void
{
Schema::dropIfExists("rules_for_cities");
}
};
7 changes: 3 additions & 4 deletions routes/web.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,7 @@
use App\Http\Controllers\CityWithoutAssignedCountryController;
use App\Http\Controllers\DashboardController;
use App\Http\Controllers\FavoritesController;
use App\Http\Controllers\RulesController;
use App\Http\RulesImporter;
use App\Http\OpenAIService;
use Illuminate\Support\Facades\Route;

Route::middleware("guest")->group(function (): void {
Expand Down Expand Up @@ -49,11 +48,11 @@
Route::delete("/delete-city-without-assigned-country/{city}", [CityWithoutAssignedCountryController::class, "destroy"]);
Route::post("/delete-all-cities-without-assigned-country", [CityWithoutAssignedCountryController::class, "destroyAll"]);

Route::get("/importRules", [RulesImporter::class, "importRules"]);
Route::get("/importRules", [OpenAIService::class, "getRules"]);
});
});

Route::get("/rules", [RulesController::class, "index"]);
Route::inertia("/rules", "Rules/Index")->name("rules");

Route::post("/language/{locale}", ChangeLocaleController::class);

Expand Down

0 comments on commit f6ab32e

Please sign in to comment.