Skip to content

Commit

Permalink
feat: songresource pivot (#602)
Browse files Browse the repository at this point in the history
  • Loading branch information
Kyrch authored Oct 30, 2023
1 parent 9b81a54 commit 8c78992
Show file tree
Hide file tree
Showing 20 changed files with 775 additions and 0 deletions.
41 changes: 41 additions & 0 deletions app/Events/Pivot/Wiki/SongResource/SongResourceCreated.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
<?php

declare(strict_types=1);

namespace App\Events\Pivot\Wiki\SongResource;

use App\Events\Base\Pivot\PivotCreatedEvent;
use App\Models\Wiki\Song;
use App\Models\Wiki\ExternalResource;
use App\Pivots\Wiki\SongResource;

/**
* Class SongResourceCreated.
*
* @extends PivotCreatedEvent<Song, ExternalResource>
*/
class SongResourceCreated extends PivotCreatedEvent
{
/**
* Create a new event instance.
*
* @param SongResource $songResource
*/
public function __construct(SongResource $songResource)
{
parent::__construct($songResource->song, $songResource->resource);
}

/**
* Get the description for the Discord message payload.
*
* @return string
*/
protected function getDiscordMessageDescription(): string
{
$foreign = $this->getForeign();
$related = $this->getRelated();

return "Resource '**{$foreign->getName()}**' has been attached to Song '**{$related->getName()}**'.";
}
}
41 changes: 41 additions & 0 deletions app/Events/Pivot/Wiki/SongResource/SongResourceDeleted.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
<?php

declare(strict_types=1);

namespace App\Events\Pivot\Wiki\SongResource;

use App\Events\Base\Pivot\PivotDeletedEvent;
use App\Models\Wiki\Song;
use App\Models\Wiki\ExternalResource;
use App\Pivots\Wiki\SongResource;

/**
* Class SongResourceDeleted.
*
* @extends PivotDeletedEvent<Song, ExternalResource>
*/
class SongResourceDeleted extends PivotDeletedEvent
{
/**
* Create a new event instance.
*
* @param SongResource $songResource
*/
public function __construct(SongResource $songResource)
{
parent::__construct($songResource->song, $songResource->resource);
}

/**
* Get the description for the Discord message payload.
*
* @return string
*/
protected function getDiscordMessageDescription(): string
{
$foreign = $this->getForeign();
$related = $this->getRelated();

return "Resource '**{$foreign->getName()}**' has been detached from Song '**{$related->getName()}**'.";
}
}
42 changes: 42 additions & 0 deletions app/Events/Pivot/Wiki/SongResource/SongResourceUpdated.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
<?php

declare(strict_types=1);

namespace App\Events\Pivot\Wiki\SongResource;

use App\Events\Base\Pivot\PivotUpdatedEvent;
use App\Models\Wiki\Song;
use App\Models\Wiki\ExternalResource;
use App\Pivots\Wiki\SongResource;

/**
* Class SongResourceUpdated.
*
* @extends PivotUpdatedEvent<Song, ExternalResource>
*/
class SongResourceUpdated extends PivotUpdatedEvent
{
/**
* Create a new event instance.
*
* @param SongResource $songResource
*/
public function __construct(SongResource $songResource)
{
parent::__construct($songResource->song, $songResource->resource);
$this->initializeEmbedFields($songResource);
}

/**
* Get the description for the Discord message payload.
*
* @return string
*/
protected function getDiscordMessageDescription(): string
{
$foreign = $this->getForeign();
$related = $this->getRelated();

return "Resource '**{$foreign->getName()}**' for Song '**{$related->getName()}**' has been updated.";
}
}
58 changes: 58 additions & 0 deletions app/Http/Api/Field/Pivot/Wiki/SongResource/SongResourceAsField.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
<?php

declare(strict_types=1);

namespace App\Http\Api\Field\Pivot\Wiki\SongResource;

use App\Contracts\Http\Api\Field\CreatableField;
use App\Contracts\Http\Api\Field\UpdatableField;
use App\Http\Api\Field\StringField;
use App\Http\Api\Schema\Schema;
use App\Pivots\Wiki\SongResource;
use Illuminate\Http\Request;

/**
* Class SongResourceAsField.
*/
class SongResourceAsField extends StringField implements CreatableField, UpdatableField
{
/**
* Create a new field instance.
*
* @param Schema $schema
*/
public function __construct(Schema $schema)
{
parent::__construct($schema, SongResource::ATTRIBUTE_AS);
}

/**
* Set the creation validation rules for the field.
*
* @param Request $request
* @return array
*/
public function getCreationRules(Request $request): array
{
return [
'nullable',
'string',
'max:192',
];
}

/**
* Set the update validation rules for the field.
*
* @param Request $request
* @return array
*/
public function getUpdateRules(Request $request): array
{
return [
'nullable',
'string',
'max:192',
];
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
<?php

declare(strict_types=1);

namespace App\Http\Api\Field\Pivot\Wiki\SongResource;

use App\Contracts\Http\Api\Field\SelectableField;
use App\Http\Api\Field\Field;
use App\Http\Api\Query\Query;
use App\Http\Api\Schema\Schema;
use App\Pivots\Wiki\SongResource;

/**
* Class SongResourceResourceIdField.
*/
class SongResourceResourceIdField extends Field implements SelectableField
{
/**
* Create a new field instance.
*
* @param Schema $schema
*/
public function __construct(Schema $schema)
{
parent::__construct($schema, SongResource::ATTRIBUTE_RESOURCE);
}

/**
* Determine if the field should be included in the select clause of our query.
*
* @param Query $query
* @param Schema $schema
* @return bool
*/
public function shouldSelect(Query $query, Schema $schema): bool
{
// Needed to match resource relation.
return true;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
<?php

declare(strict_types=1);

namespace App\Http\Api\Field\Pivot\Wiki\SongResource;

use App\Contracts\Http\Api\Field\SelectableField;
use App\Http\Api\Field\Field;
use App\Http\Api\Query\Query;
use App\Http\Api\Schema\Schema;
use App\Pivots\Wiki\SongResource;

/**
* Class SongResourceSongIdField.
*/
class SongResourceSongIdField extends Field implements SelectableField
{
/**
* Create a new field instance.
*
* @param Schema $schema
*/
public function __construct(Schema $schema)
{
parent::__construct($schema, SongResource::ATTRIBUTE_SONG);
}

/**
* Determine if the field should be included in the select clause of our query.
*
* @param Query $query
* @param Schema $schema
* @return bool
*/
public function shouldSelect(Query $query, Schema $schema): bool
{
// Needed to match anime relation.
return true;
}
}
65 changes: 65 additions & 0 deletions app/Http/Api/Schema/Pivot/Wiki/SongResourceSchema.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
<?php

declare(strict_types=1);

namespace App\Http\Api\Schema\Pivot\Wiki;

use App\Http\Api\Field\Base\CreatedAtField;
use App\Http\Api\Field\Base\UpdatedAtField;
use App\Http\Api\Field\Field;
use App\Http\Api\Field\Pivot\Wiki\SongResource\SongResourceSongIdField;
use App\Http\Api\Field\Pivot\Wiki\SongResource\SongResourceAsField;
use App\Http\Api\Field\Pivot\Wiki\SongResource\SongResourceResourceIdField;
use App\Http\Api\Include\AllowedInclude;
use App\Http\Api\Schema\EloquentSchema;
use App\Http\Api\Schema\Wiki\ExternalResourceSchema;
use App\Http\Api\Schema\Wiki\SongSchema;
use App\Http\Resources\Pivot\Wiki\Resource\SongResourceResource;
use App\Pivots\Wiki\SongResource;

/**
* Class SongResourceSchema.
*/
class SongResourceSchema extends EloquentSchema
{
/**
* Get the type of the resource.
*
* @return string
*/
public function type(): string
{
return SongResourceResource::$wrap;
}

/**
* Get the allowed includes.
*
* @return AllowedInclude[]
*/
public function allowedIncludes(): array
{
return [
new AllowedInclude(new SongSchema(), SongResource::RELATION_SONG),
new AllowedInclude(new ExternalResourceSchema(), SongResource::RELATION_RESOURCE),
];
}

/**
* Get the direct fields of the resource.
*
* @return Field[]
*
* @noinspection PhpMissingParentCallCommonInspection
*/
public function fields(): array
{
return [
new CreatedAtField($this),
new UpdatedAtField($this),
new SongResourceSongIdField($this),
new SongResourceResourceIdField($this),
new SongResourceAsField($this),
];
}
}
4 changes: 4 additions & 0 deletions app/Http/Api/Schema/Wiki/ExternalResourceSchema.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,11 @@
use App\Http\Api\Schema\EloquentSchema;
use App\Http\Api\Schema\Pivot\Wiki\AnimeResourceSchema;
use App\Http\Api\Schema\Pivot\Wiki\ArtistResourceSchema;
use App\Http\Api\Schema\Pivot\Wiki\SongResourceSchema;
use App\Http\Api\Schema\Pivot\Wiki\StudioResourceSchema;
use App\Http\Resources\Pivot\Wiki\Resource\AnimeResourceResource;
use App\Http\Resources\Pivot\Wiki\Resource\ArtistResourceResource;
use App\Http\Resources\Pivot\Wiki\Resource\SongResourceResource;
use App\Http\Resources\Pivot\Wiki\Resource\StudioResourceResource;
use App\Http\Resources\Wiki\Resource\ExternalResourceResource;
use App\Models\Wiki\ExternalResource;
Expand All @@ -37,6 +39,7 @@ public function allowedPivots(): array
new AllowedInclude(new AnimeResourceSchema(), AnimeResourceResource::$wrap),
new AllowedInclude(new ArtistResourceSchema(), ArtistResourceResource::$wrap),
new AllowedInclude(new StudioResourceSchema(), StudioResourceResource::$wrap),
new AllowedInclude(new SongResourceSchema(), SongResourceResource::$wrap)
];
}

Expand All @@ -61,6 +64,7 @@ public function allowedIncludes(): array
new AllowedInclude(new AnimeSchema(), ExternalResource::RELATION_ANIME),
new AllowedInclude(new ArtistSchema(), ExternalResource::RELATION_ARTISTS),
new AllowedInclude(new StudioSchema(), ExternalResource::RELATION_STUDIOS),
new AllowedInclude(new SongSchema(), ExternalResource::RELATION_SONG)
];
}

Expand Down
1 change: 1 addition & 0 deletions app/Http/Api/Schema/Wiki/SongSchema.php
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ public function allowedIncludes(): array
new AllowedInclude(new AnimeSchema(), Song::RELATION_ANIME),
new AllowedInclude(new ArtistSchema(), Song::RELATION_ARTISTS),
new AllowedInclude(new ThemeSchema(), Song::RELATION_ANIMETHEMES),
new AllowedInclude(new ExternalResourceSchema(), Song::RELATION_RESOURCES)
];
}

Expand Down
Loading

0 comments on commit 8c78992

Please sign in to comment.