From 6fd1d7eb897cea23dbf8246c249a830d3d9a4c60 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ond=C5=99ej=20Hru=C5=A1ka?= Date: Sat, 14 Jul 2018 12:24:24 +0200 Subject: [PATCH] updated models --- app/Models/Changeset.php | 51 ---------------- app/Models/DataRow.php | 17 ------ app/Models/Proposal.php | 41 +++++++++++++ app/Models/Revision.php | 44 ++++++++++++++ app/Models/Row.php | 12 ++++ app/Models/{DataTable.php => Table.php} | 37 +++++------- app/Models/TableComment.php | 10 +--- app/Models/User.php | 18 ++---- composer.json | 3 +- composer.lock | 59 ++++++++++++++++++- database/factories/DataTableFactory.php | 2 +- ...18_07_08_193600_create_revisions_table.php | 4 -- ...reate_proposal_remove_row_pivot_table.php} | 6 +- ...5_create_revision_proposal_pivot_table.php | 37 ++++++++++++ ...2_185840_create_table_favourites_table.php | 7 +-- ...190059_create_discussion_follows_table.php | 6 +- 16 files changed, 225 insertions(+), 129 deletions(-) delete mode 100644 app/Models/Changeset.php delete mode 100644 app/Models/DataRow.php create mode 100644 app/Models/Proposal.php create mode 100644 app/Models/Revision.php create mode 100644 app/Models/Row.php rename app/Models/{DataTable.php => Table.php} (58%) rename database/migrations/{2018_07_08_194100_create_proposal_delete_row_pivot_table.php => 2018_07_08_194100_create_proposal_remove_row_pivot_table.php} (80%) create mode 100644 database/migrations/2018_07_08_194105_create_revision_proposal_pivot_table.php diff --git a/app/Models/Changeset.php b/app/Models/Changeset.php deleted file mode 100644 index 770256f..0000000 --- a/app/Models/Changeset.php +++ /dev/null @@ -1,51 +0,0 @@ -belongsTo(DataTable::class); - } - - /** Author who created it user */ - public function author() - { - 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); - } -} diff --git a/app/Models/DataRow.php b/app/Models/DataRow.php deleted file mode 100644 index 1b01f77..0000000 --- a/app/Models/DataRow.php +++ /dev/null @@ -1,17 +0,0 @@ -belongsTo(DataTable::class); - } -} diff --git a/app/Models/Proposal.php b/app/Models/Proposal.php new file mode 100644 index 0000000..4f99837 --- /dev/null +++ b/app/Models/Proposal.php @@ -0,0 +1,41 @@ +belongsToMany(Row::class, 'proposal_add_row_pivot'); + } + + /** Removed rows */ + public function removedRows() + { + return $this->belongsToMany(Row::class, 'proposal_remove_row_pivot'); + } + + /** Authoring user */ + public function author() + { + return $this->belongsTo(User::class, 'author_id'); + } + + /** Target revision */ + public function revision() + { + return $this->belongsTo(Revision::class); + } + + /** Target table (that this was submitted to) */ + public function table() + { + return $this->belongsTo(Table::class); + } +} diff --git a/app/Models/Revision.php b/app/Models/Revision.php new file mode 100644 index 0000000..d1c1a8e --- /dev/null +++ b/app/Models/Revision.php @@ -0,0 +1,44 @@ +belongsToMany(Row::class, 'revision_row_pivot'); + } + + /** Proposal that lead to this revision */ + public function appliedProposal() + { + return $this->hasOneThrough(Proposal::class, 'revision_proposal_pivot'); + } + + /** Proposals that depend on this revision */ + public function dependentProposals() + { + return $this->hasMany(Proposal::class); + } + + /** Revision this orignates from */ + public function parentRevision() + { + return $this->belongsTo(Revision::class, 'ancestor_id'); + } + + /** Tables referencing this revision */ + public function tables() + { + return $this->belongsToMany(Table::class, 'table_revision_pivot'); + } +} diff --git a/app/Models/Row.php b/app/Models/Row.php new file mode 100644 index 0000000..3b5a9b1 --- /dev/null +++ b/app/Models/Row.php @@ -0,0 +1,12 @@ +belongsTo(DataTable::class, 'parent_table_id'); + return $this->belongsTo(Table::class, 'ancestor_id'); } /** Fork descendants */ public function forks() { - return $this->hasMany(DataTable::class, 'parent_table_id'); + return $this->hasMany(Table::class, 'ancestor_id'); + } + + /** All related revisions (this may include merged revisions) */ + public function revisions() + { + return $this->belongsToMany(Revision::class, 'table_revision_pivot'); } - /** Changesets */ - public function changesets() + /** Active revision */ + public function activeRevision() { - return $this->hasMany(Changeset::class); + return $this->hasOne(Revision::class, 'revision_id'); } - /** Data rows */ - public function dataRows() + /** Proposals submitted to this table */ + public function proposals() { - return $this->hasMany(DataRow::class); + return $this->hasMany(Proposal::class); } /** User-submitted comments */ @@ -63,10 +62,4 @@ class DataTable extends Model { return $this->belongsToMany(User::class, 'discussion_follows'); } - - /** Reports of this table */ - public function reportsOf() - { - return $this->morphMany(ContentReport::class, 'object'); - } } diff --git a/app/Models/TableComment.php b/app/Models/TableComment.php index 0827132..255679d 100644 --- a/app/Models/TableComment.php +++ b/app/Models/TableComment.php @@ -10,9 +10,9 @@ use Illuminate\Database\Eloquent\Model; class TableComment extends Model { /** Context data table */ - public function dataTable() + public function table() { - return $this->belongsTo(DataTable::class); + return $this->belongsTo(Table::class); } /** Parent comment (that we reply to; can be null) */ @@ -32,10 +32,4 @@ class TableComment extends Model { return $this->belongsTo(User::class, 'author_id'); } - - /** Reports of this comment */ - public function reportsOf() - { - return $this->morphMany(ContentReport::class, 'object'); - } } diff --git a/app/Models/User.php b/app/Models/User.php index fdfb40e..89b6cfe 100644 --- a/app/Models/User.php +++ b/app/Models/User.php @@ -34,7 +34,7 @@ class User extends Authenticatable /** Owned tables */ public function dataTables() { - return $this->hasMany(DataTable::class, 'owner_id'); + return $this->hasMany(Table::class, 'owner_id'); } /** Assigned OAuth identities */ @@ -43,10 +43,10 @@ class User extends Authenticatable return $this->hasMany(OAuthIdentity::class); } - /** Assigned OAuth identities */ - public function changesets() + /** Authored proposals */ + public function proposals() { - return $this->hasMany(Changeset::class, 'author_id'); + return $this->hasMany(Proposal::class, 'author_id'); } /** Authored comments */ @@ -58,13 +58,13 @@ class User extends Authenticatable /** User's favourite tables (personal collection) */ public function favouriteTables() { - return $this->belongsToMany(DataTable::class, 'table_favourites'); + return $this->belongsToMany(Table::class, 'table_favourites'); } /** Tables whose discussions user follows (this is similar to favourites) */ public function followedDiscussions() { - return $this->belongsToMany(DataTable::class, 'discussion_follows'); + return $this->belongsToMany(Table::class, 'discussion_follows'); } /** Reports sent by this user */ @@ -73,12 +73,6 @@ class User extends Authenticatable 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() { diff --git a/composer.json b/composer.json index 741a0ec..9da249e 100644 --- a/composer.json +++ b/composer.json @@ -12,11 +12,12 @@ "barryvdh/laravel-ide-helper": "^2.4", "doctrine/dbal": "^2.7", "fideloper/proxy": "^4.0", + "guzzlehttp/guzzle": "^6.0", "laravel/framework": "5.6.*", "laravel/socialite": "^3.0", "laravel/tinker": "^1.0", "phpoffice/phpspreadsheet": "^1.3", - "guzzlehttp/guzzle": "^6.0" + "riesjart/relaquent": "^0.1" }, "require-dev": { "filp/whoops": "^2.0", diff --git a/composer.lock b/composer.lock index 2f930d9..bba5768 100644 --- a/composer.lock +++ b/composer.lock @@ -4,7 +4,7 @@ "Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies", "This file is @generated automatically" ], - "content-hash": "cdb2e12195da014433178600b1a1899f", + "content-hash": "7c2b00ef303415cdc522f82578d90676", "packages": [ { "name": "barryvdh/laravel-ide-helper", @@ -2187,6 +2187,63 @@ ], "time": "2018-01-20T00:28:24+00:00" }, + { + "name": "riesjart/relaquent", + "version": "v0.1.1", + "source": { + "type": "git", + "url": "https://github.com/riesjart/relaquent.git", + "reference": "feb8e01cc818b25770dad792b6415b6aea9e3a1d" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/riesjart/relaquent/zipball/feb8e01cc818b25770dad792b6415b6aea9e3a1d", + "reference": "feb8e01cc818b25770dad792b6415b6aea9e3a1d", + "shasum": "" + }, + "require": { + "illuminate/cache": "5.6.*", + "illuminate/container": "5.6.*", + "illuminate/database": "~5.6.3", + "illuminate/support": "5.6.*", + "php": ">=7.1" + }, + "type": "library", + "autoload": { + "psr-4": { + "Riesjart\\Relaquent\\": "src/" + } + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "MIT" + ], + "authors": [ + { + "name": "Richard van Baarsen", + "email": "richardvanbaarsen@gmail.com" + } + ], + "description": "Extension of Laravel's Eloquent relationships: additional relationship types, query joins, pivot models, converters and helpers", + "keywords": [ + "belongs-to-morph", + "converter", + "database", + "eloquent", + "has-one-through", + "illuminate", + "join", + "laravel", + "model", + "morph-one-through", + "php", + "pivot", + "relation", + "relationship", + "sql" + ], + "time": "2018-02-22T10:42:19+00:00" + }, { "name": "swiftmailer/swiftmailer", "version": "v6.1.1", diff --git a/database/factories/DataTableFactory.php b/database/factories/DataTableFactory.php index e38a7dc..2e2bb70 100644 --- a/database/factories/DataTableFactory.php +++ b/database/factories/DataTableFactory.php @@ -2,7 +2,7 @@ use Faker\Generator as Faker; -$factory->define(\App\Models\DataTable::class, function (Faker $faker) { +$factory->define(\App\Models\Table::class, function (Faker $faker) { return [ // ]; diff --git a/database/migrations/2018_07_08_193600_create_revisions_table.php b/database/migrations/2018_07_08_193600_create_revisions_table.php index 8932af9..2f3d3bd 100644 --- a/database/migrations/2018_07_08_193600_create_revisions_table.php +++ b/database/migrations/2018_07_08_193600_create_revisions_table.php @@ -17,7 +17,6 @@ class CreateRevisionsTable extends Migration $table->increments('id'); $table->timestamps(); $table->unsignedInteger('refs'); // used for reference counting - $table->unsignedInteger('author_id')->index(); $table->unsignedInteger('ancestor_id')->index()->nullable(); // parent revision // a column that can be used as the primary key for a table; possibly a composite column if using a comma @@ -26,9 +25,6 @@ class CreateRevisionsTable extends Migration // author's note describing what has been changed $table->text('note'); - $table->foreign('author_id')->references('id')->on('users') - ->onDelete('set null'); - $table->foreign('ancestor_id')->references('id')->on('revisions') ->onDelete('set null'); }); diff --git a/database/migrations/2018_07_08_194100_create_proposal_delete_row_pivot_table.php b/database/migrations/2018_07_08_194100_create_proposal_remove_row_pivot_table.php similarity index 80% rename from database/migrations/2018_07_08_194100_create_proposal_delete_row_pivot_table.php rename to database/migrations/2018_07_08_194100_create_proposal_remove_row_pivot_table.php index 9f2f93c..64ad5ac 100644 --- a/database/migrations/2018_07_08_194100_create_proposal_delete_row_pivot_table.php +++ b/database/migrations/2018_07_08_194100_create_proposal_remove_row_pivot_table.php @@ -4,7 +4,7 @@ use Illuminate\Support\Facades\Schema; use Illuminate\Database\Schema\Blueprint; use Illuminate\Database\Migrations\Migration; -class CreateProposalDeleteRowPivotTable extends Migration +class CreateProposalRemoveRowPivotTable extends Migration { /** * Run the migrations. @@ -13,7 +13,7 @@ class CreateProposalDeleteRowPivotTable extends Migration */ public function up() { - Schema::create('proposal_delete_row_pivot', function (Blueprint $table) { + Schema::create('proposal_remove_row_pivot', function (Blueprint $table) { $table->unsignedInteger('proposal_id')->index(); $table->unsignedInteger('row_id')->index(); @@ -32,6 +32,6 @@ class CreateProposalDeleteRowPivotTable extends Migration */ public function down() { - Schema::dropIfExists('proposal_delete_row_pivot'); + Schema::dropIfExists('proposal_remove_row_pivot'); } } diff --git a/database/migrations/2018_07_08_194105_create_revision_proposal_pivot_table.php b/database/migrations/2018_07_08_194105_create_revision_proposal_pivot_table.php new file mode 100644 index 0000000..bb31e86 --- /dev/null +++ b/database/migrations/2018_07_08_194105_create_revision_proposal_pivot_table.php @@ -0,0 +1,37 @@ +unsignedInteger('proposal_id')->index(); + $table->unsignedInteger('revision_id')->index(); + + $table->foreign('proposal_id')->references('id')->on('proposals') + ->onDelete('cascade'); + + $table->foreign('revision_id')->references('id')->on('revisions') + ->onDelete('cascade'); + }); + } + + /** + * Reverse the migrations. + * + * @return void + */ + public function down() + { + Schema::dropIfExists('revision_proposal_pivot'); + } +} diff --git a/database/migrations/2018_07_12_185840_create_table_favourites_table.php b/database/migrations/2018_07_12_185840_create_table_favourites_table.php index 181feb1..50f5c35 100644 --- a/database/migrations/2018_07_12_185840_create_table_favourites_table.php +++ b/database/migrations/2018_07_12_185840_create_table_favourites_table.php @@ -14,11 +14,8 @@ class CreateTableFavouritesTable extends Migration public function up() { Schema::create('table_favourites', function (Blueprint $table) { - $table->increments('id'); - $table->timestamps(); - - $table->unsignedInteger('user_id'); - $table->unsignedInteger('table_id'); + $table->unsignedInteger('user_id')->index(); + $table->unsignedInteger('table_id')->index(); $table->foreign('user_id')->references('id')->on('users') ->onDelete('cascade'); diff --git a/database/migrations/2018_07_12_190059_create_discussion_follows_table.php b/database/migrations/2018_07_12_190059_create_discussion_follows_table.php index 8c6b6f3..736323b 100644 --- a/database/migrations/2018_07_12_190059_create_discussion_follows_table.php +++ b/database/migrations/2018_07_12_190059_create_discussion_follows_table.php @@ -14,10 +14,8 @@ class CreateDiscussionFollowsTable extends Migration public function up() { Schema::create('discussion_follows', function (Blueprint $table) { - $table->increments('id'); - $table->timestamps(); - $table->unsignedInteger('user_id'); - $table->unsignedInteger('table_id'); + $table->unsignedInteger('user_id')->index(); + $table->unsignedInteger('table_id')->index(); $table->foreign('user_id')->references('id')->on('users') ->onDelete('cascade');