add models and relations

pull/26/head
Ondřej Hruška 7 years ago
parent ce88fd3978
commit 34df868d57
Signed by: MightyPork
GPG Key ID: 2C5FD5035250423D
  1. 31
      app/Models/Changeset.php
  2. 23
      app/Models/ContentReport.php
  3. 3
      app/Models/DataRow.php
  4. 42
      app/Models/DataTable.php
  5. 51
      app/Models/Notification.php
  6. 41
      app/Models/TableComment.php
  7. 43
      app/Models/User.php
  8. 2
      database/migrations/2018_07_08_193638_create_data_tables_table.php
  9. 6
      database/migrations/2018_07_12_185431_create_table_comments_table.php
  10. 6
      database/migrations/2018_07_12_185840_create_table_favourites_table.php
  11. 1
      database/migrations/2018_07_12_190830_create_notifications_table.php

@ -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);
}
}

@ -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();
}
}

@ -4,6 +4,9 @@ namespace App\Models;
use Illuminate\Database\Eloquent\Model;
/**
* Row in a data table
*/
class DataRow extends Model
{
/** Parent data table */

@ -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(TableComment::class);
}
/** Users favouriting this table */
public function favouritingUsers()
{
return $this->belongsToMany(User::class, 'table_favourites');
}
/** Users to notify about comments */
public function discussionFollowers()
{
return $this->hasMany(Changeset::class)->where('applied', true);
return $this->belongsToMany(User::class, 'discussion_follows');
}
/** Changesets already merged */
public function pendingChangesets()
/** Reports of this table */
public function reportsOf()
{
return $this->hasMany(Changeset::class)->where('applied', false);
return $this->morphMany(ContentReport::class, 'object');
}
}

@ -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);
}
}

@ -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');
}
}

@ -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);
}
}

@ -27,7 +27,7 @@ class CreateDataTablesTable extends Migration
$table->foreign('owner_id')->references('id')->on('users')
->onDelete('cascade');
$table->foreign('precursor_id')->references('id')->on('data_tables')
$table->foreign('parent_table_id')->references('id')->on('data_tables')
->onDelete('set null');
});
}

@ -4,7 +4,7 @@ use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;
class CreateCommentsTable extends Migration
class CreateTableCommentsTable extends Migration
{
/**
* Run the migrations.
@ -13,7 +13,7 @@ class CreateCommentsTable extends Migration
*/
public function up()
{
Schema::create('comments', function (Blueprint $table) {
Schema::create('table_comments', function (Blueprint $table) {
$table->increments('id');
$table->timestamps();
$table->unsignedInteger('ancestor_id')->index()->nullable();
@ -39,6 +39,6 @@ class CreateCommentsTable extends Migration
*/
public function down()
{
Schema::dropIfExists('comments');
Schema::dropIfExists('table_comments');
}
}

@ -4,7 +4,7 @@ use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;
class CreateFavouritesTable extends Migration
class CreateTableFavouritesTable extends Migration
{
/**
* Run the migrations.
@ -13,7 +13,7 @@ class CreateFavouritesTable extends Migration
*/
public function up()
{
Schema::create('favourites', function (Blueprint $table) {
Schema::create('table_favourites', function (Blueprint $table) {
$table->increments('id');
$table->timestamps();
@ -35,6 +35,6 @@ class CreateFavouritesTable extends Migration
*/
public function down()
{
Schema::dropIfExists('favourites');
Schema::dropIfExists('table_favourites');
}
}

@ -16,6 +16,7 @@ class CreateNotificationsTable extends Migration
Schema::create('notifications', function (Blueprint $table) {
$table->increments('id');
$table->timestamps();
$table->boolean('seen');
$table->unsignedInteger('user_id')->index();
$table->unsignedInteger('actor_id')->index()->nullable(); // who did it
$table->string('action'); // what happened, e.g.: favourited, mentioned, forked, commented

Loading…
Cancel
Save