Keep artwork_category as membership, store a deterministic primary_category_id, and let upload plus studio pick up to five additional same-type categories without changing canonical URLs.
95 lines
3.3 KiB
PHP
95 lines
3.3 KiB
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
namespace App\Services\Artworks;
|
|
|
|
use App\DTOs\Artworks\ArtworkDraftResult;
|
|
use App\Models\Artwork;
|
|
use App\Models\Group;
|
|
use App\Models\User;
|
|
use App\Services\GroupService;
|
|
use Illuminate\Support\Facades\DB;
|
|
use Illuminate\Support\Str;
|
|
use Illuminate\Validation\ValidationException;
|
|
|
|
final class ArtworkDraftService
|
|
{
|
|
public function __construct(
|
|
private readonly GroupService $groups,
|
|
private readonly ArtworkCategoryService $categories,
|
|
) {
|
|
}
|
|
|
|
/**
|
|
* @param list<int> $secondaryCategoryIds
|
|
*/
|
|
public function createDraft(User $user, string $title, ?string $description, ?int $categoryId = null, bool $isMature = false, string|int|null $groupIdentifier = null, array $secondaryCategoryIds = []): ArtworkDraftResult
|
|
{
|
|
return DB::transaction(function () use ($user, $title, $description, $categoryId, $isMature, $groupIdentifier, $secondaryCategoryIds) {
|
|
$slug = $this->makeSlug($title);
|
|
$group = $this->resolveGroup($user, $groupIdentifier);
|
|
|
|
$artwork = Artwork::create([
|
|
'user_id' => (int) $user->id,
|
|
'group_id' => $group?->id,
|
|
'uploaded_by_user_id' => (int) $user->id,
|
|
'primary_author_user_id' => (int) $user->id,
|
|
'published_as_type' => $group ? Artwork::PUBLISHED_AS_GROUP : Artwork::PUBLISHED_AS_USER,
|
|
'published_as_id' => $group?->id ?: (int) $user->id,
|
|
'title' => $title,
|
|
'slug' => $slug,
|
|
'description' => $description,
|
|
'file_name' => 'pending',
|
|
'file_path' => '',
|
|
'file_size' => 0,
|
|
'mime_type' => 'application/octet-stream',
|
|
'width' => 1,
|
|
'height' => 1,
|
|
'is_public' => false,
|
|
'visibility' => Artwork::VISIBILITY_PRIVATE,
|
|
'is_approved' => false,
|
|
'is_mature' => $isMature,
|
|
'published_at' => null,
|
|
'artwork_status' => 'draft',
|
|
]);
|
|
|
|
if ($categoryId !== null || $secondaryCategoryIds !== []) {
|
|
$this->categories->sync($artwork, $categoryId, $secondaryCategoryIds, 'user', false);
|
|
}
|
|
|
|
if ($group) {
|
|
$this->groups->syncArtworkCount($group);
|
|
}
|
|
|
|
return new ArtworkDraftResult((int) $artwork->id, 'draft');
|
|
});
|
|
}
|
|
|
|
private function resolveGroup(User $user, string|int|null $groupIdentifier): ?Group
|
|
{
|
|
if ($groupIdentifier === null || $groupIdentifier === '') {
|
|
return null;
|
|
}
|
|
|
|
$group = is_numeric($groupIdentifier)
|
|
? Group::query()->with('members')->findOrFail((int) $groupIdentifier)
|
|
: Group::query()->with('members')->where('slug', (string) $groupIdentifier)->firstOrFail();
|
|
|
|
if (! $group->canCreateArtworkDrafts($user) && ! $user->isAdmin()) {
|
|
throw ValidationException::withMessages([
|
|
'group' => 'You are not allowed to create drafts for this group.',
|
|
]);
|
|
}
|
|
|
|
return $group;
|
|
}
|
|
|
|
private function makeSlug(string $title): string
|
|
{
|
|
$base = Str::slug($title);
|
|
|
|
return Str::limit($base !== '' ? $base : 'artwork', 160, '');
|
|
}
|
|
}
|