feat(website-type): add presentation fields and site logo relationship to WebsiteType model
This commit is contained in:
@@ -2,14 +2,21 @@
|
||||
|
||||
namespace App\Domains\Tenant\Models;
|
||||
|
||||
use App\Domains\Attachable\Models\Attachment;
|
||||
use Illuminate\Database\Eloquent\Attributes\Fillable;
|
||||
use Illuminate\Database\Eloquent\Factories\HasFactory;
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
use Illuminate\Database\Eloquent\Relations\BelongsTo;
|
||||
use Illuminate\Database\Eloquent\Relations\HasMany;
|
||||
|
||||
#[Fillable([
|
||||
'codigo',
|
||||
'nombre',
|
||||
'primary_color',
|
||||
'secondary_color',
|
||||
'danger_color',
|
||||
'success_color',
|
||||
'site_logo',
|
||||
])]
|
||||
class WebsiteType extends Model
|
||||
{
|
||||
@@ -17,6 +24,14 @@ class WebsiteType extends Model
|
||||
|
||||
protected $table = 'website_type';
|
||||
|
||||
/**
|
||||
* @return BelongsTo<Attachment, $this>
|
||||
*/
|
||||
public function siteLogo(): BelongsTo
|
||||
{
|
||||
return $this->belongsTo(Attachment::class, 'site_logo');
|
||||
}
|
||||
|
||||
/**
|
||||
* @return HasMany<WebsiteTypeExtra, $this>
|
||||
*/
|
||||
|
||||
@@ -37,6 +37,11 @@ class TenantResource extends JsonResource
|
||||
fn () => $this->websiteType ? [
|
||||
'codigo' => $this->websiteType->codigo,
|
||||
'nombre' => $this->websiteType->nombre,
|
||||
'primary_color' => $this->websiteType->primary_color,
|
||||
'secondary_color' => $this->websiteType->secondary_color,
|
||||
'danger_color' => $this->websiteType->danger_color,
|
||||
'success_color' => $this->websiteType->success_color,
|
||||
'site_logo' => $this->websiteType->siteLogo?->getTemporaryUrl(1440),
|
||||
] : null
|
||||
),
|
||||
'extras' => $this->whenLoaded(
|
||||
|
||||
@@ -14,7 +14,7 @@ class TenantInformationService
|
||||
'headerLogo',
|
||||
'footerLogo',
|
||||
'socialMedia',
|
||||
'websiteType',
|
||||
'websiteType.siteLogo',
|
||||
'websiteExtras.websiteTypeExtra',
|
||||
];
|
||||
|
||||
|
||||
49
app/Domains/Tenant/Services/WebsiteTypeService.php
Normal file
49
app/Domains/Tenant/Services/WebsiteTypeService.php
Normal file
@@ -0,0 +1,49 @@
|
||||
<?php
|
||||
|
||||
namespace App\Domains\Tenant\Services;
|
||||
|
||||
use App\Domains\Attachable\Models\Attachment;
|
||||
use App\Domains\Attachable\Services\AttachmentService;
|
||||
use App\Domains\Tenant\Models\WebsiteType;
|
||||
use Illuminate\Support\Facades\DB;
|
||||
use Illuminate\Support\Str;
|
||||
|
||||
class WebsiteTypeService
|
||||
{
|
||||
public function __construct(
|
||||
protected AttachmentService $attachmentService,
|
||||
) {}
|
||||
|
||||
/**
|
||||
* Create a website type and store its site logo.
|
||||
*
|
||||
* @param array<string, mixed> $data
|
||||
*/
|
||||
public function create(array $data): WebsiteType
|
||||
{
|
||||
return DB::transaction(function () use ($data): WebsiteType {
|
||||
$siteLogo = $data['site_logo'] ?? null;
|
||||
|
||||
unset($data['site_logo']);
|
||||
|
||||
$siteLogoAttachmentId = null;
|
||||
|
||||
if ($siteLogo) {
|
||||
$attachment = Str::isUuid($siteLogo)
|
||||
? Attachment::query()->where('key', $siteLogo)->first()
|
||||
: $this->attachmentService->store($siteLogo, 'website-types');
|
||||
|
||||
if ($attachment) {
|
||||
$siteLogoAttachmentId = $attachment->id;
|
||||
}
|
||||
}
|
||||
|
||||
$data['site_logo'] = $siteLogoAttachmentId;
|
||||
|
||||
/** @var WebsiteType $websiteType */
|
||||
$websiteType = WebsiteType::query()->create($data);
|
||||
|
||||
return $websiteType;
|
||||
});
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user