Skip to content

Commit

Permalink
feat: migrating theme groups to an appropriate table (#611) (#659)
Browse files Browse the repository at this point in the history
  • Loading branch information
Kyrch authored Apr 28, 2024
1 parent 99153cb commit e472a1d
Show file tree
Hide file tree
Showing 36 changed files with 1,627 additions and 256 deletions.
68 changes: 68 additions & 0 deletions app/Events/Wiki/Group/GroupCreated.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
<?php

declare(strict_types=1);

namespace App\Events\Wiki\Group;

use App\Contracts\Events\UpdateRelatedIndicesEvent;
use App\Events\Base\Wiki\WikiCreatedEvent;
use App\Models\Wiki\Anime\AnimeTheme;
use App\Models\Wiki\Anime\Theme\AnimeThemeEntry;
use App\Models\Wiki\Group;
use App\Models\Wiki\Video;

/**
* Class GroupCreated.
*
* @extends WikiCreatedEvent<Group>
*/
class GroupCreated extends WikiCreatedEvent implements UpdateRelatedIndicesEvent
{
/**
* Create a new event instance.
*
* @param Group $group
*/
public function __construct(Group $group)
{
parent::__construct($group);
}

/**
* Get the model that has fired this event.
*
* @return Group
*/
public function getModel(): Group
{
return $this->model;
}

/**
* Get the description for the Discord message payload.
*
* @return string
*/
protected function getDiscordMessageDescription(): string
{
return "Group '**{$this->getModel()->getName()}**' has been created.";
}

/**
* Perform updates on related indices.
*
* @return void
*/
public function updateRelatedIndices(): void
{
$group = $this->getModel()->load(Group::RELATION_VIDEOS);

$group->animethemes->each(function (AnimeTheme $theme) {
$theme->searchable();
$theme->animethemeentries->each(function (AnimeThemeEntry $entry) {
$entry->searchable();
$entry->videos->each(fn (Video $video) => $video->searchable());
});
});
}
}
91 changes: 91 additions & 0 deletions app/Events/Wiki/Group/GroupDeleted.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
<?php

declare(strict_types=1);

namespace App\Events\Wiki\Group;

use App\Contracts\Events\UpdateRelatedIndicesEvent;
use App\Events\Base\Wiki\WikiDeletedEvent;
use App\Models\Wiki\Anime\AnimeTheme;
use App\Models\Wiki\Anime\Theme\AnimeThemeEntry;
use App\Models\Wiki\Group;
use App\Models\Wiki\Video;
use App\Nova\Resources\Wiki\Group as GroupResource;

/**
* Class GroupDeleted.
*
* @extends WikiDeletedEvent<Group>
*/
class GroupDeleted extends WikiDeletedEvent implements UpdateRelatedIndicesEvent
{
/**
* Create a new event instance.
*
* @param Group $group
*/
public function __construct(Group $group)
{
parent::__construct($group);
}

/**
* Get the model that has fired this event.
*
* @return Group
*/
public function getModel(): Group
{
return $this->model;
}

/**
* Get the description for the Discord message payload.
*
* @return string
*/
protected function getDiscordMessageDescription(): string
{
return "Group '**{$this->getModel()->getName()}**' has been deleted.";
}

/**
* Get the message for the nova notification.
*
* @return string
*/
protected function getNotificationMessage(): string
{
return "Group '{$this->getModel()->getName()}' has been deleted. It will be automatically pruned in one week. Please review.";
}

/**
* Get the URL for the nova notification.
*
* @return string
*/
protected function getNovaNotificationUrl(): string
{
$uriKey = GroupResource::uriKey();

return "/resources/$uriKey/{$this->getModel()->getKey()}";
}

/**
* Perform updates on related indices.
*
* @return void
*/
public function updateRelatedIndices(): void
{
$group = $this->getModel()->load(Group::RELATION_VIDEOS);

$group->animethemes->each(function (AnimeTheme $theme) {
$theme->searchable();
$theme->animethemeentries->each(function (AnimeThemeEntry $entry) {
$entry->searchable();
$entry->videos->each(fn (Video $video) => $video->searchable());
});
});
}
}
64 changes: 64 additions & 0 deletions app/Events/Wiki/Group/GroupDeleting.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
<?php

declare(strict_types=1);

namespace App\Events\Wiki\Group;

use App\Contracts\Events\UpdateRelatedIndicesEvent;
use App\Events\BaseEvent;
use App\Models\Wiki\Anime\AnimeTheme;
use App\Models\Wiki\Anime\Theme\AnimeThemeEntry;
use App\Models\Wiki\Group;
use App\Models\Wiki\Video;

/**
* Class GroupDeleting.
*
* @extends BaseEvent<Group>
*/
class GroupDeleting extends BaseEvent implements UpdateRelatedIndicesEvent
{
/**
* Create a new event instance.
*
* @param Group $group
*/
public function __construct(Group $group)
{
parent::__construct($group);
}

/**
* Get the model that has fired this event.
*
* @return Group
*/
public function getModel(): Group
{
return $this->model;
}

/**
* Perform cascading deletes.
*
* @return void
*/
public function updateRelatedIndices(): void
{
$group = $this->getModel()->load(Group::RELATION_VIDEOS);

if ($group->isForceDeleting()) {
$group->animethemes->each(function (AnimeTheme $theme) {
AnimeTheme::withoutEvents(function () use ($theme) {
$theme->theme_group()->dissociate();
$theme->save();
});
$theme->searchable();
$theme->animethemeentries->each(function (AnimeThemeEntry $entry) {
$entry->searchable();
$entry->videos->each(fn (Video $video) => $video->searchable());
});
});
}
}
}
68 changes: 68 additions & 0 deletions app/Events/Wiki/Group/GroupRestored.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
<?php

declare(strict_types=1);

namespace App\Events\Wiki\Group;

use App\Contracts\Events\UpdateRelatedIndicesEvent;
use App\Events\Base\Wiki\WikiRestoredEvent;
use App\Models\Wiki\Anime\AnimeTheme;
use App\Models\Wiki\Anime\Theme\AnimeThemeEntry;
use App\Models\Wiki\Group;
use App\Models\Wiki\Video;

/**
* Class GroupRestored.
*
* @extends WikiRestoredEvent<Group>
*/
class GroupRestored extends WikiRestoredEvent implements UpdateRelatedIndicesEvent
{
/**
* Create a new event instance.
*
* @param Group $group
*/
public function __construct(Group $group)
{
parent::__construct($group);
}

/**
* Get the model that has fired this event.
*
* @return Group
*/
public function getModel(): Group
{
return $this->model;
}

/**
* Get the description for the Discord message payload.
*
* @return string
*/
protected function getDiscordMessageDescription(): string
{
return "Group '**{$this->getModel()->getName()}**' has been restored.";
}

/**
* Perform cascading deletes.
*
* @return void
*/
public function updateRelatedIndices(): void
{
$group = $this->getModel()->load(Group::RELATION_VIDEOS);

$group->animethemes->each(function (AnimeTheme $theme) {
$theme->searchable();
$theme->animethemeentries->each(function (AnimeThemeEntry $entry) {
$entry->searchable();
$entry->videos->each(fn (Video $video) => $video->searchable());
});
});
}
}
69 changes: 69 additions & 0 deletions app/Events/Wiki/Group/GroupUpdated.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
<?php

declare(strict_types=1);

namespace App\Events\Wiki\Group;

use App\Contracts\Events\UpdateRelatedIndicesEvent;
use App\Events\Base\Wiki\WikiUpdatedEvent;
use App\Models\Wiki\Anime\AnimeTheme;
use App\Models\Wiki\Anime\Theme\AnimeThemeEntry;
use App\Models\Wiki\Group;
use App\Models\Wiki\Video;

/**
* Class GroupUpdated.
*
* @extends WikiUpdatedEvent<Group>
*/
class GroupUpdated extends WikiUpdatedEvent implements UpdateRelatedIndicesEvent
{
/**
* Create a new event instance.
*
* @param Group $group
*/
public function __construct(Group $group)
{
parent::__construct($group);
$this->initializeEmbedFields($group);
}

/**
* Get the model that has fired this event.
*
* @return Group
*/
public function getModel(): Group
{
return $this->model;
}

/**
* Get the description for the Discord message payload.
*
* @return string
*/
protected function getDiscordMessageDescription(): string
{
return "Group '**{$this->getModel()->getName()}**' has been updated.";
}

/**
* Perform updates on related indices.
*
* @return void
*/
public function updateRelatedIndices(): void
{
$group = $this->getModel()->load(Group::RELATION_VIDEOS);

$group->animethemes->each(function (AnimeTheme $theme) {
$theme->searchable();
$theme->animethemeentries->each(function (AnimeThemeEntry $entry) {
$entry->searchable();
$entry->videos->each(fn (Video $video) => $video->searchable());
});
});
}
}
Loading

0 comments on commit e472a1d

Please sign in to comment.