From bf6f63604ff978c2cffd3a171cce061f0050b0bf Mon Sep 17 00:00:00 2001 From: "Sander den Hollander :)" Date: Sat, 25 May 2024 12:00:22 +0200 Subject: [PATCH 01/22] Add to providers --- bootstrap/providers.php | 1 + 1 file changed, 1 insertion(+) diff --git a/bootstrap/providers.php b/bootstrap/providers.php index 55e8e15..35e69f9 100644 --- a/bootstrap/providers.php +++ b/bootstrap/providers.php @@ -4,4 +4,5 @@ App\Providers\AppServiceProvider::class, App\Providers\FortifyServiceProvider::class, App\Providers\JetstreamServiceProvider::class, + Spatie\Permission\PermissionServiceProvider::class, ]; From 04c4a54c3f2365b3f120aecceee4f02b16120a4b Mon Sep 17 00:00:00 2001 From: "Sander den Hollander :)" Date: Sat, 25 May 2024 13:46:25 +0200 Subject: [PATCH 02/22] Set up permissions and roles --- app/Console/Commands/GiveRole.php | 43 ++++ app/Console/Commands/SeedRoles.php | 40 ++++ app/Models/User.php | 2 + app/Providers/AppServiceProvider.php | 5 +- config/permission.php | 186 ++++++++++++++++++ ..._05_25_100036_create_permission_tables.php | 138 +++++++++++++ 6 files changed, 413 insertions(+), 1 deletion(-) create mode 100644 app/Console/Commands/GiveRole.php create mode 100644 app/Console/Commands/SeedRoles.php create mode 100644 config/permission.php create mode 100644 database/migrations/2024_05_25_100036_create_permission_tables.php diff --git a/app/Console/Commands/GiveRole.php b/app/Console/Commands/GiveRole.php new file mode 100644 index 0000000..a6c9cc0 --- /dev/null +++ b/app/Console/Commands/GiveRole.php @@ -0,0 +1,43 @@ +ask('Enter the email of the user'); + $role = $this->ask('Enter the role to give to the user'); + $user = \App\Models\User::where('email', $email)->first(); + if ($user) { + $user->assignRole($role); + $this->info('Role given successfully!'); + } else { + $this->error('User not found!'); + } + $loop = $this->confirm('Do you want to give another role to a user?'); + } + $this->info('Bye!'); + } +} diff --git a/app/Console/Commands/SeedRoles.php b/app/Console/Commands/SeedRoles.php new file mode 100644 index 0000000..d18ca23 --- /dev/null +++ b/app/Console/Commands/SeedRoles.php @@ -0,0 +1,40 @@ +forgetCachedPermissions(); + Role::create(['name' => 'admin']); + $user = Role::create(['name' => 'user']); + + $travelRoutes = Permission::create(['name' => 'travel routes']); + $travelRoutes->assignRole($user); + + $this->info('Roles and permissions seeded successfully!'); + } +} diff --git a/app/Models/User.php b/app/Models/User.php index 14709f5..ab45c03 100644 --- a/app/Models/User.php +++ b/app/Models/User.php @@ -10,6 +10,7 @@ use Laravel\Jetstream\HasProfilePhoto; use Laravel\Jetstream\HasTeams; use Laravel\Sanctum\HasApiTokens; +use Spatie\Permission\Traits\HasRoles; class User extends Authenticatable { @@ -19,6 +20,7 @@ class User extends Authenticatable use HasTeams; use Notifiable; use TwoFactorAuthenticatable; + use HasRoles; /** * The attributes that are mass assignable. diff --git a/app/Providers/AppServiceProvider.php b/app/Providers/AppServiceProvider.php index 452e6b6..e5c57f7 100644 --- a/app/Providers/AppServiceProvider.php +++ b/app/Providers/AppServiceProvider.php @@ -2,6 +2,7 @@ namespace App\Providers; +use Illuminate\Support\Facades\Gate; use Illuminate\Support\ServiceProvider; class AppServiceProvider extends ServiceProvider @@ -19,6 +20,8 @@ public function register(): void */ public function boot(): void { - // + Gate::before(function ($user, $ability) { + return $user->hasRole('admin') ? true : null; + }); } } diff --git a/config/permission.php b/config/permission.php new file mode 100644 index 0000000..2a520f3 --- /dev/null +++ b/config/permission.php @@ -0,0 +1,186 @@ + [ + + /* + * When using the "HasPermissions" trait from this package, we need to know which + * Eloquent model should be used to retrieve your permissions. Of course, it + * is often just the "Permission" model but you may use whatever you like. + * + * The model you want to use as a Permission model needs to implement the + * `Spatie\Permission\Contracts\Permission` contract. + */ + + 'permission' => Spatie\Permission\Models\Permission::class, + + /* + * When using the "HasRoles" trait from this package, we need to know which + * Eloquent model should be used to retrieve your roles. Of course, it + * is often just the "Role" model but you may use whatever you like. + * + * The model you want to use as a Role model needs to implement the + * `Spatie\Permission\Contracts\Role` contract. + */ + + 'role' => Spatie\Permission\Models\Role::class, + + ], + + 'table_names' => [ + + /* + * When using the "HasRoles" trait from this package, we need to know which + * table should be used to retrieve your roles. We have chosen a basic + * default value but you may easily change it to any table you like. + */ + + 'roles' => 'roles', + + /* + * When using the "HasPermissions" trait from this package, we need to know which + * table should be used to retrieve your permissions. We have chosen a basic + * default value but you may easily change it to any table you like. + */ + + 'permissions' => 'permissions', + + /* + * When using the "HasPermissions" trait from this package, we need to know which + * table should be used to retrieve your models permissions. We have chosen a + * basic default value but you may easily change it to any table you like. + */ + + 'model_has_permissions' => 'model_has_permissions', + + /* + * When using the "HasRoles" trait from this package, we need to know which + * table should be used to retrieve your models roles. We have chosen a + * basic default value but you may easily change it to any table you like. + */ + + 'model_has_roles' => 'model_has_roles', + + /* + * When using the "HasRoles" trait from this package, we need to know which + * table should be used to retrieve your roles permissions. We have chosen a + * basic default value but you may easily change it to any table you like. + */ + + 'role_has_permissions' => 'role_has_permissions', + ], + + 'column_names' => [ + /* + * Change this if you want to name the related pivots other than defaults + */ + 'role_pivot_key' => null, //default 'role_id', + 'permission_pivot_key' => null, //default 'permission_id', + + /* + * Change this if you want to name the related model primary key other than + * `model_id`. + * + * For example, this would be nice if your primary keys are all UUIDs. In + * that case, name this `model_uuid`. + */ + + 'model_morph_key' => 'model_id', + + /* + * Change this if you want to use the teams feature and your related model's + * foreign key is other than `team_id`. + */ + + 'team_foreign_key' => 'team_id', + ], + + /* + * When set to true, the method for checking permissions will be registered on the gate. + * Set this to false if you want to implement custom logic for checking permissions. + */ + + 'register_permission_check_method' => true, + + /* + * When set to true, Laravel\Octane\Events\OperationTerminated event listener will be registered + * this will refresh permissions on every TickTerminated, TaskTerminated and RequestTerminated + * NOTE: This should not be needed in most cases, but an Octane/Vapor combination benefited from it. + */ + 'register_octane_reset_listener' => false, + + /* + * Teams Feature. + * When set to true the package implements teams using the 'team_foreign_key'. + * If you want the migrations to register the 'team_foreign_key', you must + * set this to true before doing the migration. + * If you already did the migration then you must make a new migration to also + * add 'team_foreign_key' to 'roles', 'model_has_roles', and 'model_has_permissions' + * (view the latest version of this package's migration file) + */ + + 'teams' => false, + + /* + * Passport Client Credentials Grant + * When set to true the package will use Passports Client to check permissions + */ + + 'use_passport_client_credentials' => false, + + /* + * When set to true, the required permission names are added to exception messages. + * This could be considered an information leak in some contexts, so the default + * setting is false here for optimum safety. + */ + + 'display_permission_in_exception' => false, + + /* + * When set to true, the required role names are added to exception messages. + * This could be considered an information leak in some contexts, so the default + * setting is false here for optimum safety. + */ + + 'display_role_in_exception' => false, + + /* + * By default wildcard permission lookups are disabled. + * See documentation to understand supported syntax. + */ + + 'enable_wildcard_permission' => false, + + /* + * The class to use for interpreting wildcard permissions. + * If you need to modify delimiters, override the class and specify its name here. + */ + // 'permission.wildcard_permission' => Spatie\Permission\WildcardPermission::class, + + /* Cache-specific settings */ + + 'cache' => [ + + /* + * By default all permissions are cached for 24 hours to speed up performance. + * When permissions or roles are updated the cache is flushed automatically. + */ + + 'expiration_time' => \DateInterval::createFromDateString('24 hours'), + + /* + * The cache key used to store all permissions. + */ + + 'key' => 'spatie.permission.cache', + + /* + * You may optionally indicate a specific cache driver to use for permission and + * role caching using any of the `store` drivers listed in the cache.php config + * file. Using 'default' here means to use the `default` set in cache.php. + */ + + 'store' => 'default', + ], +]; diff --git a/database/migrations/2024_05_25_100036_create_permission_tables.php b/database/migrations/2024_05_25_100036_create_permission_tables.php new file mode 100644 index 0000000..b865d48 --- /dev/null +++ b/database/migrations/2024_05_25_100036_create_permission_tables.php @@ -0,0 +1,138 @@ +bigIncrements('id'); // permission id + $table->string('name'); // For MySQL 8.0 use string('name', 125); + $table->string('guard_name'); // For MySQL 8.0 use string('guard_name', 125); + $table->timestamps(); + + $table->unique(['name', 'guard_name']); + }); + + Schema::create($tableNames['roles'], function (Blueprint $table) use ($teams, $columnNames) { + $table->bigIncrements('id'); // role id + if ($teams || config('permission.testing')) { // permission.testing is a fix for sqlite testing + $table->unsignedBigInteger($columnNames['team_foreign_key'])->nullable(); + $table->index($columnNames['team_foreign_key'], 'roles_team_foreign_key_index'); + } + $table->string('name'); // For MySQL 8.0 use string('name', 125); + $table->string('guard_name'); // For MySQL 8.0 use string('guard_name', 125); + $table->timestamps(); + if ($teams || config('permission.testing')) { + $table->unique([$columnNames['team_foreign_key'], 'name', 'guard_name']); + } else { + $table->unique(['name', 'guard_name']); + } + }); + + Schema::create($tableNames['model_has_permissions'], function (Blueprint $table) use ($tableNames, $columnNames, $pivotPermission, $teams) { + $table->unsignedBigInteger($pivotPermission); + + $table->string('model_type'); + $table->unsignedBigInteger($columnNames['model_morph_key']); + $table->index([$columnNames['model_morph_key'], 'model_type'], 'model_has_permissions_model_id_model_type_index'); + + $table->foreign($pivotPermission) + ->references('id') // permission id + ->on($tableNames['permissions']) + ->onDelete('cascade'); + if ($teams) { + $table->unsignedBigInteger($columnNames['team_foreign_key']); + $table->index($columnNames['team_foreign_key'], 'model_has_permissions_team_foreign_key_index'); + + $table->primary([$columnNames['team_foreign_key'], $pivotPermission, $columnNames['model_morph_key'], 'model_type'], + 'model_has_permissions_permission_model_type_primary'); + } else { + $table->primary([$pivotPermission, $columnNames['model_morph_key'], 'model_type'], + 'model_has_permissions_permission_model_type_primary'); + } + + }); + + Schema::create($tableNames['model_has_roles'], function (Blueprint $table) use ($tableNames, $columnNames, $pivotRole, $teams) { + $table->unsignedBigInteger($pivotRole); + + $table->string('model_type'); + $table->unsignedBigInteger($columnNames['model_morph_key']); + $table->index([$columnNames['model_morph_key'], 'model_type'], 'model_has_roles_model_id_model_type_index'); + + $table->foreign($pivotRole) + ->references('id') // role id + ->on($tableNames['roles']) + ->onDelete('cascade'); + if ($teams) { + $table->unsignedBigInteger($columnNames['team_foreign_key']); + $table->index($columnNames['team_foreign_key'], 'model_has_roles_team_foreign_key_index'); + + $table->primary([$columnNames['team_foreign_key'], $pivotRole, $columnNames['model_morph_key'], 'model_type'], + 'model_has_roles_role_model_type_primary'); + } else { + $table->primary([$pivotRole, $columnNames['model_morph_key'], 'model_type'], + 'model_has_roles_role_model_type_primary'); + } + }); + + Schema::create($tableNames['role_has_permissions'], function (Blueprint $table) use ($tableNames, $pivotRole, $pivotPermission) { + $table->unsignedBigInteger($pivotPermission); + $table->unsignedBigInteger($pivotRole); + + $table->foreign($pivotPermission) + ->references('id') // permission id + ->on($tableNames['permissions']) + ->onDelete('cascade'); + + $table->foreign($pivotRole) + ->references('id') // role id + ->on($tableNames['roles']) + ->onDelete('cascade'); + + $table->primary([$pivotPermission, $pivotRole], 'role_has_permissions_permission_id_role_id_primary'); + }); + + app('cache') + ->store(config('permission.cache.store') != 'default' ? config('permission.cache.store') : null) + ->forget(config('permission.cache.key')); + } + + /** + * Reverse the migrations. + */ + public function down(): void + { + $tableNames = config('permission.table_names'); + + if (empty($tableNames)) { + throw new \Exception('Error: config/permission.php not found and defaults could not be merged. Please publish the package configuration before proceeding, or drop the tables manually.'); + } + + Schema::drop($tableNames['role_has_permissions']); + Schema::drop($tableNames['model_has_roles']); + Schema::drop($tableNames['model_has_permissions']); + Schema::drop($tableNames['roles']); + Schema::drop($tableNames['permissions']); + } +}; From e888554cfae5dbdc3c2738144a359abb9443a3b0 Mon Sep 17 00:00:00 2001 From: "Sander den Hollander :)" Date: Sat, 25 May 2024 13:55:08 +0200 Subject: [PATCH 03/22] Created Routes --- app/Models/Routes.php | 11 ++++++ database/factories/RoutesFactory.php | 23 ++++++++++++ .../2024_05_25_114853_create_routes_table.php | 35 +++++++++++++++++++ database/seeders/RoutesSeeder.php | 17 +++++++++ tests/Feature/Models/RoutesTest.php | 5 +++ 5 files changed, 91 insertions(+) create mode 100644 app/Models/Routes.php create mode 100644 database/factories/RoutesFactory.php create mode 100644 database/migrations/2024_05_25_114853_create_routes_table.php create mode 100644 database/seeders/RoutesSeeder.php create mode 100644 tests/Feature/Models/RoutesTest.php diff --git a/app/Models/Routes.php b/app/Models/Routes.php new file mode 100644 index 0000000..c8bc79b --- /dev/null +++ b/app/Models/Routes.php @@ -0,0 +1,11 @@ + + */ +class RoutesFactory extends Factory +{ + /** + * Define the model's default state. + * + * @return array + */ + public function definition(): array + { + return [ + // + ]; + } +} diff --git a/database/migrations/2024_05_25_114853_create_routes_table.php b/database/migrations/2024_05_25_114853_create_routes_table.php new file mode 100644 index 0000000..85a4809 --- /dev/null +++ b/database/migrations/2024_05_25_114853_create_routes_table.php @@ -0,0 +1,35 @@ +id(); + $table->string('name'); + $table->string('description'); + $table->foreignId('user_id')->constrained()->onDelete('cascade'); + $table->integer('length')->nullable(); + $table->integer('duration')->nullable(); + $table->dateTime('available_at')->nullable(); + $table->dateTime('unavailable_at')->nullable(); + $table->boolean('is_public')->default(false); + $table->timestamps(); + }); + } + + /** + * Reverse the migrations. + */ + public function down(): void + { + Schema::dropIfExists('routes'); + } +}; diff --git a/database/seeders/RoutesSeeder.php b/database/seeders/RoutesSeeder.php new file mode 100644 index 0000000..88fbc79 --- /dev/null +++ b/database/seeders/RoutesSeeder.php @@ -0,0 +1,17 @@ + Date: Sat, 25 May 2024 13:58:40 +0200 Subject: [PATCH 04/22] Created Locations --- app/Models/Locations.php | 11 ++++++ database/factories/LocationsFactory.php | 23 +++++++++++++ ...24_05_25_115537_create_locations_table.php | 34 +++++++++++++++++++ database/seeders/LocationsSeeder.php | 17 ++++++++++ tests/Feature/Models/LocationsTest.php | 7 ++++ 5 files changed, 92 insertions(+) create mode 100644 app/Models/Locations.php create mode 100644 database/factories/LocationsFactory.php create mode 100644 database/migrations/2024_05_25_115537_create_locations_table.php create mode 100644 database/seeders/LocationsSeeder.php create mode 100644 tests/Feature/Models/LocationsTest.php diff --git a/app/Models/Locations.php b/app/Models/Locations.php new file mode 100644 index 0000000..60cce60 --- /dev/null +++ b/app/Models/Locations.php @@ -0,0 +1,11 @@ + + */ +class LocationsFactory extends Factory +{ + /** + * Define the model's default state. + * + * @return array + */ + public function definition(): array + { + return [ + // + ]; + } +} diff --git a/database/migrations/2024_05_25_115537_create_locations_table.php b/database/migrations/2024_05_25_115537_create_locations_table.php new file mode 100644 index 0000000..acda4ab --- /dev/null +++ b/database/migrations/2024_05_25_115537_create_locations_table.php @@ -0,0 +1,34 @@ +id(); + $table->string('name'); + $table->string('description'); + $table->foreignId('route_id')->constrained()->onDelete('cascade'); + $table->integer('order'); + $table->string('latitude')->nullable(); + $table->string('longitude')->nullable(); + $table->string('address'); + $table->timestamps(); + }); + } + + /** + * Reverse the migrations. + */ + public function down(): void + { + Schema::dropIfExists('locations'); + } +}; diff --git a/database/seeders/LocationsSeeder.php b/database/seeders/LocationsSeeder.php new file mode 100644 index 0000000..0887757 --- /dev/null +++ b/database/seeders/LocationsSeeder.php @@ -0,0 +1,17 @@ +get('/'); + + $response->assertStatus(200); +}); From 4ee3d77d82ab6b3d60b02c250c762b92bb1ab17c Mon Sep 17 00:00:00 2001 From: "Sander den Hollander :)" Date: Sat, 25 May 2024 14:04:43 +0200 Subject: [PATCH 05/22] Created Questions --- app/Models/Questions.php | 11 +++++++ database/factories/QuestionsFactory.php | 23 ++++++++++++++ ...24_05_25_115853_create_questions_table.php | 31 +++++++++++++++++++ database/seeders/QuestionsSeeder.php | 17 ++++++++++ tests/Feature/Models/QuestionsTest.php | 7 +++++ 5 files changed, 89 insertions(+) create mode 100644 app/Models/Questions.php create mode 100644 database/factories/QuestionsFactory.php create mode 100644 database/migrations/2024_05_25_115853_create_questions_table.php create mode 100644 database/seeders/QuestionsSeeder.php create mode 100644 tests/Feature/Models/QuestionsTest.php diff --git a/app/Models/Questions.php b/app/Models/Questions.php new file mode 100644 index 0000000..311d0df --- /dev/null +++ b/app/Models/Questions.php @@ -0,0 +1,11 @@ + + */ +class QuestionsFactory extends Factory +{ + /** + * Define the model's default state. + * + * @return array + */ + public function definition(): array + { + return [ + // + ]; + } +} diff --git a/database/migrations/2024_05_25_115853_create_questions_table.php b/database/migrations/2024_05_25_115853_create_questions_table.php new file mode 100644 index 0000000..1fbc399 --- /dev/null +++ b/database/migrations/2024_05_25_115853_create_questions_table.php @@ -0,0 +1,31 @@ +id(); + $table->foreignId('location_id')->constrained()->cascadeOnDelete(); + $table->string('question'); + $table->enum('type', ['text', 'radio', 'checkbox']); + $table->integer('order')->default(0); + $table->timestamps(); + }); + } + + /** + * Reverse the migrations. + */ + public function down(): void + { + Schema::dropIfExists('questions'); + } +}; diff --git a/database/seeders/QuestionsSeeder.php b/database/seeders/QuestionsSeeder.php new file mode 100644 index 0000000..96676d8 --- /dev/null +++ b/database/seeders/QuestionsSeeder.php @@ -0,0 +1,17 @@ +get('/'); + + $response->assertStatus(200); +}); From 9220444ea64ca7bc40ea9a81d823100aba1829c5 Mon Sep 17 00:00:00 2001 From: "Sander den Hollander :)" Date: Sat, 25 May 2024 14:06:19 +0200 Subject: [PATCH 06/22] Created Answers --- app/Models/Answers.php | 11 +++++++ database/factories/AnswersFactory.php | 23 ++++++++++++++ ...2024_05_25_120449_create_answers_table.php | 30 +++++++++++++++++++ database/seeders/AnswersSeeder.php | 17 +++++++++++ tests/Feature/Models/AnswersTest.php | 7 +++++ 5 files changed, 88 insertions(+) create mode 100644 app/Models/Answers.php create mode 100644 database/factories/AnswersFactory.php create mode 100644 database/migrations/2024_05_25_120449_create_answers_table.php create mode 100644 database/seeders/AnswersSeeder.php create mode 100644 tests/Feature/Models/AnswersTest.php diff --git a/app/Models/Answers.php b/app/Models/Answers.php new file mode 100644 index 0000000..a710839 --- /dev/null +++ b/app/Models/Answers.php @@ -0,0 +1,11 @@ + + */ +class AnswersFactory extends Factory +{ + /** + * Define the model's default state. + * + * @return array + */ + public function definition(): array + { + return [ + // + ]; + } +} diff --git a/database/migrations/2024_05_25_120449_create_answers_table.php b/database/migrations/2024_05_25_120449_create_answers_table.php new file mode 100644 index 0000000..4016755 --- /dev/null +++ b/database/migrations/2024_05_25_120449_create_answers_table.php @@ -0,0 +1,30 @@ +id(); + $table->foreignId('question_id')->constrained()->onDelete('cascade'); + $table->string('answer'); + $table->boolean('is_correct')->default(false); + $table->timestamps(); + }); + } + + /** + * Reverse the migrations. + */ + public function down(): void + { + Schema::dropIfExists('answers'); + } +}; diff --git a/database/seeders/AnswersSeeder.php b/database/seeders/AnswersSeeder.php new file mode 100644 index 0000000..d77fb24 --- /dev/null +++ b/database/seeders/AnswersSeeder.php @@ -0,0 +1,17 @@ +get('/'); + + $response->assertStatus(200); +}); From 088f040e1ba8a75356f675f48039974b601484e7 Mon Sep 17 00:00:00 2001 From: "Sander den Hollander :)" Date: Sat, 25 May 2024 14:08:51 +0200 Subject: [PATCH 07/22] Created Media Table --- app/Models/Media.php | 11 ++++++ database/factories/MediaFactory.php | 23 +++++++++++++ .../2024_05_25_120624_create_media_table.php | 34 +++++++++++++++++++ database/seeders/MediaSeeder.php | 17 ++++++++++ tests/Feature/Models/MediaTest.php | 7 ++++ 5 files changed, 92 insertions(+) create mode 100644 app/Models/Media.php create mode 100644 database/factories/MediaFactory.php create mode 100644 database/migrations/2024_05_25_120624_create_media_table.php create mode 100644 database/seeders/MediaSeeder.php create mode 100644 tests/Feature/Models/MediaTest.php diff --git a/app/Models/Media.php b/app/Models/Media.php new file mode 100644 index 0000000..5a5464c --- /dev/null +++ b/app/Models/Media.php @@ -0,0 +1,11 @@ + + */ +class MediaFactory extends Factory +{ + /** + * Define the model's default state. + * + * @return array + */ + public function definition(): array + { + return [ + // + ]; + } +} diff --git a/database/migrations/2024_05_25_120624_create_media_table.php b/database/migrations/2024_05_25_120624_create_media_table.php new file mode 100644 index 0000000..becc37b --- /dev/null +++ b/database/migrations/2024_05_25_120624_create_media_table.php @@ -0,0 +1,34 @@ +id(); + $table->foreignId('route_id')->constrained()->cascadeOnDelete(); + $table->foreignId('location_id')->constrained()->cascadeOnDelete(); + $table->foreignId('question_id')->constrained()->cascadeOnDelete(); + $table->foreignId('answer_id')->constrained()->cascadeOnDelete(); + $table->string('url'); + $table->enum('type', ['image', 'video', 'audio']); + $table->string('alt')->nullable(); + $table->timestamps(); + }); + } + + /** + * Reverse the migrations. + */ + public function down(): void + { + Schema::dropIfExists('media'); + } +}; diff --git a/database/seeders/MediaSeeder.php b/database/seeders/MediaSeeder.php new file mode 100644 index 0000000..7ea75cf --- /dev/null +++ b/database/seeders/MediaSeeder.php @@ -0,0 +1,17 @@ +get('/'); + + $response->assertStatus(200); +}); From de8200ec08df61e98c392ea03b65ebc23fc3acf4 Mon Sep 17 00:00:00 2001 From: "Sander den Hollander :)" Date: Sat, 25 May 2024 14:12:31 +0200 Subject: [PATCH 08/22] Created Progress --- app/Models/Progress.php | 11 ++++++ database/factories/ProgressFactory.php | 23 ++++++++++++ ...024_05_25_120858_create_progress_table.php | 35 +++++++++++++++++++ database/seeders/ProgressSeeder.php | 17 +++++++++ tests/Feature/Models/ProgressTest.php | 7 ++++ 5 files changed, 93 insertions(+) create mode 100644 app/Models/Progress.php create mode 100644 database/factories/ProgressFactory.php create mode 100644 database/migrations/2024_05_25_120858_create_progress_table.php create mode 100644 database/seeders/ProgressSeeder.php create mode 100644 tests/Feature/Models/ProgressTest.php diff --git a/app/Models/Progress.php b/app/Models/Progress.php new file mode 100644 index 0000000..4350863 --- /dev/null +++ b/app/Models/Progress.php @@ -0,0 +1,11 @@ + + */ +class ProgressFactory extends Factory +{ + /** + * Define the model's default state. + * + * @return array + */ + public function definition(): array + { + return [ + // + ]; + } +} diff --git a/database/migrations/2024_05_25_120858_create_progress_table.php b/database/migrations/2024_05_25_120858_create_progress_table.php new file mode 100644 index 0000000..23ff0c8 --- /dev/null +++ b/database/migrations/2024_05_25_120858_create_progress_table.php @@ -0,0 +1,35 @@ +id(); + $table->foreignId('user_id')->constrained()->cascadeOnDelete(); + $table->foreignId('route_id')->constrained()->cascadeOnDelete(); + $table->integer('progress')->default(0); + $table->foreignId('location_id')->nullable()->constrained()->nullOnDelete(); + $table->foreignId('question_id')->nullable()->constrained()->nullOnDelete(); + $table->integer('score')->default(0); + $table->timestamp('started_at')->nullable(); + $table->timestamp('completed_at')->nullable(); + $table->timestamps(); + }); + } + + /** + * Reverse the migrations. + */ + public function down(): void + { + Schema::dropIfExists('progress'); + } +}; diff --git a/database/seeders/ProgressSeeder.php b/database/seeders/ProgressSeeder.php new file mode 100644 index 0000000..21798f7 --- /dev/null +++ b/database/seeders/ProgressSeeder.php @@ -0,0 +1,17 @@ +get('/'); + + $response->assertStatus(200); +}); From 8b5f6c60a3ade5c01d6eda924da791a4bff54f27 Mon Sep 17 00:00:00 2001 From: "Sander den Hollander :)" Date: Sat, 25 May 2024 14:15:16 +0200 Subject: [PATCH 09/22] Renamed all to singular --- app/Models/{Routes.php => Answer.php} | 2 +- app/Models/{Locations.php => Location.php} | 2 +- app/Models/{Questions.php => Question.php} | 2 +- app/Models/{Answers.php => Route.php} | 2 +- config/fortify.php | 6 +++--- database/factories/{RoutesFactory.php => AnswerFactory.php} | 4 ++-- .../factories/{LocationsFactory.php => LocationFactory.php} | 4 ++-- .../factories/{QuestionsFactory.php => QuestionFactory.php} | 4 ++-- database/factories/{AnswersFactory.php => RouteFactory.php} | 4 ++-- 9 files changed, 15 insertions(+), 15 deletions(-) rename app/Models/{Routes.php => Answer.php} (84%) rename app/Models/{Locations.php => Location.php} (83%) rename app/Models/{Questions.php => Question.php} (83%) rename app/Models/{Answers.php => Route.php} (84%) rename database/factories/{RoutesFactory.php => AnswerFactory.php} (87%) rename database/factories/{LocationsFactory.php => LocationFactory.php} (86%) rename database/factories/{QuestionsFactory.php => QuestionFactory.php} (86%) rename database/factories/{AnswersFactory.php => RouteFactory.php} (87%) diff --git a/app/Models/Routes.php b/app/Models/Answer.php similarity index 84% rename from app/Models/Routes.php rename to app/Models/Answer.php index c8bc79b..c2e96b1 100644 --- a/app/Models/Routes.php +++ b/app/Models/Answer.php @@ -5,7 +5,7 @@ use Illuminate\Database\Eloquent\Factories\HasFactory; use Illuminate\Database\Eloquent\Model; -class Routes extends Model +class Answer extends Model { use HasFactory; } diff --git a/app/Models/Locations.php b/app/Models/Location.php similarity index 83% rename from app/Models/Locations.php rename to app/Models/Location.php index 60cce60..35b1313 100644 --- a/app/Models/Locations.php +++ b/app/Models/Location.php @@ -5,7 +5,7 @@ use Illuminate\Database\Eloquent\Factories\HasFactory; use Illuminate\Database\Eloquent\Model; -class Locations extends Model +class Location extends Model { use HasFactory; } diff --git a/app/Models/Questions.php b/app/Models/Question.php similarity index 83% rename from app/Models/Questions.php rename to app/Models/Question.php index 311d0df..b1b8eaa 100644 --- a/app/Models/Questions.php +++ b/app/Models/Question.php @@ -5,7 +5,7 @@ use Illuminate\Database\Eloquent\Factories\HasFactory; use Illuminate\Database\Eloquent\Model; -class Questions extends Model +class Question extends Model { use HasFactory; } diff --git a/app/Models/Answers.php b/app/Models/Route.php similarity index 84% rename from app/Models/Answers.php rename to app/Models/Route.php index a710839..ca1ed57 100644 --- a/app/Models/Answers.php +++ b/app/Models/Route.php @@ -5,7 +5,7 @@ use Illuminate\Database\Eloquent\Factories\HasFactory; use Illuminate\Database\Eloquent\Model; -class Answers extends Model +class Route extends Model { use HasFactory; } diff --git a/config/fortify.php b/config/fortify.php index 0551d1d..0be801d 100644 --- a/config/fortify.php +++ b/config/fortify.php @@ -77,7 +77,7 @@ /* |-------------------------------------------------------------------------- - | Fortify Routes Prefix / Subdomain + | Fortify Route Prefix / Subdomain |-------------------------------------------------------------------------- | | Here you may specify which prefix Fortify will assign to all the routes @@ -92,7 +92,7 @@ /* |-------------------------------------------------------------------------- - | Fortify Routes Middleware + | Fortify Route Middleware |-------------------------------------------------------------------------- | | Here you may specify which middleware Fortify will assign to the routes @@ -121,7 +121,7 @@ /* |-------------------------------------------------------------------------- - | Register View Routes + | Register View Route |-------------------------------------------------------------------------- | | Here you may specify if the routes returning views should be disabled as diff --git a/database/factories/RoutesFactory.php b/database/factories/AnswerFactory.php similarity index 87% rename from database/factories/RoutesFactory.php rename to database/factories/AnswerFactory.php index bb60603..7116fcf 100644 --- a/database/factories/RoutesFactory.php +++ b/database/factories/AnswerFactory.php @@ -5,9 +5,9 @@ use Illuminate\Database\Eloquent\Factories\Factory; /** - * @extends \Illuminate\Database\Eloquent\Factories\Factory<\App\Models\Routes> + * @extends \Illuminate\Database\Eloquent\Factories\Factory<\App\Models\Answer> */ -class RoutesFactory extends Factory +class AnswerFactory extends Factory { /** * Define the model's default state. diff --git a/database/factories/LocationsFactory.php b/database/factories/LocationFactory.php similarity index 86% rename from database/factories/LocationsFactory.php rename to database/factories/LocationFactory.php index 57a31a7..0ce01f4 100644 --- a/database/factories/LocationsFactory.php +++ b/database/factories/LocationFactory.php @@ -5,9 +5,9 @@ use Illuminate\Database\Eloquent\Factories\Factory; /** - * @extends \Illuminate\Database\Eloquent\Factories\Factory<\App\Models\Locations> + * @extends \Illuminate\Database\Eloquent\Factories\Factory<\App\Models\Location> */ -class LocationsFactory extends Factory +class LocationFactory extends Factory { /** * Define the model's default state. diff --git a/database/factories/QuestionsFactory.php b/database/factories/QuestionFactory.php similarity index 86% rename from database/factories/QuestionsFactory.php rename to database/factories/QuestionFactory.php index b6cb3e3..957622e 100644 --- a/database/factories/QuestionsFactory.php +++ b/database/factories/QuestionFactory.php @@ -5,9 +5,9 @@ use Illuminate\Database\Eloquent\Factories\Factory; /** - * @extends \Illuminate\Database\Eloquent\Factories\Factory<\App\Models\Questions> + * @extends \Illuminate\Database\Eloquent\Factories\Factory<\App\Models\Question> */ -class QuestionsFactory extends Factory +class QuestionFactory extends Factory { /** * Define the model's default state. diff --git a/database/factories/AnswersFactory.php b/database/factories/RouteFactory.php similarity index 87% rename from database/factories/AnswersFactory.php rename to database/factories/RouteFactory.php index 4042a0e..c78f0ab 100644 --- a/database/factories/AnswersFactory.php +++ b/database/factories/RouteFactory.php @@ -5,9 +5,9 @@ use Illuminate\Database\Eloquent\Factories\Factory; /** - * @extends \Illuminate\Database\Eloquent\Factories\Factory<\App\Models\Answers> + * @extends \Illuminate\Database\Eloquent\Factories\Factory<\App\Models\Route> */ -class AnswersFactory extends Factory +class RouteFactory extends Factory { /** * Define the model's default state. From 66eb63df5a98c9b1fdcc80cf80d1e7c3b2adaac6 Mon Sep 17 00:00:00 2001 From: "Sander den Hollander :)" Date: Mon, 27 May 2024 09:50:13 +0200 Subject: [PATCH 10/22] Replaced sentry replay script --- resources/views/layouts/app.blade.php | 2 +- resources/views/layouts/guest.blade.php | 2 +- resources/views/layouts/main.blade.php | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/resources/views/layouts/app.blade.php b/resources/views/layouts/app.blade.php index 267eb93..990ff09 100644 --- a/resources/views/layouts/app.blade.php +++ b/resources/views/layouts/app.blade.php @@ -51,7 +51,7 @@ diff --git a/resources/views/layouts/guest.blade.php b/resources/views/layouts/guest.blade.php index 7c33d5a..addc302 100644 --- a/resources/views/layouts/guest.blade.php +++ b/resources/views/layouts/guest.blade.php @@ -51,7 +51,7 @@ diff --git a/resources/views/layouts/main.blade.php b/resources/views/layouts/main.blade.php index 9e32af4..bdde8a0 100644 --- a/resources/views/layouts/main.blade.php +++ b/resources/views/layouts/main.blade.php @@ -61,7 +61,7 @@ class="h-full" From 8f4f9cf6ac8e0f416e7f5f199cd0dbe3c10c5755 Mon Sep 17 00:00:00 2001 From: "Sander den Hollander :)" Date: Mon, 27 May 2024 10:47:38 +0200 Subject: [PATCH 11/22] Renamed locations to checkpoints --- app/Models/Checkpoint.php | 36 +++++++++++++++++++ app/Models/Location.php | 11 ------ ...05_25_115537_create_checkpoints_table.php} | 4 +-- ...ationsSeeder.php => CheckpointsSeeder.php} | 2 +- ...{LocationsTest.php => CheckpointsTest.php} | 0 5 files changed, 39 insertions(+), 14 deletions(-) create mode 100644 app/Models/Checkpoint.php delete mode 100644 app/Models/Location.php rename database/migrations/{2024_05_25_115537_create_locations_table.php => 2024_05_25_115537_create_checkpoints_table.php} (87%) rename database/seeders/{LocationsSeeder.php => CheckpointsSeeder.php} (85%) rename tests/Feature/Models/{LocationsTest.php => CheckpointsTest.php} (100%) diff --git a/app/Models/Checkpoint.php b/app/Models/Checkpoint.php new file mode 100644 index 0000000..97a4f55 --- /dev/null +++ b/app/Models/Checkpoint.php @@ -0,0 +1,36 @@ +belongsTo(Route::class); + } + + public function question() + { + return $this->hasMany(Question::class); + } + + public function media() + { + return $this->morphMany(Media::class, 'mediable'); + } +} diff --git a/app/Models/Location.php b/app/Models/Location.php deleted file mode 100644 index 35b1313..0000000 --- a/app/Models/Location.php +++ /dev/null @@ -1,11 +0,0 @@ -id(); $table->string('name'); $table->string('description'); @@ -29,6 +29,6 @@ public function up(): void */ public function down(): void { - Schema::dropIfExists('locations'); + Schema::dropIfExists('checkpoints'); } }; diff --git a/database/seeders/LocationsSeeder.php b/database/seeders/CheckpointsSeeder.php similarity index 85% rename from database/seeders/LocationsSeeder.php rename to database/seeders/CheckpointsSeeder.php index 0887757..dd5ca22 100644 --- a/database/seeders/LocationsSeeder.php +++ b/database/seeders/CheckpointsSeeder.php @@ -5,7 +5,7 @@ use Illuminate\Database\Console\Seeds\WithoutModelEvents; use Illuminate\Database\Seeder; -class LocationsSeeder extends Seeder +class CheckpointsSeeder extends Seeder { /** * Run the database seeds. diff --git a/tests/Feature/Models/LocationsTest.php b/tests/Feature/Models/CheckpointsTest.php similarity index 100% rename from tests/Feature/Models/LocationsTest.php rename to tests/Feature/Models/CheckpointsTest.php From 824dbf8e63b3cc6f8eb08de9e55bb3251b4e98ed Mon Sep 17 00:00:00 2001 From: "Sander den Hollander :)" Date: Mon, 27 May 2024 10:47:43 +0200 Subject: [PATCH 12/22] Renamed locations to checkpoints --- .../factories/{LocationFactory.php => CheckpointFactory.php} | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) rename database/factories/{LocationFactory.php => CheckpointFactory.php} (85%) diff --git a/database/factories/LocationFactory.php b/database/factories/CheckpointFactory.php similarity index 85% rename from database/factories/LocationFactory.php rename to database/factories/CheckpointFactory.php index 0ce01f4..e6ed7f4 100644 --- a/database/factories/LocationFactory.php +++ b/database/factories/CheckpointFactory.php @@ -5,9 +5,9 @@ use Illuminate\Database\Eloquent\Factories\Factory; /** - * @extends \Illuminate\Database\Eloquent\Factories\Factory<\App\Models\Location> + * @extends \Illuminate\Database\Eloquent\Factories\Factory<\App\Models\Checkpoint> */ -class LocationFactory extends Factory +class CheckpointFactory extends Factory { /** * Define the model's default state. From b061c589a4bac29dbcc1eefca83b75de187e504f Mon Sep 17 00:00:00 2001 From: "Sander den Hollander :)" Date: Mon, 27 May 2024 10:48:01 +0200 Subject: [PATCH 13/22] Added polymorphic relationships --- .../migrations/2024_05_25_120624_create_media_table.php | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/database/migrations/2024_05_25_120624_create_media_table.php b/database/migrations/2024_05_25_120624_create_media_table.php index becc37b..05db074 100644 --- a/database/migrations/2024_05_25_120624_create_media_table.php +++ b/database/migrations/2024_05_25_120624_create_media_table.php @@ -13,13 +13,11 @@ public function up(): void { Schema::create('media', function (Blueprint $table) { $table->id(); - $table->foreignId('route_id')->constrained()->cascadeOnDelete(); - $table->foreignId('location_id')->constrained()->cascadeOnDelete(); - $table->foreignId('question_id')->constrained()->cascadeOnDelete(); - $table->foreignId('answer_id')->constrained()->cascadeOnDelete(); $table->string('url'); $table->enum('type', ['image', 'video', 'audio']); $table->string('alt')->nullable(); + $table->integer('mediable_id'); + $table->string('mediable_type'); $table->timestamps(); }); } From dee3231756dceae759f48c5d67cb39dc631d1015 Mon Sep 17 00:00:00 2001 From: "Sander den Hollander :)" Date: Mon, 27 May 2024 10:49:30 +0200 Subject: [PATCH 14/22] Renamed locations to checkpoints --- .../migrations/2024_05_25_115853_create_questions_table.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/database/migrations/2024_05_25_115853_create_questions_table.php b/database/migrations/2024_05_25_115853_create_questions_table.php index 1fbc399..edc03b3 100644 --- a/database/migrations/2024_05_25_115853_create_questions_table.php +++ b/database/migrations/2024_05_25_115853_create_questions_table.php @@ -13,7 +13,7 @@ public function up(): void { Schema::create('questions', function (Blueprint $table) { $table->id(); - $table->foreignId('location_id')->constrained()->cascadeOnDelete(); + $table->foreignId('checkpoint_id')->constrained()->cascadeOnDelete(); $table->string('question'); $table->enum('type', ['text', 'radio', 'checkbox']); $table->integer('order')->default(0); From cc6bf414fc6aa664c68a61dc6a9af73c5538c656 Mon Sep 17 00:00:00 2001 From: "Sander den Hollander :)" Date: Mon, 27 May 2024 10:50:00 +0200 Subject: [PATCH 15/22] Renamed locations to checkpoints --- database/migrations/2024_05_25_120858_create_progress_table.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/database/migrations/2024_05_25_120858_create_progress_table.php b/database/migrations/2024_05_25_120858_create_progress_table.php index 23ff0c8..b7229e9 100644 --- a/database/migrations/2024_05_25_120858_create_progress_table.php +++ b/database/migrations/2024_05_25_120858_create_progress_table.php @@ -16,7 +16,7 @@ public function up(): void $table->foreignId('user_id')->constrained()->cascadeOnDelete(); $table->foreignId('route_id')->constrained()->cascadeOnDelete(); $table->integer('progress')->default(0); - $table->foreignId('location_id')->nullable()->constrained()->nullOnDelete(); + $table->foreignId('checkpoint_id')->nullable()->constrained()->nullOnDelete(); $table->foreignId('question_id')->nullable()->constrained()->nullOnDelete(); $table->integer('score')->default(0); $table->timestamp('started_at')->nullable(); From 90925d6e7387c3810bfc61c9ebbcf0e544654382 Mon Sep 17 00:00:00 2001 From: "Sander den Hollander :)" Date: Mon, 27 May 2024 10:59:09 +0200 Subject: [PATCH 16/22] Filled in models --- app/Models/Answer.php | 16 ++++++++++++++++ app/Models/Media.php | 15 +++++++++++++++ app/Models/Progress.php | 26 ++++++++++++++++++++++++++ app/Models/Question.php | 22 ++++++++++++++++++++++ app/Models/Route.php | 32 ++++++++++++++++++++++++++++++++ app/Models/User.php | 9 +++++++++ 6 files changed, 120 insertions(+) diff --git a/app/Models/Answer.php b/app/Models/Answer.php index c2e96b1..6bd3e1b 100644 --- a/app/Models/Answer.php +++ b/app/Models/Answer.php @@ -8,4 +8,20 @@ class Answer extends Model { use HasFactory; + + protected $fillable = [ + 'question_id', + 'answer', + 'is_correct' + ]; + + public function question() + { + return $this->belongsTo(Question::class); + } + + public function media() + { + return $this->morphMany(Media::class, 'mediable'); + } } diff --git a/app/Models/Media.php b/app/Models/Media.php index 5a5464c..52d27f9 100644 --- a/app/Models/Media.php +++ b/app/Models/Media.php @@ -8,4 +8,19 @@ class Media extends Model { use HasFactory; + + protected $fillable = [ + 'route_id', + 'location_id', + 'question_id', + 'answer_id', + 'url', + 'type', + 'alt' + ]; + + public function mediable() + { + return $this->morphTo(); + } } diff --git a/app/Models/Progress.php b/app/Models/Progress.php index 4350863..05abc96 100644 --- a/app/Models/Progress.php +++ b/app/Models/Progress.php @@ -8,4 +8,30 @@ class Progress extends Model { use HasFactory; + + protected $fillable = [ + 'user_id', + 'route_id', + 'progress', + 'location_id', + 'question_id', + 'score', + 'started_at', + 'completed_at' + ]; + + public function user() + { + return $this->belongsTo(User::class); + } + + public function route() + { + return $this->belongsTo(Route::class); + } + + public function checkpoint() + { + return $this->has(Checkpoint::class); + } } diff --git a/app/Models/Question.php b/app/Models/Question.php index b1b8eaa..8c3c985 100644 --- a/app/Models/Question.php +++ b/app/Models/Question.php @@ -8,4 +8,26 @@ class Question extends Model { use HasFactory; + + protected $fillable = [ + 'question', + 'location_id', + 'type', + 'order' + ]; + + public function checkpoint() + { + return $this->belongsTo(Checkpoint::class); + } + + public function answers() + { + return $this->hasMany(Answer::class); + } + + public function media() + { + return $this->morphMany(Media::class, 'mediable'); + } } diff --git a/app/Models/Route.php b/app/Models/Route.php index ca1ed57..f0be1b8 100644 --- a/app/Models/Route.php +++ b/app/Models/Route.php @@ -8,4 +8,36 @@ class Route extends Model { use HasFactory; + + public function checkpoints() + { + return $this->hasMany(Checkpoint::class); + } + + protected $fillable = [ + 'name', + 'description', + 'difficulty', + 'length', + 'duration', + 'user_id', + 'available_at', + 'unavailable_at', + 'is_public' + ]; + + public function user() + { + return $this->belongsTo(User::class); + } + + public function progress() + { + return $this->hasMany(Progress::class); + } + + public function media() + { + return $this->morphMany(Media::class, 'mediable'); + } } diff --git a/app/Models/User.php b/app/Models/User.php index ab45c03..3283300 100644 --- a/app/Models/User.php +++ b/app/Models/User.php @@ -66,4 +66,13 @@ protected function casts(): array 'password' => 'hashed', ]; } + + public function routes() + { + return $this->hasMany(Route::class); + } + + public function progress() { + return $this->hasMany(Progress::class); + } } From 1c40937a2c850a986f2dc3a412567927087f3b9a Mon Sep 17 00:00:00 2001 From: "Sander den Hollander :)" Date: Mon, 27 May 2024 17:11:21 +0200 Subject: [PATCH 17/22] Filled in factories --- database/factories/AnswerFactory.php | 5 ++++- database/factories/CheckpointFactory.php | 10 +++++++++- database/factories/MediaFactory.php | 6 +++++- database/factories/ProgressFactory.php | 10 +++++++++- database/factories/QuestionFactory.php | 6 +++++- database/factories/RouteFactory.php | 10 +++++++++- 6 files changed, 41 insertions(+), 6 deletions(-) diff --git a/database/factories/AnswerFactory.php b/database/factories/AnswerFactory.php index 7116fcf..704d9ba 100644 --- a/database/factories/AnswerFactory.php +++ b/database/factories/AnswerFactory.php @@ -2,6 +2,7 @@ namespace Database\Factories; +use App\Models\Question; use Illuminate\Database\Eloquent\Factories\Factory; /** @@ -17,7 +18,9 @@ class AnswerFactory extends Factory public function definition(): array { return [ - // + 'question_id' => Question::all()->random()->id, + 'answer' => $this->faker->sentence(10), + 'is_correct' => $this->faker->boolean, ]; } } diff --git a/database/factories/CheckpointFactory.php b/database/factories/CheckpointFactory.php index e6ed7f4..0e76c0e 100644 --- a/database/factories/CheckpointFactory.php +++ b/database/factories/CheckpointFactory.php @@ -2,6 +2,7 @@ namespace Database\Factories; +use App\Models\Route; use Illuminate\Database\Eloquent\Factories\Factory; /** @@ -16,8 +17,15 @@ class CheckpointFactory extends Factory */ public function definition(): array { + $address = $this->faker->address; return [ - // + 'name' => $address, + 'description' => $this->faker->sentence(30), + 'route_id' => Route::all()->random()->id, + 'order' => 0, + 'latitude' => $this->faker->latitude, + 'longitude' => $this->faker->longitude, + 'address' => $address, ]; } } diff --git a/database/factories/MediaFactory.php b/database/factories/MediaFactory.php index b04a407..4fe4409 100644 --- a/database/factories/MediaFactory.php +++ b/database/factories/MediaFactory.php @@ -2,6 +2,7 @@ namespace Database\Factories; +use App\Models\Checkpoint; use Illuminate\Database\Eloquent\Factories\Factory; /** @@ -17,7 +18,10 @@ class MediaFactory extends Factory public function definition(): array { return [ - // + 'url' => $this->faker->imageUrl(), + 'type' => 'image', + 'mediable_type' => 'App\Models\Checkpoint', + 'mediable_id' => Checkpoint::all()->random()->id, ]; } } diff --git a/database/factories/ProgressFactory.php b/database/factories/ProgressFactory.php index e757f4e..953f07d 100644 --- a/database/factories/ProgressFactory.php +++ b/database/factories/ProgressFactory.php @@ -2,6 +2,8 @@ namespace Database\Factories; +use App\Models\Route; +use App\Models\User; use Illuminate\Database\Eloquent\Factories\Factory; /** @@ -16,8 +18,14 @@ class ProgressFactory extends Factory */ public function definition(): array { + $route = Route::all()->random(); return [ - // + 'user_id' => User::all()->random()->id, + 'route_id' => $route->id, + 'progress' => $this->faker->randomFloat(2, 0, $route->length), + 'checkpoint_id' => $route->checkpoints->random()->id, + 'started_at' => $this->faker->dateTimeBetween('-1 year', 'now'), + 'finished_at' => $this->faker->dateTimeBetween('-1 year', 'now'), ]; } } diff --git a/database/factories/QuestionFactory.php b/database/factories/QuestionFactory.php index 957622e..54da356 100644 --- a/database/factories/QuestionFactory.php +++ b/database/factories/QuestionFactory.php @@ -2,6 +2,7 @@ namespace Database\Factories; +use App\Models\Checkpoint; use Illuminate\Database\Eloquent\Factories\Factory; /** @@ -17,7 +18,10 @@ class QuestionFactory extends Factory public function definition(): array { return [ - // + 'checkpoint_id' => Checkpoint::all()->random()->id, + 'question' => $this->faker->sentence(10), + 'type' => $this->faker->randomElement(['text', 'radio', 'checkbox']), + 'order' => 0, ]; } } diff --git a/database/factories/RouteFactory.php b/database/factories/RouteFactory.php index c78f0ab..c124af2 100644 --- a/database/factories/RouteFactory.php +++ b/database/factories/RouteFactory.php @@ -2,6 +2,7 @@ namespace Database\Factories; +use App\Models\User; use Illuminate\Database\Eloquent\Factories\Factory; /** @@ -17,7 +18,14 @@ class RouteFactory extends Factory public function definition(): array { return [ - // + 'name' => $this->faker->sentence(6), + 'description' => $this->faker->sentence(20), + 'user_id' => User::all()->random()->id, + 'length' => $this->faker->randomFloat(2, 1, 100), + 'duration' => $this->faker->randomFloat(2, 1, 300), + 'available_at' => null, + 'unavailable_at' => null, + 'is_public' => $this->faker->boolean(50), ]; } } From 2dff9eaa595c04a9d4bb4f0511ed831ead13d5f7 Mon Sep 17 00:00:00 2001 From: "Sander den Hollander :)" Date: Mon, 27 May 2024 18:29:02 +0200 Subject: [PATCH 18/22] Change some strings to text --- database/migrations/2024_05_25_114853_create_routes_table.php | 2 +- .../migrations/2024_05_25_115537_create_checkpoints_table.php | 2 +- .../migrations/2024_05_25_115853_create_questions_table.php | 2 +- database/migrations/2024_05_25_120449_create_answers_table.php | 2 +- 4 files changed, 4 insertions(+), 4 deletions(-) diff --git a/database/migrations/2024_05_25_114853_create_routes_table.php b/database/migrations/2024_05_25_114853_create_routes_table.php index 85a4809..ae8312f 100644 --- a/database/migrations/2024_05_25_114853_create_routes_table.php +++ b/database/migrations/2024_05_25_114853_create_routes_table.php @@ -14,7 +14,7 @@ public function up(): void Schema::create('routes', function (Blueprint $table) { $table->id(); $table->string('name'); - $table->string('description'); + $table->text('description'); $table->foreignId('user_id')->constrained()->onDelete('cascade'); $table->integer('length')->nullable(); $table->integer('duration')->nullable(); diff --git a/database/migrations/2024_05_25_115537_create_checkpoints_table.php b/database/migrations/2024_05_25_115537_create_checkpoints_table.php index 3981718..efc9609 100644 --- a/database/migrations/2024_05_25_115537_create_checkpoints_table.php +++ b/database/migrations/2024_05_25_115537_create_checkpoints_table.php @@ -14,7 +14,7 @@ public function up(): void Schema::create('checkpoints', function (Blueprint $table) { $table->id(); $table->string('name'); - $table->string('description'); + $table->text('description'); $table->foreignId('route_id')->constrained()->onDelete('cascade'); $table->integer('order'); $table->string('latitude')->nullable(); diff --git a/database/migrations/2024_05_25_115853_create_questions_table.php b/database/migrations/2024_05_25_115853_create_questions_table.php index edc03b3..2cbb12a 100644 --- a/database/migrations/2024_05_25_115853_create_questions_table.php +++ b/database/migrations/2024_05_25_115853_create_questions_table.php @@ -14,7 +14,7 @@ public function up(): void Schema::create('questions', function (Blueprint $table) { $table->id(); $table->foreignId('checkpoint_id')->constrained()->cascadeOnDelete(); - $table->string('question'); + $table->text('question'); $table->enum('type', ['text', 'radio', 'checkbox']); $table->integer('order')->default(0); $table->timestamps(); diff --git a/database/migrations/2024_05_25_120449_create_answers_table.php b/database/migrations/2024_05_25_120449_create_answers_table.php index 4016755..f630884 100644 --- a/database/migrations/2024_05_25_120449_create_answers_table.php +++ b/database/migrations/2024_05_25_120449_create_answers_table.php @@ -14,7 +14,7 @@ public function up(): void Schema::create('answers', function (Blueprint $table) { $table->id(); $table->foreignId('question_id')->constrained()->onDelete('cascade'); - $table->string('answer'); + $table->text('answer'); $table->boolean('is_correct')->default(false); $table->timestamps(); }); From 593248913fa6de05b68c72492252e3d8cfe970c3 Mon Sep 17 00:00:00 2001 From: "Sander den Hollander :)" Date: Mon, 27 May 2024 18:29:33 +0200 Subject: [PATCH 19/22] Remove all separate seeders --- database/seeders/AnswersSeeder.php | 17 ----------------- database/seeders/CheckpointsSeeder.php | 17 ----------------- database/seeders/MediaSeeder.php | 17 ----------------- database/seeders/ProgressSeeder.php | 17 ----------------- database/seeders/QuestionsSeeder.php | 17 ----------------- database/seeders/RoutesSeeder.php | 17 ----------------- 6 files changed, 102 deletions(-) delete mode 100644 database/seeders/AnswersSeeder.php delete mode 100644 database/seeders/CheckpointsSeeder.php delete mode 100644 database/seeders/MediaSeeder.php delete mode 100644 database/seeders/ProgressSeeder.php delete mode 100644 database/seeders/QuestionsSeeder.php delete mode 100644 database/seeders/RoutesSeeder.php diff --git a/database/seeders/AnswersSeeder.php b/database/seeders/AnswersSeeder.php deleted file mode 100644 index d77fb24..0000000 --- a/database/seeders/AnswersSeeder.php +++ /dev/null @@ -1,17 +0,0 @@ - Date: Mon, 27 May 2024 19:02:47 +0200 Subject: [PATCH 20/22] Change enum types --- database/factories/QuestionFactory.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/database/factories/QuestionFactory.php b/database/factories/QuestionFactory.php index 54da356..926da4e 100644 --- a/database/factories/QuestionFactory.php +++ b/database/factories/QuestionFactory.php @@ -20,7 +20,7 @@ public function definition(): array return [ 'checkpoint_id' => Checkpoint::all()->random()->id, 'question' => $this->faker->sentence(10), - 'type' => $this->faker->randomElement(['text', 'radio', 'checkbox']), + 'type' => $this->faker->randomElement(['multiple_choice', 'true_or_false', 'short_answer']), 'order' => 0, ]; } From c69fc6476b58e99c2f9f85f2d5d1189c21cd7bc0 Mon Sep 17 00:00:00 2001 From: "Sander den Hollander :)" Date: Mon, 27 May 2024 19:04:05 +0200 Subject: [PATCH 21/22] changed wrong name --- database/factories/ProgressFactory.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/database/factories/ProgressFactory.php b/database/factories/ProgressFactory.php index 953f07d..7a0ad5b 100644 --- a/database/factories/ProgressFactory.php +++ b/database/factories/ProgressFactory.php @@ -25,7 +25,7 @@ public function definition(): array 'progress' => $this->faker->randomFloat(2, 0, $route->length), 'checkpoint_id' => $route->checkpoints->random()->id, 'started_at' => $this->faker->dateTimeBetween('-1 year', 'now'), - 'finished_at' => $this->faker->dateTimeBetween('-1 year', 'now'), + 'completed_at' => $this->faker->dateTimeBetween('-1 year', 'now'), ]; } } From 19916c3734d84860201e494fa02ac69a3ba0af14 Mon Sep 17 00:00:00 2001 From: "Sander den Hollander :)" Date: Mon, 27 May 2024 19:05:47 +0200 Subject: [PATCH 22/22] Factory works! --- ...24_05_25_115853_create_questions_table.php | 2 +- database/seeders/DatabaseSeeder.php | 67 ++++++++++++++++++- 2 files changed, 67 insertions(+), 2 deletions(-) diff --git a/database/migrations/2024_05_25_115853_create_questions_table.php b/database/migrations/2024_05_25_115853_create_questions_table.php index 2cbb12a..6ba3f84 100644 --- a/database/migrations/2024_05_25_115853_create_questions_table.php +++ b/database/migrations/2024_05_25_115853_create_questions_table.php @@ -15,7 +15,7 @@ public function up(): void $table->id(); $table->foreignId('checkpoint_id')->constrained()->cascadeOnDelete(); $table->text('question'); - $table->enum('type', ['text', 'radio', 'checkbox']); + $table->enum('type', ['multiple_choice', 'true_or_false', 'short_answer']); $table->integer('order')->default(0); $table->timestamps(); }); diff --git a/database/seeders/DatabaseSeeder.php b/database/seeders/DatabaseSeeder.php index d01a0ef..96ad23f 100644 --- a/database/seeders/DatabaseSeeder.php +++ b/database/seeders/DatabaseSeeder.php @@ -2,7 +2,14 @@ namespace Database\Seeders; +use App\Models\Answer; +use App\Models\Checkpoint; +use App\Models\Media; +use App\Models\Progress; +use App\Models\Question; +use App\Models\Route; use App\Models\User; + // use Illuminate\Database\Console\Seeds\WithoutModelEvents; use Illuminate\Database\Seeder; @@ -13,11 +20,69 @@ class DatabaseSeeder extends Seeder */ public function run(): void { - // User::factory(10)->create(); + User::factory(rand(10, 100))->create(); User::factory()->create([ 'name' => 'Test User', 'email' => 'test@example.com', ]); + + Route::factory(50)->create(); + + for ($i = 1; $i <= 450; $i++) { + $checkpoint = Checkpoint::factory()->create(); + $checkpointExisting = Checkpoint::where('route_id', $checkpoint->route_id); + if ($checkpointExisting->count() > 1) { + $checkpoint->order = $checkpointExisting->count() - 1; + $checkpoint->save(); + } + } + + foreach (Checkpoint::all() as $checkpoint) { + $question = null; + for ($i = 1; $i <= rand(0, 5); $i++) { + $question = Question::factory()->create([ + 'checkpoint_id' => $checkpoint->id, + ]); + $questionExisting = Question::where('checkpoint_id', $question->checkpoint_id); + if ($questionExisting->count() > 1) { + $question->order = $questionExisting->count() - 1; + $question->save(); + } + + $type = $question->type; + + if ($type === 'multiple_choice') { + $true = rand(0, 3); + for ($j = 0; $j < rand(3, 5); $j++) { + $correct = false; + if ((bool) $true) $correct = (bool) rand(0, 1); + if ($correct) $true -= 1; + $question->answers()->create([ + 'answer' => fake()->sentence(6) . ($correct ? ' (correct)' : ''), + 'is_correct' => $correct, + ]); + } + } elseif ($type === 'true_or_false') { + $question->answers()->create([ + 'answer' => 'True (correct)', + 'is_correct' => true, + ]); + $question->answers()->create([ + 'answer' => 'False', + 'is_correct' => false, + ]); + } elseif ($type === 'short_answer') { + Answer::factory(rand(1, 5))->create([ + 'question_id' => $question->id, + 'is_correct' => true, + ]); + } + } + } + + Progress::factory(rand(50, 150))->create(); + + Media::factory(rand(50, 150))->create(); } }