Upload beautify
This commit is contained in:
55
app/Services/Artworks/ArtworkDraftService.php
Normal file
55
app/Services/Artworks/ArtworkDraftService.php
Normal file
@@ -0,0 +1,55 @@
|
||||
<?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): ArtworkDraftResult
|
||||
{
|
||||
return DB::transaction(function () use ($userId, $title, $description) {
|
||||
$slug = $this->uniqueSlug($title);
|
||||
|
||||
$artwork = Artwork::create([
|
||||
'user_id' => $userId,
|
||||
'title' => $title,
|
||||
'slug' => $slug,
|
||||
'description' => $description,
|
||||
'file_name' => 'pending',
|
||||
'file_path' => 'pending',
|
||||
'file_size' => 0,
|
||||
'mime_type' => 'application/octet-stream',
|
||||
'width' => 1,
|
||||
'height' => 1,
|
||||
'is_public' => false,
|
||||
'is_approved' => false,
|
||||
'published_at' => null,
|
||||
]);
|
||||
|
||||
return new ArtworkDraftResult((int) $artwork->id, 'draft');
|
||||
});
|
||||
}
|
||||
|
||||
private function uniqueSlug(string $title): string
|
||||
{
|
||||
$base = Str::slug($title);
|
||||
$base = $base !== '' ? $base : 'artwork';
|
||||
|
||||
for ($i = 0; $i < 5; $i++) {
|
||||
$suffix = Str::lower(Str::random(6));
|
||||
$slug = Str::limit($base . '-' . $suffix, 160, '');
|
||||
|
||||
if (! Artwork::where('slug', $slug)->exists()) {
|
||||
return $slug;
|
||||
}
|
||||
}
|
||||
|
||||
return Str::limit($base . '-' . Str::uuid()->toString(), 160, '');
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user