mirror of
https://github.com/AnimeThemes/animethemes-server.git
synced 2026-07-11 01:24:46 +02:00
clean(api): remove track position (#1198)
This commit is contained in:
@@ -9,7 +9,6 @@ use App\Models\Admin\Activity;
|
||||
use Filament\Actions\Action;
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
use Illuminate\Support\Str;
|
||||
use Spatie\Activitylog\Contracts\Activity as ContractsActivity;
|
||||
use Throwable;
|
||||
|
||||
trait HasActivityLogs
|
||||
|
||||
@@ -1,17 +0,0 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Http\Api\Field\List\Playlist\Track;
|
||||
|
||||
use App\Http\Api\Field\IntField;
|
||||
use App\Http\Api\Schema\Schema;
|
||||
use App\Models\List\Playlist\PlaylistTrack;
|
||||
|
||||
class TrackPositionField extends IntField
|
||||
{
|
||||
public function __construct(Schema $schema)
|
||||
{
|
||||
parent::__construct($schema, PlaylistTrack::ATTRIBUTE_POSITION);
|
||||
}
|
||||
}
|
||||
@@ -10,7 +10,6 @@ use App\Http\Api\Field\Field;
|
||||
use App\Http\Api\Field\List\Playlist\Track\TrackEntryIdField;
|
||||
use App\Http\Api\Field\List\Playlist\Track\TrackHashidsField;
|
||||
use App\Http\Api\Field\List\Playlist\Track\TrackIdField;
|
||||
use App\Http\Api\Field\List\Playlist\Track\TrackPositionField;
|
||||
use App\Http\Api\Field\List\Playlist\Track\TrackVideoIdField;
|
||||
use App\Http\Api\Include\AllowedInclude;
|
||||
use App\Http\Api\Schema\EloquentSchema;
|
||||
@@ -21,7 +20,6 @@ use App\Http\Api\Schema\Wiki\ImageSchema;
|
||||
use App\Http\Api\Schema\Wiki\VideoSchema;
|
||||
use App\Http\Resources\List\Playlist\Resource\TrackJsonResource;
|
||||
use App\Models\List\Playlist\PlaylistTrack;
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
|
||||
class ForwardBackwardSchema extends EloquentSchema
|
||||
{
|
||||
@@ -56,7 +54,6 @@ class ForwardBackwardSchema extends EloquentSchema
|
||||
new TrackHashidsField($this),
|
||||
new TrackEntryIdField($this),
|
||||
new TrackVideoIdField($this),
|
||||
new TrackPositionField($this),
|
||||
];
|
||||
}
|
||||
|
||||
|
||||
@@ -13,7 +13,6 @@ use App\Http\Api\Field\List\Playlist\Track\TrackIdField;
|
||||
use App\Http\Api\Field\List\Playlist\Track\TrackNextHashidsField;
|
||||
use App\Http\Api\Field\List\Playlist\Track\TrackNextIdField;
|
||||
use App\Http\Api\Field\List\Playlist\Track\TrackPlaylistIdField;
|
||||
use App\Http\Api\Field\List\Playlist\Track\TrackPositionField;
|
||||
use App\Http\Api\Field\List\Playlist\Track\TrackPreviousHashidsField;
|
||||
use App\Http\Api\Field\List\Playlist\Track\TrackPreviousIdField;
|
||||
use App\Http\Api\Field\List\Playlist\Track\TrackVideoIdField;
|
||||
@@ -67,7 +66,6 @@ class TrackSchema extends EloquentSchema
|
||||
new TrackNextIdField($this),
|
||||
new TrackNextHashidsField($this),
|
||||
new TrackPlaylistIdField($this),
|
||||
new TrackPositionField($this),
|
||||
new TrackPreviousIdField($this),
|
||||
new TrackPreviousHashidsField($this),
|
||||
new TrackEntryIdField($this),
|
||||
|
||||
@@ -26,24 +26,4 @@ class PlaylistTrackFactory extends Factory
|
||||
{
|
||||
return [];
|
||||
}
|
||||
|
||||
/**
|
||||
* Configure the model factory.
|
||||
*/
|
||||
public function configure(): static
|
||||
{
|
||||
$this->afterCreating(function (PlaylistTrack $track): void {
|
||||
if ($track->playlist_id === null) {
|
||||
return;
|
||||
}
|
||||
|
||||
$position = PlaylistTrack::query()
|
||||
->whereBelongsTo($track->playlist)
|
||||
->max(PlaylistTrack::ATTRIBUTE_POSITION) ?? 0;
|
||||
|
||||
$track->update([PlaylistTrack::ATTRIBUTE_POSITION => $position + 1]);
|
||||
});
|
||||
|
||||
return $this;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -64,7 +64,7 @@ class PlaylistFactory extends Factory
|
||||
->createOne();
|
||||
|
||||
if ($index === 1) {
|
||||
$track->moveToStart();
|
||||
$track->position = 1;
|
||||
$playlist->first()->associate($track)->save();
|
||||
}
|
||||
|
||||
@@ -78,7 +78,7 @@ class PlaylistFactory extends Factory
|
||||
}
|
||||
|
||||
if ($index === $count) {
|
||||
$track->moveToEnd();
|
||||
$track->position = $index;
|
||||
$playlist->last()->associate($track);
|
||||
$playlist->save();
|
||||
}
|
||||
@@ -114,6 +114,7 @@ class PlaylistFactory extends Factory
|
||||
->createOne();
|
||||
|
||||
if ($index === 1) {
|
||||
$track->position = 1;
|
||||
$playlist->first()->associate($track)->save();
|
||||
}
|
||||
|
||||
@@ -126,6 +127,7 @@ class PlaylistFactory extends Factory
|
||||
}
|
||||
|
||||
if ($index === count($videoIds)) {
|
||||
$track->position = $index;
|
||||
$playlist->last()->associate($track);
|
||||
$playlist->save();
|
||||
}
|
||||
|
||||
@@ -3,18 +3,15 @@
|
||||
declare(strict_types=1);
|
||||
|
||||
use App\Constants\Config\PlaylistConstants;
|
||||
use App\Constants\Config\ValidationConstants;
|
||||
use App\Enums\Auth\CrudPermission;
|
||||
use App\Enums\Auth\SpecialPermission;
|
||||
use App\Enums\Models\List\PlaylistVisibility;
|
||||
use App\Enums\Rules\ModerationService;
|
||||
use App\Features\AllowPlaylistManagement;
|
||||
use App\Models\Auth\User;
|
||||
use App\Models\List\Playlist;
|
||||
use Illuminate\Foundation\Testing\WithFaker;
|
||||
use Illuminate\Support\Arr;
|
||||
use Illuminate\Support\Facades\Config;
|
||||
use Illuminate\Support\Facades\Http;
|
||||
use Laravel\Pennant\Feature;
|
||||
use Laravel\Sanctum\Sanctum;
|
||||
|
||||
@@ -177,89 +174,3 @@ test('max track limit permitted for bypass', function (): void {
|
||||
|
||||
$response->assertCreated();
|
||||
});
|
||||
|
||||
test('created if not flagged by open ai', function (): void {
|
||||
Feature::activate(AllowPlaylistManagement::class);
|
||||
Config::set(ValidationConstants::MODERATION_SERVICE_QUALIFIED, ModerationService::OPENAI->value);
|
||||
|
||||
Http::fake([
|
||||
'https://api.openai.com/v1/moderations' => Http::response([
|
||||
'results' => [
|
||||
0 => [
|
||||
'flagged' => false,
|
||||
],
|
||||
],
|
||||
]),
|
||||
]);
|
||||
|
||||
$visibility = Arr::random(PlaylistVisibility::cases());
|
||||
|
||||
$parameters = array_merge(
|
||||
Playlist::factory()->raw(),
|
||||
[Playlist::ATTRIBUTE_VISIBILITY => $visibility->localize()],
|
||||
);
|
||||
|
||||
$user = User::factory()->withPermissions(CrudPermission::CREATE->format(Playlist::class))->createOne();
|
||||
|
||||
Sanctum::actingAs($user);
|
||||
|
||||
$response = post(route('api.playlist.store', $parameters));
|
||||
|
||||
$response->assertCreated();
|
||||
});
|
||||
|
||||
test('created if open ai fails', function (): void {
|
||||
Feature::activate(AllowPlaylistManagement::class);
|
||||
Config::set(ValidationConstants::MODERATION_SERVICE_QUALIFIED, ModerationService::OPENAI->value);
|
||||
|
||||
Http::fake([
|
||||
'https://api.openai.com/v1/moderations' => Http::response(status: 404),
|
||||
]);
|
||||
|
||||
$visibility = Arr::random(PlaylistVisibility::cases());
|
||||
|
||||
$parameters = array_merge(
|
||||
Playlist::factory()->raw(),
|
||||
[Playlist::ATTRIBUTE_VISIBILITY => $visibility->localize()],
|
||||
);
|
||||
|
||||
$user = User::factory()->withPermissions(CrudPermission::CREATE->format(Playlist::class))->createOne();
|
||||
|
||||
Sanctum::actingAs($user);
|
||||
|
||||
$response = post(route('api.playlist.store', $parameters));
|
||||
|
||||
$response->assertCreated();
|
||||
});
|
||||
|
||||
test('validation error when flagged by open ai', function (): void {
|
||||
Feature::activate(AllowPlaylistManagement::class);
|
||||
Config::set(ValidationConstants::MODERATION_SERVICE_QUALIFIED, ModerationService::OPENAI->value);
|
||||
|
||||
Http::fake([
|
||||
'https://api.openai.com/v1/moderations' => Http::response([
|
||||
'results' => [
|
||||
0 => [
|
||||
'flagged' => true,
|
||||
],
|
||||
],
|
||||
]),
|
||||
]);
|
||||
|
||||
$visibility = Arr::random(PlaylistVisibility::cases());
|
||||
|
||||
$parameters = array_merge(
|
||||
Playlist::factory()->raw(),
|
||||
[Playlist::ATTRIBUTE_VISIBILITY => $visibility->localize()],
|
||||
);
|
||||
|
||||
$user = User::factory()->withPermissions(CrudPermission::CREATE->format(Playlist::class))->createOne();
|
||||
|
||||
Sanctum::actingAs($user);
|
||||
|
||||
$response = post(route('api.playlist.store', $parameters));
|
||||
|
||||
$response->assertJsonValidationErrors([
|
||||
Playlist::ATTRIBUTE_NAME,
|
||||
]);
|
||||
});
|
||||
|
||||
@@ -2,20 +2,16 @@
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
use App\Constants\Config\ValidationConstants;
|
||||
use App\Enums\Auth\CrudPermission;
|
||||
use App\Enums\Auth\SpecialPermission;
|
||||
use App\Enums\Models\List\PlaylistVisibility;
|
||||
use App\Enums\Rules\ModerationService;
|
||||
use App\Events\List\Playlist\PlaylistCreated;
|
||||
use App\Features\AllowPlaylistManagement;
|
||||
use App\Models\Auth\User;
|
||||
use App\Models\List\Playlist;
|
||||
use Illuminate\Foundation\Testing\WithFaker;
|
||||
use Illuminate\Support\Arr;
|
||||
use Illuminate\Support\Facades\Config;
|
||||
use Illuminate\Support\Facades\Event;
|
||||
use Illuminate\Support\Facades\Http;
|
||||
use Laravel\Pennant\Feature;
|
||||
use Laravel\Sanctum\Sanctum;
|
||||
|
||||
@@ -175,113 +171,3 @@ test('update permitted for bypass', function (): void {
|
||||
|
||||
$response->assertOk();
|
||||
});
|
||||
|
||||
test('updated if not flagged by open ai', function (): void {
|
||||
Event::fakeExcept(PlaylistCreated::class);
|
||||
|
||||
Feature::activate(AllowPlaylistManagement::class);
|
||||
Config::set(ValidationConstants::MODERATION_SERVICE_QUALIFIED, ModerationService::OPENAI->value);
|
||||
|
||||
Http::fake([
|
||||
'https://api.openai.com/v1/moderations' => Http::response([
|
||||
'results' => [
|
||||
0 => [
|
||||
'flagged' => false,
|
||||
],
|
||||
],
|
||||
]),
|
||||
]);
|
||||
|
||||
$user = User::factory()->withPermissions(CrudPermission::UPDATE->format(Playlist::class))->createOne();
|
||||
|
||||
$playlist = Playlist::factory()
|
||||
->for($user)
|
||||
->createOne();
|
||||
|
||||
$visibility = Arr::random(PlaylistVisibility::cases());
|
||||
|
||||
$parameters = array_merge(
|
||||
Playlist::factory()->raw(),
|
||||
[
|
||||
Playlist::ATTRIBUTE_VISIBILITY => $visibility->localize(),
|
||||
],
|
||||
);
|
||||
|
||||
Sanctum::actingAs($user);
|
||||
|
||||
$response = put(route('api.playlist.update', ['playlist' => $playlist] + $parameters));
|
||||
|
||||
$response->assertOk();
|
||||
});
|
||||
|
||||
test('updated if open ai fails', function (): void {
|
||||
Event::fakeExcept(PlaylistCreated::class);
|
||||
|
||||
Feature::activate(AllowPlaylistManagement::class);
|
||||
Config::set(ValidationConstants::MODERATION_SERVICE_QUALIFIED, ModerationService::OPENAI->value);
|
||||
|
||||
Http::fake([
|
||||
'https://api.openai.com/v1/moderations' => Http::response(status: 404),
|
||||
]);
|
||||
|
||||
$user = User::factory()->withPermissions(CrudPermission::UPDATE->format(Playlist::class))->createOne();
|
||||
|
||||
$playlist = Playlist::factory()
|
||||
->for($user)
|
||||
->createOne();
|
||||
|
||||
$visibility = Arr::random(PlaylistVisibility::cases());
|
||||
|
||||
$parameters = array_merge(
|
||||
Playlist::factory()->raw(),
|
||||
[
|
||||
Playlist::ATTRIBUTE_VISIBILITY => $visibility->localize(),
|
||||
],
|
||||
);
|
||||
|
||||
Sanctum::actingAs($user);
|
||||
|
||||
$response = put(route('api.playlist.update', ['playlist' => $playlist] + $parameters));
|
||||
|
||||
$response->assertOk();
|
||||
});
|
||||
|
||||
test('validation error when flagged by open ai', function (): void {
|
||||
Event::fakeExcept(PlaylistCreated::class);
|
||||
|
||||
Feature::activate(AllowPlaylistManagement::class);
|
||||
Config::set(ValidationConstants::MODERATION_SERVICE_QUALIFIED, ModerationService::OPENAI->value);
|
||||
|
||||
Http::fake([
|
||||
'https://api.openai.com/v1/moderations' => Http::response([
|
||||
'results' => [
|
||||
0 => [
|
||||
'flagged' => true,
|
||||
],
|
||||
],
|
||||
]),
|
||||
]);
|
||||
|
||||
$user = User::factory()->withPermissions(CrudPermission::UPDATE->format(Playlist::class))->createOne();
|
||||
|
||||
$playlist = Playlist::factory()
|
||||
->for($user)
|
||||
->createOne();
|
||||
|
||||
$visibility = Arr::random(PlaylistVisibility::cases());
|
||||
|
||||
$parameters = array_merge(
|
||||
Playlist::factory()->raw(),
|
||||
[
|
||||
Playlist::ATTRIBUTE_VISIBILITY => $visibility->localize(),
|
||||
],
|
||||
);
|
||||
|
||||
Sanctum::actingAs($user);
|
||||
|
||||
$response = put(route('api.playlist.update', ['playlist' => $playlist] + $parameters));
|
||||
|
||||
$response->assertJsonValidationErrors([
|
||||
Playlist::ATTRIBUTE_NAME,
|
||||
]);
|
||||
});
|
||||
|
||||
@@ -101,7 +101,7 @@ test('parse global scope', function (): void {
|
||||
});
|
||||
|
||||
test('parse type scope', function (): void {
|
||||
$type = Str::singular(fake()->word());
|
||||
$type = Str::singular('posts');
|
||||
|
||||
$parameters = [
|
||||
SortParser::param() => [
|
||||
|
||||
Reference in New Issue
Block a user