add models and relations

This commit is contained in:
2018-07-12 21:56:07 +02:00
parent ce88fd3978
commit 34df868d57
11 changed files with 230 additions and 19 deletions
+31
View File
@@ -4,6 +4,9 @@ namespace App\Models;
use Illuminate\Database\Eloquent\Model;
/**
* A patch describing changes to apply or applied to a data table.
*/
class Changeset extends Model
{
/** Parent data table */
@@ -17,4 +20,32 @@ class Changeset extends Model
{
return $this->belongsTo(User::class, 'author_id');
}
/** Reports of this changeset */
public function reportsOf()
{
return $this->morphMany(ContentReport::class, 'object');
}
/**
* Scope for pending changesets
*
* @param \Illuminate\Database\Eloquent\Builder $query
* @return \Illuminate\Database\Eloquent\Builder
*/
public function scopePending($query)
{
return $query->where('applied', false);
}
/**
* Scope for applied changesets
*
* @param \Illuminate\Database\Eloquent\Builder $query
* @return \Illuminate\Database\Eloquent\Builder
*/
public function scopeApplied($query)
{
return $query->where('applied', true);
}
}
+23
View File
@@ -0,0 +1,23 @@
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Model;
/**
* Report (something objectionable spotted by a user)
*/
class ContentReport extends Model
{
/** Authoring user */
public function author()
{
return $this->belongsTo(User::class, 'author_id');
}
/** Authoring user */
public function object()
{
return $this->morphTo();
}
}
+3
View File
@@ -4,6 +4,9 @@ namespace App\Models;
use Illuminate\Database\Eloquent\Model;
/**
* Row in a data table
*/
class DataRow extends Model
{
/** Parent data table */
+32 -10
View File
@@ -4,8 +4,18 @@ namespace App\Models;
use Illuminate\Database\Eloquent\Model;
/**
* A data table object (referencing changesets and rows)
*/
class DataTable extends Model
{
/**
* The attributes that should be mutated to dates.
*
* @var array
*/
protected $dates = ['deleted_at'];
/** Owning user */
public function owner()
{
@@ -13,15 +23,15 @@ class DataTable extends Model
}
/** Fork precursor */
public function precursor()
public function parentTable()
{
return $this->belongsTo(DataTable::class, 'precursor_id');
return $this->belongsTo(DataTable::class, 'parent_table_id');
}
/** Fork descendants */
public function descendants()
public function forks()
{
return $this->hasMany(DataTable::class, 'precursor_id');
return $this->hasMany(DataTable::class, 'parent_table_id');
}
/** Changesets */
@@ -36,15 +46,27 @@ class DataTable extends Model
return $this->hasMany(DataRow::class);
}
/** Changesets already merged */
public function appliedChangesets()
/** User-submitted comments */
public function comments()
{
return $this->hasMany(Changeset::class)->where('applied', true);
return $this->hasMany(TableComment::class);
}
/** Changesets already merged */
public function pendingChangesets()
/** Users favouriting this table */
public function favouritingUsers()
{
return $this->hasMany(Changeset::class)->where('applied', false);
return $this->belongsToMany(User::class, 'table_favourites');
}
/** Users to notify about comments */
public function discussionFollowers()
{
return $this->belongsToMany(User::class, 'discussion_follows');
}
/** Reports of this table */
public function reportsOf()
{
return $this->morphMany(ContentReport::class, 'object');
}
}
+51
View File
@@ -0,0 +1,51 @@
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Model;
/**
* Notification received by a user
*/
class Notification extends Model
{
/** Recipient user */
public function user()
{
return $this->belongsTo(User::class, 'user_id');
}
/** User who triggered this notification */
public function actor()
{
return $this->belongsTo(User::class, 'actor_id');
}
/** Notification context (what was affected) */
public function context()
{
return $this->morphTo();
}
/**
* Unseen notifications
*
* @param \Illuminate\Database\Eloquent\Builder $query
* @return \Illuminate\Database\Eloquent\Builder
*/
public function scopeUnseen($query)
{
return $query->where('seen', false);
}
/**
* Seen notifications
*
* @param \Illuminate\Database\Eloquent\Builder $query
* @return \Illuminate\Database\Eloquent\Builder
*/
public function scopeSeen($query)
{
return $query->where('seen', true);
}
}
+41
View File
@@ -0,0 +1,41 @@
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Model;
/**
* Comment on a data table
*/
class TableComment extends Model
{
/** Context data table */
public function dataTable()
{
return $this->belongsTo(DataTable::class);
}
/** Parent comment (that we reply to; can be null) */
public function ancestor()
{
return $this->belongsTo(TableComment::class, 'ancestor_id');
}
/** Replies to this comment */
public function replies()
{
return $this->hasMany(TableComment::class, 'ancestor_id');
}
/** Authoring user */
public function author()
{
return $this->belongsTo(User::class, 'author_id');
}
/** Reports of this comment */
public function reportsOf()
{
return $this->morphMany(ContentReport::class, 'object');
}
}
+41 -2
View File
@@ -6,6 +6,9 @@ use AdamWathan\EloquentOAuth\OAuthIdentity;
use Illuminate\Notifications\Notifiable;
use Illuminate\Foundation\Auth\User as Authenticatable;
/**
* A user in the application
*/
class User extends Authenticatable
{
use Notifiable;
@@ -31,7 +34,7 @@ class User extends Authenticatable
/** Owned tables */
public function dataTables()
{
return $this->hasMany(DataTable::class);
return $this->hasMany(DataTable::class, 'owner_id');
}
/** Assigned OAuth identities */
@@ -43,6 +46,42 @@ class User extends Authenticatable
/** Assigned OAuth identities */
public function changesets()
{
return $this->hasMany(Changeset::class);
return $this->hasMany(Changeset::class, 'author_id');
}
/** Authored comments */
public function tableComments()
{
return $this->hasMany(TableComment::class, 'author_id');
}
/** User's favourite tables (personal collection) */
public function favouriteTables()
{
return $this->belongsToMany(DataTable::class, 'table_favourites');
}
/** Tables whose discussions user follows (this is similar to favourites) */
public function followedDiscussions()
{
return $this->belongsToMany(DataTable::class, 'discussion_follows');
}
/** Reports sent by this user */
public function authoredReports()
{
return $this->hasMany(ContentReport::class, 'author_id');
}
/** Reports of this user */
public function reportsOf()
{
return $this->morphMany(ContentReport::class, 'object');
}
/** Notifications for this user */
public function notifications()
{
return $this->hasMany(Notification::class);
}
}