Skip to content
Snippets Groups Projects
Commit 1c37b8ba authored by jorplaz's avatar jorplaz
Browse files

Merge branch '1-create-entities' into 'develop'

All models created with the migrations and seeders

See merge request !1
parents fe946e78 214bb446
Branches
No related tags found
2 merge requests!3Develop,!1All models created with the migrations and seeders
Showing
with 649 additions and 6 deletions
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
use Illuminate\Database\Eloquent\Relations\HasMany;
/**
* @method static create(array $array)
*/
class Bike extends Model
{
use HasFactory;
protected $fillable = [
'stop_id',
'unlocked',
];
public function stop(): BelongsTo{
return $this->belongsTo(Stop::class);
}
public function routes(): HasMany{
return $this->hasMany(Route::class);
}
}
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
class ElectricBike extends Bike
{
use HasFactory;
protected $table = 'electric_bikes';
protected $fillable = [
'id',
'battery',
];
}
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsToMany;
/**
* @method static create(array $array)
*/
class Reward extends Model
{
use HasFactory;
protected $fillable = [
'title',
'description',
'user_id',
'reward_id',
];
public function users(): BelongsToMany{
return $this->belongsToMany(User::class, 'user_reward')->withTimestamps();
}
}
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
class Route extends Model
{
use HasFactory;
protected $fillable = [
"user_id",
"initial_stop_id",
"final_stop_id",
"bike_id",
"distance",
"duration",
"points",
];
public function user(): BelongsTo{
return $this->belongsTo(Route::class);
}
public function start(): BelongsTo{
return $this->belongsTo(Stop::class,'initial_stop_id');
}
public function end(): BelongsTo{
return $this->belongsTo(Stop::class, "final_stop_id");
}
public function bike(): BelongsTo{
return $this->belongsTo(Bike::class);
}
}
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\HasMany;
/**
* @method static create(array $array)
*/
class Stop extends Model
{
use HasFactory;
protected $fillable = [
"lng",
"lat",
"address",
"total_spaces"
];
public function bikes(): HasMany{
return $this->hasMany(Bike::class);
}
public function routes(): HasMany{
return $this->hasMany(Route::class);
}
public function routesStart(): HasMany{
return $this->hasMany(Route::class, "initial_stop_id");
}
public function routesEnd(): HasMany{
return $this->hasMany(Route::class, "final_stop_id");
}
}
......@@ -4,10 +4,15 @@ namespace App\Models;
use Illuminate\Contracts\Auth\MustVerifyEmail;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Relations\BelongsToMany;
use Illuminate\Database\Eloquent\Relations\HasMany;
use Illuminate\Foundation\Auth\User as Authenticatable;
use Illuminate\Notifications\Notifiable;
use Laravel\Sanctum\HasApiTokens;
/**
* @method static create(string[] $array)
*/
class User extends Authenticatable
{
use HasApiTokens, HasFactory, Notifiable;
......@@ -41,4 +46,12 @@ class User extends Authenticatable
protected $casts = [
'email_verified_at' => 'datetime',
];
public function routes(): HasMany{
return $this->hasMany(Route::class);
}
public function rewards(): BelongsToMany{
return $this->belongsToMany(Reward::class, 'user_reward')->withTimestamps();
}
}
<?php
namespace Database\Factories;
use Illuminate\Database\Eloquent\Factories\Factory;
/**
* @extends \Illuminate\Database\Eloquent\Factories\Factory<\App\Models\Bike>
*/
class BikeFactory extends Factory
{
/**
* Define the model's default state.
*
* @return array<string, mixed>
*/
public function definition()
{
return [
];
}
}
<?php
namespace Database\Factories;
use Illuminate\Database\Eloquent\Factories\Factory;
/**
* @extends \Illuminate\Database\Eloquent\Factories\Factory<\App\Models\Reward>
*/
class RewardFactory extends Factory
{
/**
* Define the model's default state.
*
* @return array<string, mixed>
*/
public function definition()
{
return [
//
];
}
}
<?php
namespace Database\Factories;
use Illuminate\Database\Eloquent\Factories\Factory;
/**
* @extends \Illuminate\Database\Eloquent\Factories\Factory<\App\Models\Stop>
*/
class StopFactory extends Factory
{
/**
* Define the model's default state.
*
* @return array<string, mixed>
*/
public function definition()
{
return [
'lng' => $this->faker->longitude,
'lat' => $this->faker->latitude,
'address' => $this->faker->address,
'total_spaces' => 10,
];
}
}
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
return new class extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('stops', function (Blueprint $table) {
$table->id();
$table->double("lng");
$table->double("lat");
$table->string("address");
$table->integer("total_spaces");
$table->timestamps();
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::dropIfExists('stops');
}
};
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
return new class extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('bikes', function (Blueprint $table) {
$table->id();
$table->foreignId('stop_id')
->index()
->constrained()
->cascadeOnUpdate();
$table->boolean('unlocked');
$table->timestamps();
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::dropIfExists('bikes');
}
};
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
return new class extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('electric_bikes', function (Blueprint $table) {
$table->id();
$table->integer('battery');
$table->timestamps();
$table->foreign('id')->references('id')->on('bikes');
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::dropIfExists('electric_bikes');
}
};
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
return new class extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('routes', function (Blueprint $table) {
$table->id();
$table->foreignId('user_id')
->index()
->constrained()
->cascadeOnUpdate()
->cascadeOnDelete();
$table->bigInteger('initial_stop_id')
->index();
$table->foreign('initial_stop_id')
->references('id')
->on('stops')
->onUpdate('cascade')
->onDelete('cascade');
$table->bigInteger('final_stop_id')
->index()
->nullable();
$table->foreign('final_stop_id')
->references('id')
->on('stops')
->onUpdate('cascade')
->onDelete('cascade');
$table->foreignId('bike_id')
->index()
->constrained()
->cascadeOnUpdate()
->cascadeOnDelete();
$table->integer('distance')
->nullable();
$table->integer('duration')
->nullable();
$table->integer('points')
->nullable();
$table->json('mapbox_response')
->nullable();
$table->timestamps();
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::dropIfExists('routes');
}
};
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
return new class extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('rewards', function (Blueprint $table) {
$table->id();
$table->string('name');
$table->string('description');
$table->integer('points');
$table->integer('total_available');
$table->string('image')->nullable();
$table->timestamps();
});
Schema::create('user_reward', function (Blueprint $table){
$table->id();
$table->foreignId('user_id')
->constrained()
->cascadeOnUpdate()
->cascadeOnDelete();
$table->foreignId('reward_id')
->constrained()
->cascadeOnUpdate()
->cascadeOnDelete();
$table->timestamps();
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::dropIfExists('rewards');
Schema::dropIfExists('user_reward');
}
};
<?php
namespace Database\Seeders;
use App\Models\Bike;
use App\Models\ElectricBike;
use Illuminate\Database\Console\Seeds\WithoutModelEvents;
use Illuminate\Database\Seeder;
class BikeSeeder extends Seeder
{
/**
* Run the database seeds.
*
* @return void
*/
public function run()
{
$bike = Bike::create([
'stop_id' => 1,
'unlocked' => false
]);
ElectricBike::create([
'id' => $bike->id,
'battery' => 60
]);
Bike::create([
'stop_id' => 1,
'unlocked' => false
]);
$bike = Bike::create([
'stop_id' => 2,
'unlocked' => false
]);
ElectricBike::create([
'id' => $bike->id,
'battery' => 70
]);
Bike::create([
'stop_id' => 3,
'unlocked' => false
]);
}
}
......@@ -14,11 +14,10 @@ class DatabaseSeeder extends Seeder
*/
public function run()
{
// \App\Models\User::factory(10)->create();
// \App\Models\User::factory()->create([
// 'name' => 'Test User',
// 'email' => 'test@example.com',
// ]);
$this->call(UserSeeder::class);
$this->call(StopSeeder::class);
$this->call(BikeSeeder::class);
$this->call(RouteSeeder::class);
$this->call(RewardSeeder::class);
}
}
<?php
namespace Database\Seeders;
use Illuminate\Database\Console\Seeds\WithoutModelEvents;
use Illuminate\Database\Seeder;
class ElectricBikeSeeder extends Seeder
{
/**
* Run the database seeds.
*
* @return void
*/
public function run()
{
//
}
}
<?php
namespace Database\Seeders;
use App\Models\Reward;
use Illuminate\Database\Console\Seeds\WithoutModelEvents;
use Illuminate\Database\Seeder;
class RewardSeeder extends Seeder
{
/**
* Run the database seeds.
*
* @return void
*/
public function run()
{
$reward = Reward::create([
'name' => 'Reward 1',
'description' => 'A short description',
'points' => 100,
'total_available' => 5
]);
$reward->users()->sync([1,2]);
$reward = Reward::create([
'name' => 'Reward 2',
'description' => 'A short description 2',
'points' => 200,
'total_available' => 2
]);
$reward->users()->sync([1]);
Reward::create([
'name' => 'Reward 3',
'description' => 'A short description 3',
'points' => 1000,
'total_available' => 1
]);
Reward::create([
'name' => 'Reward 4',
'description' => 'A short description 4',
'points' => 4,
'total_available' => 10
]);
Reward::create([
'name' => 'Reward 5',
'description' => 'A short description 5',
'points' => 500,
'total_available' => 5
]);
}
}
<?php
namespace Database\Seeders;
use App\Models\Route;
use Illuminate\Database\Console\Seeds\WithoutModelEvents;
use Illuminate\Database\Seeder;
class RouteSeeder extends Seeder
{
/**
* Run the database seeds.
*
* @return void
*/
public function run()
{
Route::create([
"user_id" => 1,
"initial_stop_id" => 1,
"final_stop_id" => 2,
"bike_id" => 1,
"distance" => 500,
"duration" => 1000,
"points" => 100,
"mapbox_response" => '{"routes":[{"geometry":{"coordinates":[[-4.730855,41.653319],[-4.731204,41.653354],[-4.731172,41.653568],[-4.729951,41.653454],[-4.725932,41.652181],[-4.726006,41.652131],[-4.726499,41.65228]],"type":"LineString"},"legs":[{"summary":"","weight":328,"duration":298,"steps":[],"distance":571.6}],"weight_name":"cyclability","weight":328,"duration":298,"distance":571.6}],"waypoints":[{"distance":0.33309312638707467,"name":"","location":[-4.730855,41.653319]},{"distance":1.4233601888820147,"name":"Plaza de la Fuente Dorada","location":[-4.726499,41.65228]}],"code":"Ok","uuid":"34oyDWtItbRpJKZb00E5_J2jmiJlWLfQagPHfulbhv5_2m0FkCUA0g=="}'
]);
}
}
<?php
namespace Database\Seeders;
use App\Models\Bike;
use App\Models\Stop;
use Illuminate\Database\Console\Seeds\WithoutModelEvents;
use Illuminate\Database\Seeder;
class StopSeeder extends Seeder
{
/**
* Run the database seeds.
*
* @return void
*/
public function run()
{
Stop::create([
"lng"=> -4.731,
"lat"=> 41.653,
"address"=> "Plaza de Poniente",
"total_spaces"=> 10,
]);
Stop::create([
"lng"=> -4.726,
"lat"=> 41.652,
"address"=> "Plaza de Fuente Dorada",
"total_spaces"=> 10,
]);
Stop::create([
"lng"=> -4.725,
"lat"=> 41.648,
"address"=> "Plaza Madrid",
"total_spaces"=> 10,
]);
Stop::create([
"lng"=> -4.729,
"lat"=> 41.647,
"address"=> "Plaza Zorrilla",
"total_spaces"=> 10,
]);
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please to comment