-
-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
#2595 WIP changes done to a route are kept in a separate table, to di…
…splay later in the Teams function.
- Loading branch information
Showing
7 changed files
with
237 additions
and
46 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
<?php | ||
|
||
namespace App\Http\Controllers\Traits; | ||
|
||
use App\Models\DungeonRoute\DungeonRoute; | ||
use App\Models\DungeonRoute\DungeonRouteChange; | ||
use Exception; | ||
use Illuminate\Database\Eloquent\Model; | ||
use Illuminate\Support\Facades\Auth; | ||
|
||
trait ChangesDungeonRoute | ||
{ | ||
private const IGNORE_KEYS = ['updated_at']; | ||
|
||
/** | ||
* @param DungeonRoute $dungeonRoute | ||
* @param Model|null $beforeModel | ||
* @param Model|null $afterModel | ||
* @throws Exception | ||
*/ | ||
public function dungeonRouteChanged(DungeonRoute $dungeonRoute, ?Model $beforeModel, ?Model $afterModel): void | ||
{ | ||
if ($beforeModel === null && $afterModel === null) { | ||
throw new Exception('Must have at least a $beforeModel OR $afterModel'); | ||
} | ||
|
||
$user = Auth::user(); | ||
|
||
$boolToInt = fn($value) => is_bool($value) ? (int)$value : $value; | ||
$beforeAttributes = $beforeModel !== null ? array_map($boolToInt, $beforeModel->getAttributes()) : []; | ||
$afterAttributes = $afterModel !== null ? array_map($boolToInt, $afterModel->getAttributes()) : []; | ||
|
||
$changedKeys = $this->getChangedData($beforeAttributes, $afterAttributes, self::IGNORE_KEYS); | ||
|
||
DungeonRouteChange::create([ | ||
'dungeon_route_id' => $dungeonRoute->id, | ||
'user_id' => $user?->id, | ||
'team_id' => $dungeonRoute->team_id, | ||
'team_role' => $dungeonRoute->team?->getUserRole($user), | ||
'model_id' => $beforeModel?->id ?? $afterModel->id, | ||
'model_class' => ($beforeModel ?? $afterModel)::class, | ||
'before' => $beforeModel !== null ? json_encode(array_intersect_key($beforeAttributes, $changedKeys)) : null, | ||
'after' => $afterModel !== null ? json_encode(array_intersect_key($afterAttributes, $changedKeys)) : null, | ||
]); | ||
} | ||
|
||
private function getChangedData(array $before, array $after, array $excludeKeys = []): array | ||
{ | ||
$alteredKeys = []; | ||
foreach ($before as $key => $value) { | ||
if (in_array($key, $excludeKeys)) { | ||
continue; | ||
} | ||
|
||
if ($value !== $after[$key]) { | ||
$alteredKeys[$key] = [ | ||
'before' => $value, | ||
'after' => $after[$key], | ||
]; | ||
} | ||
} | ||
|
||
return $alteredKeys; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
<?php | ||
|
||
namespace App\Models\DungeonRoute; | ||
|
||
use App\Models\Team; | ||
use App\Models\Traits\HasGenericModelRelation; | ||
use App\Models\User; | ||
use Eloquent; | ||
use Illuminate\Database\Eloquent\Model; | ||
use Illuminate\Database\Eloquent\Relations\BelongsTo; | ||
use Illuminate\Support\Carbon; | ||
|
||
/** | ||
* @property int $id | ||
* @property int $dungeon_route_id | ||
* @property int|null $user_id | ||
* @property int|null $team_id | ||
* @property int|null $team_role | ||
* @property int $model_id | ||
* @property string $model_class | ||
* @property array|null $before | ||
* @property array|null $after | ||
* | ||
* @property Carbon|null $created_at | ||
* @property Carbon|null $updated_at | ||
* | ||
* @mixin Eloquent | ||
*/ | ||
class DungeonRouteChange extends Model | ||
{ | ||
use HasGenericModelRelation; | ||
|
||
protected $fillable = [ | ||
'dungeon_route_id', | ||
'user_id', | ||
'team_id', | ||
'team_role', | ||
'model_class', | ||
'before', | ||
'after', | ||
]; | ||
|
||
protected $casts = [ | ||
'before' => 'array', | ||
'after' => 'array', | ||
]; | ||
|
||
public function dungeonRoute(): BelongsTo | ||
{ | ||
return $this->belongsTo(DungeonRoute::class); | ||
} | ||
|
||
public function user(): BelongsTo | ||
{ | ||
return $this->belongsTo(User::class); | ||
} | ||
|
||
public function team(): BelongsTo | ||
{ | ||
return $this->belongsTo(Team::class); | ||
} | ||
} |
Oops, something went wrong.