mega-commit: migrations, controllers, models, etc.

This commit is contained in:
Didier Slof 2022-12-08 09:30:07 +01:00
parent 9732135e90
commit 2c6745e812
Signed by: didier
GPG key ID: 01E71F18AA4398E5
70 changed files with 2124 additions and 400 deletions

View file

@ -4,7 +4,7 @@ namespace App\Models;
use Illuminate\Database\Eloquent\Model;
class MovieGenre extends Model
class Genre extends Model
{
protected $table = 'genres';
protected $primaryKey = 'genre_id';
@ -13,9 +13,14 @@ class MovieGenre extends Model
'genre_name',
];
public function findGenreByName($genre_name)
{
return Genre::where('genre_name', $genre_name)->first();
}
public function movies()
{
return $this->belongsToMany(Movie::class, 'movie_genres', 'genre_id', 'movie_id');
return $this->hasMany('App\Models\Movie', 'genre_id');
}
}

View file

@ -24,9 +24,14 @@ class Movie extends Model
'updated_at',
];
public static function findOrfail($id)
{
return Movie::where('movie_id', $id)->firstOrFail();
}
public function genre()
{
return $this->belongsTo(MovieGenre::class, 'genre_id', 'genre_id');
return $this->belongsTo(Genre::class, 'genre_id', 'genre_id');
}
public function showings()

View file

@ -17,11 +17,9 @@ class Permission extends Model
'updated_at',
];
// Permissions are linked to users by the user_permissions table
// User permissions are linked to users by the user_id and are valid per date
public function users()
{
return $this->belongsToMany(User::class, 'user_permissions', 'permission_id', 'user_id');
return $this->belongsToMany('App\Models\User', 'user_permissions', 'permission_id', 'user_id');
}
}

View file

@ -15,6 +15,7 @@ class Room extends Model
'room_rows',
'room_columns',
'user_id', // who added the room?
'cinema_id',
];
protected $hidden = [
@ -22,10 +23,24 @@ class Room extends Model
'updated_at',
];
public static function find(int $room_id)
{
return self::where('room_id', $room_id)->first();
}
public function showings()
{
return $this->hasMany(Showing::class, 'room_id', 'room_id');
}
public function seats()
{
return $this->hasMany(Seat::class, 'room_id', 'room_id');
}
public function cinema()
{
return $this->belongsTo(Cinema::class, 'cinema_id', 'cinema_id');
}
}

View file

@ -32,5 +32,23 @@ class Seat extends Model
return $this->belongsToMany(Order::class, 'order_seats', 'seat_id', 'order_id');
}
public function tickets()
{
return $this->hasMany(Ticket::class, 'seat_id', 'seat_id');
}
// isReserved(int showing_id) method
// Looks at showing / order / ticket if it's reserved
// Returns true if it is reserved, false if it isn't
public function is_reserved(int $showing_id)
{
$tickets = $this->tickets()->where('showing_id', $showing_id)->get();
foreach ($tickets as $ticket) {
if ($ticket->order->order_status == 'pending' || $ticket->order->order_status == 'paid') {
return true;
}
}
return false;
}
}

View file

@ -16,7 +16,6 @@ class Showing extends Model
protected $fillable = [
'movie_id',
'showing_start',
'showing_end',
'room_id', // which room is showing the movie?
'user_id', // who added the showing?
];
@ -26,6 +25,16 @@ class Showing extends Model
'updated_at',
];
public static function findOrfail($id)
{
return Showing::where('showing_id', $id)->firstOrFail();
}
public function room()
{
return $this->belongsTo(Room::class, 'room_id', 'room_id');
}
public function movie()
{
return $this->belongsTo(Movie::class, 'movie_id', 'movie_id');

View file

@ -32,6 +32,4 @@ class Ticket extends Model
return $this->belongsTo(Showing::class, 'showing_id', 'showing_id');
}
}

View file

@ -14,10 +14,6 @@ class User extends Authenticatable
protected $primaryKey = 'user_id';
// User can be a customer or an employee
// If customer, then there'll be a customer_id in the users table
// If employee, then there'll be an employee_id in the users table
/**
* The attributes that are mass assignable.
*
@ -26,10 +22,8 @@ class User extends Authenticatable
protected $fillable = [
'name',
'email',
'password',
'customer_id', // if this is filled, then this user is a customer
'employee_id', // if this is filled, then this user is an employee
// if both are filled, then this user is both a customer and an employee
'role', // default, employee, manage
'password'
];
/**
@ -53,5 +47,43 @@ class User extends Authenticatable
'email_verified_at' => 'datetime',
];
public function permissions()
{
// get permissions for this user
// note: permissions are linked to users by the user_permissions table
// the permissions are in the permissions table
// only return valid permissions
return $this->belongsToMany('App\Models\Permission', 'user_permissions', 'user_id', 'permission_id')->where(function ($query) {
$query->where('user_permission_start', '<=', now())->where(function ($query) {
$query->where('user_permission_end', '>=', now())->orWhereNull('user_permission_end');
});
});
}
public function allowedTo($permission): bool
{
if($this->role === 'manage') {
return true;
}
if ($this->permissions()->where('permission_name', $permission)->first()) {
return true;
}
return false;
}
public function atleast($role): bool
{
$hierarchy = [
'default' => 0,
'employee' => 1,
'manage' => 2
];
return $hierarchy[$this->role] >= $hierarchy[$role];
}
public static function find($id)
{
return User::all()->where('user_id', $id)->first();
}
}