Skip to content

Commit

Permalink
Merge pull request #9 from NickMous/feature/implement-database-structure
Browse files Browse the repository at this point in the history
Feature/implement database structure
  • Loading branch information
NickMous authored May 27, 2024
2 parents 15ffd86 + 19916c3 commit 24ebb5a
Show file tree
Hide file tree
Showing 36 changed files with 1,107 additions and 8 deletions.
43 changes: 43 additions & 0 deletions app/Console/Commands/GiveRole.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
<?php

namespace App\Console\Commands;

use Illuminate\Console\Command;

class GiveRole extends Command
{
/**
* The name and signature of the console command.
*
* @var string
*/
protected $signature = 'app:give-role';

/**
* The console command description.
*
* @var string
*/
protected $description = 'Give a role to a user';

/**
* Execute the console command.
*/
public function handle()
{
$loop = true;
while ($loop) {
$email = $this->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!');
}
}
40 changes: 40 additions & 0 deletions app/Console/Commands/SeedRoles.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
<?php

namespace App\Console\Commands;

use Illuminate\Console\Command;
use Spatie\Permission\Models\Permission;
use Spatie\Permission\Models\Role;
use Spatie\Permission\PermissionRegistrar;

class SeedRoles extends Command
{
/**
* The name and signature of the console command.
*
* @var string
*/
protected $signature = 'app:seed-roles';

/**
* The console command description.
*
* @var string
*/
protected $description = 'Seed the roles and permissions';

/**
* Execute the console command.
*/
public function handle()
{
app()[PermissionRegistrar::class]->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!');
}
}
27 changes: 27 additions & 0 deletions app/Models/Answer.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?php

namespace App\Models;

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

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');
}
}
36 changes: 36 additions & 0 deletions app/Models/Checkpoint.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
<?php

namespace App\Models;

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

class Checkpoint extends Model
{
use HasFactory;

protected $fillable = [
'name',
'description',
'route_id',
'order',
'latitude',
'longitude',
'address'
];

public function route()
{
return $this->belongsTo(Route::class);
}

public function question()
{
return $this->hasMany(Question::class);
}

public function media()
{
return $this->morphMany(Media::class, 'mediable');
}
}
26 changes: 26 additions & 0 deletions app/Models/Media.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?php

namespace App\Models;

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

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();
}
}
37 changes: 37 additions & 0 deletions app/Models/Progress.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
<?php

namespace App\Models;

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

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);
}
}
33 changes: 33 additions & 0 deletions app/Models/Question.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?php

namespace App\Models;

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

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');
}
}
43 changes: 43 additions & 0 deletions app/Models/Route.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
<?php

namespace App\Models;

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

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');
}
}
11 changes: 11 additions & 0 deletions app/Models/User.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand All @@ -19,6 +20,7 @@ class User extends Authenticatable
use HasTeams;
use Notifiable;
use TwoFactorAuthenticatable;
use HasRoles;

/**
* The attributes that are mass assignable.
Expand Down Expand Up @@ -64,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);
}
}
5 changes: 4 additions & 1 deletion app/Providers/AppServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace App\Providers;

use Illuminate\Support\Facades\Gate;
use Illuminate\Support\ServiceProvider;

class AppServiceProvider extends ServiceProvider
Expand All @@ -19,6 +20,8 @@ public function register(): void
*/
public function boot(): void
{
//
Gate::before(function ($user, $ability) {
return $user->hasRole('admin') ? true : null;
});
}
}
1 change: 1 addition & 0 deletions bootstrap/providers.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,5 @@
App\Providers\AppServiceProvider::class,
App\Providers\FortifyServiceProvider::class,
App\Providers\JetstreamServiceProvider::class,
Spatie\Permission\PermissionServiceProvider::class,
];
6 changes: 3 additions & 3 deletions config/fortify.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -92,7 +92,7 @@

/*
|--------------------------------------------------------------------------
| Fortify Routes Middleware
| Fortify Route Middleware
|--------------------------------------------------------------------------
|
| Here you may specify which middleware Fortify will assign to the routes
Expand Down Expand Up @@ -121,7 +121,7 @@

/*
|--------------------------------------------------------------------------
| Register View Routes
| Register View Route
|--------------------------------------------------------------------------
|
| Here you may specify if the routes returning views should be disabled as
Expand Down
Loading

0 comments on commit 24ebb5a

Please sign in to comment.