some work on models

pull/26/head
Ondřej Hruška 7 years ago
parent 6fd1d7eb89
commit 7493d3e176
Signed by: MightyPork
GPG Key ID: 2C5FD5035250423D
  1. 29
      app/Models/Concerns/NotificationContext.php
  2. 28
      app/Models/Concerns/Reportable.php
  3. 20
      app/Models/Proposal.php
  4. 10
      app/Models/Revision.php
  5. 15
      app/Models/Table.php
  6. 5
      app/Models/TableComment.php
  7. 31
      app/Models/User.php

@ -0,0 +1,29 @@
<?php
namespace App\Models\Concerns;
use App\Models\ContentReport;
use App\Models\Notification;
use Illuminate\Database\Eloquent\Relations\MorphMany;
trait NotificationContext
{
public function bootNotificationContext()
{
static::deleting(function(NotificationContext $self) {
$self->notificationsAbout()->delete();
});
}
/**
* Notifications having this model as a context
*
* @return MorphMany
*/
public function notificationsAbout()
{
return $this->morphMany(Notification::class, 'context');
}
}

@ -0,0 +1,28 @@
<?php
namespace App\Models\Concerns;
use App\Models\ContentReport;
use Illuminate\Database\Eloquent\Relations\MorphMany;
trait Reportable
{
public function bootReportable()
{
static::deleting(function(Reportable $self) {
$self->reportsOf()->delete();
});
}
/**
* Reports of this user
*
* @return MorphMany
*/
public function reportsOf()
{
return $this->morphMany(ContentReport::class, 'object');
}
}

@ -2,13 +2,33 @@
namespace App\Models;
use App\Models\Concerns\NotificationContext;
use App\Models\Concerns\Reportable;
use Illuminate\Database\Eloquent\Collection;
use Illuminate\Database\Eloquent\Model;
/**
* Change proposal
*
* @property Row[]|Collection $addedRows
* @property Row[]|Collection $removedRows
*/
class Proposal extends Model
{
use Reportable;
use NotificationContext;
protected static function boot()
{
parent::boot();
static::deleting(function(Proposal $self) {
// update refcounts
$self->addedRows()->decrement('refs');
$self->removedRows()->decrement('refs');
});
}
/** Added rows */
public function addedRows()
{

@ -12,6 +12,16 @@ class Revision extends Model
{
use HasRelaquentRelationships;
protected static function boot()
{
parent::boot();
static::deleting(function(Revision $self) {
// update refcounts
$self->rows()->decrement('refs');
});
}
/** Included rows */
public function rows()
{

@ -2,6 +2,8 @@
namespace App\Models;
use App\Models\Concerns\NotificationContext;
use App\Models\Concerns\Reportable;
use Illuminate\Database\Eloquent\Model;
/**
@ -9,6 +11,19 @@ use Illuminate\Database\Eloquent\Model;
*/
class Table extends Model
{
use Reportable;
use NotificationContext;
protected static function boot()
{
parent::boot();
static::deleting(function(Table $self) {
// update refcounts
$self->revisions()->decrement('refs');
});
}
/** Owning user */
public function owner()
{

@ -2,6 +2,8 @@
namespace App\Models;
use App\Models\Concerns\NotificationContext;
use App\Models\Concerns\Reportable;
use Illuminate\Database\Eloquent\Model;
/**
@ -9,6 +11,9 @@ use Illuminate\Database\Eloquent\Model;
*/
class TableComment extends Model
{
use Reportable;
use NotificationContext;
/** Context data table */
public function table()
{

@ -3,14 +3,21 @@
namespace App\Models;
use AdamWathan\EloquentOAuth\OAuthIdentity;
use App\Models\Concerns\Reportable;
use Illuminate\Database\Eloquent\Collection;
use Illuminate\Database\Eloquent\Relations\HasMany;
use Illuminate\Database\Eloquent\Relations\MorphMany;
use Illuminate\Notifications\Notifiable;
use Illuminate\Foundation\Auth\User as Authenticatable;
/**
* A user in the application
*
* @property Proposal[]|Collection $proposals
*/
class User extends Authenticatable
{
use Reportable;
use Notifiable;
/**
@ -31,6 +38,20 @@ class User extends Authenticatable
'password', 'remember_token',
];
protected static function boot()
{
parent::boot();
static::deleting(function(User $self) {
// manually delete proposals to ensure row refcounts are updated
foreach ($self->proposals as $proposal) {
$proposal->delete();
}
$self->notificationsCaused()->delete();
});
}
/** Owned tables */
public function dataTables()
{
@ -78,4 +99,14 @@ class User extends Authenticatable
{
return $this->hasMany(Notification::class);
}
/**
* Notifications caused by this user
*
* @return HasMany
*/
public function notificationsCaused()
{
return $this->hasMany(Notification::class, 'actor_id');
}
}

Loading…
Cancel
Save