Skip to content

Commit

Permalink
Added uuid in teams table and corrected other things
Browse files Browse the repository at this point in the history
  • Loading branch information
oliuz committed Jun 4, 2020
1 parent 133f2b5 commit 5e6699f
Show file tree
Hide file tree
Showing 9 changed files with 92 additions and 84 deletions.
37 changes: 36 additions & 1 deletion src/TeamworkTeam.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

namespace Mpociot\Teamwork;

use Ramsey\Uuid\Uuid;
use Illuminate\Support\Str;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Facades\Config;
use Mpociot\Teamwork\Traits\TeamworkTeamTrait;
Expand Down Expand Up @@ -31,7 +33,7 @@ class TeamworkTeam extends Model
/**
* @var array
*/
protected $fillable = ['name', 'owner_id', 'slug'];
protected $fillable = ['uuid', 'name', 'owner_id', 'slug'];

/**
* Creates a new instance of the model.
Expand All @@ -43,4 +45,37 @@ public function __construct(array $attributes = [])
parent::__construct($attributes);
$this->table = Config::get('teamwork.teams_table');
}

/**
*
*/
protected static function boot()
{
parent::boot();

static::creating(function ($model) {
$model->uuid = (string)Uuid::uuid1();
$model->slug = Str::slug($model->name);
$model->owner_id = auth()->user()->getKey();
});

static::updating(function ($model) {
$model->uuid = (string)Uuid::uuid1();
$model->slug = Str::slug($model->name);
});

static::deleting(function($model) {
$userModel = config('teamwork.user_model');
$userModel::where('current_team_id', $model)
->update(['current_team_id' => null]);
});
}

/**
* @return string
*/
public function getRouteKeyName()
{
return 'uuid';
}
}
6 changes: 3 additions & 3 deletions src/Traits/TeamworkTeamInviteTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ trait TeamworkTeamInviteTrait
*
* @return \Illuminate\Database\Eloquent\Relations\HasOne
*/
public function team(): HasOne
public function team()
{
return $this->hasOne(Config::get('teamwork.team_model'), 'id', 'team_id');
}
Expand All @@ -32,7 +32,7 @@ public function team(): HasOne
*
* @return \Illuminate\Database\Eloquent\Relations\HasOne
*/
public function user(): HasOne
public function user()
{
return $this->hasOne(Config::get('teamwork.user_model'), 'email', 'email');
}
Expand All @@ -42,7 +42,7 @@ public function user(): HasOne
*
* @return \Illuminate\Database\Eloquent\Relations\HasOne
*/
public function inviter(): HasOne
public function inviter()
{
return $this->hasOne(Config::get('teamwork.user_model'), 'id', 'user_id');
}
Expand Down
77 changes: 36 additions & 41 deletions src/database/migrations/2016_05_18_000000_teamwork_setup_tables.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,49 +6,46 @@

class TeamworkSetupTables extends Migration
{

/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::table( \Config::get( 'teamwork.users_table' ), function ( Blueprint $table )
{
$table->unsignedBigInteger( 'current_team_id' )->nullable();
} );

Schema::table(config('teamwork.users_table'), function (Blueprint $table) {
$table->unsignedBigInteger('current_team_id')
->nullable()
->after('id');
});

Schema::create( \Config::get( 'teamwork.teams_table' ), function ( Blueprint $table )
{
Schema::create(config('teamwork.teams_table'), function (Blueprint $table) {
$table->bigIncrements('id')->unsigned();
$table->uuid('uuid')->nullable();
$table->unsignedBigInteger('owner_id')->nullable();
$table->string('name')->unique();
$table->string( 'slug' )->unique();
$table->string('slug')->unique();
$table->timestamps();
} );
});

Schema::create( \Config::get( 'teamwork.team_user_table' ), function ( Blueprint $table )
{
Schema::create(config('teamwork.team_user_table'), function (Blueprint $table) {
$table->unsignedBigInteger('user_id');
$table->unsignedBigInteger('team_id');
$table->timestamps();

$table->foreign( 'user_id' )
->references( \Config::get( 'teamwork.user_foreign_key' ) )
->on( \Config::get( 'teamwork.users_table' ) )
->onUpdate( 'cascade' )
->onDelete( 'cascade' );
$table->foreign('user_id')
->references(config('teamwork.user_foreign_key'))
->on(config('teamwork.users_table'))
->onUpdate('cascade')
->onDelete('cascade');

$table->foreign( 'team_id' )
->references( 'id' )
->on( \Config::get( 'teamwork.teams_table' ) )
->onDelete( 'cascade' );
} );
$table->foreign('team_id')
->references('id')
->on(config('teamwork.teams_table'))
->onDelete('cascade');
});

Schema::create( \Config::get( 'teamwork.team_invites_table' ), function(Blueprint $table)
{
Schema::create(config('teamwork.team_invites_table'), function (Blueprint $table) {
$table->bigIncrements('id');
$table->unsignedBigInteger('user_id');
$table->unsignedBigInteger('team_id');
Expand All @@ -57,13 +54,13 @@ public function up()
$table->string('accept_token');
$table->string('deny_token');
$table->timestamps();
$table->foreign( 'team_id' )
->references( 'id' )
->on( \Config::get( 'teamwork.teams_table' ) )
->onDelete( 'cascade' );
$table->foreign( 'user_id' )
->references( 'id' )->on('users')
->onDelete( 'cascade' );
$table->foreign('team_id')
->references('id')
->on(config('teamwork.teams_table'))
->onDelete('cascade');
$table->foreign('user_id')
->references('id')->on('users')
->onDelete('cascade');
});
}

Expand All @@ -74,19 +71,17 @@ public function up()
*/
public function down()
{
Schema::table(\Config::get( 'teamwork.users_table' ), function(Blueprint $table)
{
Schema::table(config('teamwork.users_table'), function (Blueprint $table) {
$table->dropColumn('current_team_id');
});

Schema::table(\Config::get('teamwork.team_user_table'), function (Blueprint $table) {
$table->dropForeign(\Config::get('teamwork.team_user_table').'_user_id_foreign');
$table->dropForeign(\Config::get('teamwork.team_user_table').'_team_id_foreign');
Schema::table(config('teamwork.team_user_table'), function (Blueprint $table) {
$table->dropForeign(config('teamwork.team_user_table') . '_user_id_foreign');
$table->dropForeign(config('teamwork.team_user_table') . '_team_id_foreign');
});

Schema::drop(\Config::get('teamwork.team_user_table'));
Schema::drop(\Config::get('teamwork.team_invites_table'));
Schema::drop(\Config::get('teamwork.teams_table'));

Schema::drop(config('teamwork.team_user_table'));
Schema::drop(config('teamwork.team_invites_table'));
Schema::drop(config('teamwork.teams_table'));
}
}
}
37 changes: 9 additions & 28 deletions src/stubs/controllers/TeamController.stub
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

namespace {{namespace}}Http\Controllers\Teamwork;

use Illuminate\Support\Str;
use Mpociot\Teamwork\TeamworkTeam;
use App\Http\Controllers\Controller;
use {{namespace}}Http\Requests\Teamwork\TeamStoreRequest;
Expand Down Expand Up @@ -44,14 +43,7 @@ class TeamController extends Controller
*/
public function store(TeamStoreRequest $request)
{
$teamModel = config('teamwork.team_model');

$team = $teamModel::create([
'name' => $request->name,
'slug' => Str::slug($request->name),
'owner_id' => $request->user()->getKey()
]);
$team->slug = Str::slug($request->get('name'));
$team = TeamworkTeam::create($request->validated());
$request->user()->attachTeam($team);

return redirect(route('teams.index'))->with('success', 'Team created successfully!');
Expand All @@ -65,9 +57,6 @@ class TeamController extends Controller
*/
public function edit(TeamworkTeam $team)
{
$teamModel = config('teamwork.team_model');
$team = $teamModel::findOrFail($id);

if (!auth()->user()->isOwnerOfTeam($team)) {
abort(403);
}
Expand All @@ -84,34 +73,26 @@ class TeamController extends Controller
*/
public function update(TeamUpdateRequest $request, TeamworkTeam $team)
{
$teamModel = config('teamwork.team_model');

$team = $teamModel::findOrFail($id);
$team->name = $request->name;
$team->slug = Str::slug($request->name);
$team->save();
$team->update($request->validated());

return redirect(route('teams.index'))->with('success', 'Team updated successfully!');
}

/**
* Remove the specified resource from storage.
*
* @param TeamworkTeam $team
* @param TeamworkTeam $team
* @return \Illuminate\Routing\Redirector
* @throws \Exception
*/
public function destroy(TeamworkTeam $team)
{
if (!auth()->user()->isOwnerOfTeam($team)) {
abort(403);
}

$team->delete();
if (!auth()->user()->isOwnerOfTeam($team)) {
abort(403);
}

$userModel = config('teamwork.user_model');
$userModel::where('current_team_id', $id)
->update(['current_team_id' => null]);
$team->delete();

return redirect(route('teams.index'))->with('success', 'Team has deleted successfully!');
return redirect(route('teams.index'))->with('success', 'Team has deleted successfully!');
}
}
3 changes: 2 additions & 1 deletion src/stubs/controllers/TeamInviteController.stub
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace {{namespace}}Http\Controllers\Teamwork;

use Mpociot\Teamwork\TeamInvite;
use Mpociot\Teamwork\TeamworkTeam;
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\Mail;
Expand Down Expand Up @@ -35,7 +36,7 @@ class TeamInviteController extends Controller
]);
}

return redirect(route('teams.members.show', $team->id))->with('success', "The invitation for the {$request->email} has been sent successfully");
return redirect(route('teams.members.show', $team))->with('success', "The invitation for the {$request->email} has been sent successfully");
}

/**
Expand Down
2 changes: 1 addition & 1 deletion src/stubs/controllers/TeamMemberController.stub
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,6 @@ class TeamMemberController extends Controller

$user->detachTeam($team);

return redirect(route('teams.index'))->with('success', 'Team Member has been deleted successfully!');
return redirect(route('teams.members.show', $team))->with('success', 'Team Member has been deleted successfully!');
}
}
1 change: 0 additions & 1 deletion src/stubs/routes.stub
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@


/**
* Teamwork routes
*/
Expand Down
4 changes: 2 additions & 2 deletions src/stubs/views/index.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,13 +43,13 @@
@endif
</td>
<td>
<a href="{{route('teams.members.show', $team)}}" class="btn btn-sm btn-dark">
<a href="{{ route('teams.members.show', $team) }}" class="btn btn-sm btn-dark">
<i class="fa fa-users"></i> Members
</a>

@if($team->isOwnerAuth())

<a href="{{route('teams.edit', $team)}}" class="btn btn-sm btn-primary">
<a href="{{ route('teams.edit', $team) }}" class="btn btn-sm btn-primary">
<i class="fa fa-pencil"></i> Edit
</a>

Expand Down
9 changes: 3 additions & 6 deletions src/stubs/views/members/list.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -97,18 +97,15 @@
</td>
<td>
<a href="{{ route('teams.members.resend_invite', $invite) }}"
class="btn btn-sm btn-default">
class="btn btn-sm btn-secondary">
<i class="fa fa-envelope-o"></i>
Resend invite
</a>
<form style="display: inline-block;"
action="{{ route('teams.members.invite_destroy', $invite) }}"
method="POST">
@csrf @method('DELETE')
<button class="btn btn-link btn-sm text-black-50"
data-toggle="tooltip"
data-placement="top"
title="Delete Invite">
<button class="btn btn-danger btn-sm">
<i class="fas fa-trash"></i>
Delete Invite
</button>
Expand All @@ -128,4 +125,4 @@ class="btn btn-sm btn-default">
</div>
@endif
</div>
@endsection
@endsection

0 comments on commit 5e6699f

Please sign in to comment.