Files
SkinbaseNova/app/Services/Artworks/ArtworkDraftService.php

54 lines
1.7 KiB
PHP

<?php
declare(strict_types=1);
namespace App\Services\Artworks;
use App\DTOs\Artworks\ArtworkDraftResult;
use App\Models\Artwork;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Str;
final class ArtworkDraftService
{
public function createDraft(int $userId, string $title, ?string $description, ?int $categoryId = null, bool $isMature = false): ArtworkDraftResult
{
return DB::transaction(function () use ($userId, $title, $description, $categoryId, $isMature) {
$slug = $this->makeSlug($title);
$artwork = Artwork::create([
'user_id' => $userId,
'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',
]);
// Attach the selected category to the artwork pivot table
if ($categoryId !== null && \App\Models\Category::where('id', $categoryId)->exists()) {
$artwork->categories()->sync([$categoryId]);
}
return new ArtworkDraftResult((int) $artwork->id, 'draft');
});
}
private function makeSlug(string $title): string
{
$base = Str::slug($title);
return Str::limit($base !== '' ? $base : 'artwork', 160, '');
}
}