feat(website-type): add footer logo support in WebsiteType model, controller, and resource; update seeder and tests
This commit is contained in:
@@ -17,7 +17,7 @@ class BootstrapAdminAppController extends Controller
|
||||
|
||||
return BootstrapAdminAppResource::make(
|
||||
WebsiteType::query()
|
||||
->with('siteLogo')
|
||||
->with(['siteLogo', 'footerLogo'])
|
||||
->where('dominio', $domain)
|
||||
->firstOrFail()
|
||||
);
|
||||
|
||||
@@ -25,6 +25,7 @@ use Illuminate\Database\Eloquent\Relations\HasMany;
|
||||
'border_color',
|
||||
'login_header_footer_color',
|
||||
'site_logo',
|
||||
'footer_logo',
|
||||
])]
|
||||
class WebsiteType extends Model
|
||||
{
|
||||
@@ -40,6 +41,14 @@ class WebsiteType extends Model
|
||||
return $this->belongsTo(Attachment::class, 'site_logo');
|
||||
}
|
||||
|
||||
/**
|
||||
* @return BelongsTo<Attachment, $this>
|
||||
*/
|
||||
public function footerLogo(): BelongsTo
|
||||
{
|
||||
return $this->belongsTo(Attachment::class, 'footer_logo');
|
||||
}
|
||||
|
||||
/**
|
||||
* @return HasMany<WebsiteTypeExtra, $this>
|
||||
*/
|
||||
|
||||
@@ -33,6 +33,7 @@ class BootstrapAdminAppResource extends JsonResource
|
||||
'border_color' => $this->border_color,
|
||||
'login_header_footer_color' => $this->login_header_footer_color,
|
||||
'site_logo' => $this->siteLogo?->getTemporaryUrl(1440),
|
||||
'footer_logo' => $this->footerLogo?->getTemporaryUrl(1440),
|
||||
];
|
||||
}
|
||||
}
|
||||
|
||||
@@ -15,7 +15,7 @@ class WebsiteTypeService
|
||||
) {}
|
||||
|
||||
/**
|
||||
* Create a website type and store its site logo.
|
||||
* Create a website type and store its logos.
|
||||
*
|
||||
* @param array<string, mixed> $data
|
||||
*/
|
||||
@@ -25,7 +25,7 @@ class WebsiteTypeService
|
||||
}
|
||||
|
||||
/**
|
||||
* Create or update a website type and replace its site logo when provided.
|
||||
* Create or update a website type and replace its logos when provided.
|
||||
*
|
||||
* @param array<string, mixed> $attributes
|
||||
* @param array<string, mixed> $values
|
||||
@@ -44,34 +44,41 @@ class WebsiteTypeService
|
||||
private function save(WebsiteType $websiteType, array $data): WebsiteType
|
||||
{
|
||||
return DB::transaction(function () use ($websiteType, $data): WebsiteType {
|
||||
$hasSiteLogo = array_key_exists('site_logo', $data);
|
||||
$siteLogo = $data['site_logo'] ?? null;
|
||||
$previousSiteLogo = $websiteType->exists
|
||||
? $websiteType->siteLogo()->first()
|
||||
: null;
|
||||
$previousLogos = [];
|
||||
|
||||
unset($data['site_logo']);
|
||||
|
||||
if ($hasSiteLogo) {
|
||||
$attachment = null;
|
||||
|
||||
if ($siteLogo) {
|
||||
$attachment = is_string($siteLogo) && Str::isUuid($siteLogo)
|
||||
? Attachment::query()->where('key', $siteLogo)->first()
|
||||
: $this->attachmentService->store($siteLogo, 'website-types');
|
||||
foreach (['site_logo' => 'siteLogo', 'footer_logo' => 'footerLogo'] as $field => $relation) {
|
||||
if (! array_key_exists($field, $data)) {
|
||||
continue;
|
||||
}
|
||||
|
||||
$data['site_logo'] = $attachment?->id;
|
||||
$logo = $data[$field];
|
||||
$previousLogos[$field] = $websiteType->exists
|
||||
? $websiteType->{$relation}()->first()
|
||||
: null;
|
||||
unset($data[$field]);
|
||||
|
||||
$attachment = null;
|
||||
|
||||
if ($logo) {
|
||||
$attachment = is_string($logo) && Str::isUuid($logo)
|
||||
? Attachment::query()->where('key', $logo)->first()
|
||||
: $this->attachmentService->store($logo, 'website-types');
|
||||
}
|
||||
|
||||
$data[$field] = $attachment?->id;
|
||||
}
|
||||
|
||||
$websiteType->fill($data)->save();
|
||||
|
||||
if (
|
||||
$hasSiteLogo
|
||||
&& $previousSiteLogo instanceof Attachment
|
||||
&& $previousSiteLogo->id !== $websiteType->site_logo
|
||||
) {
|
||||
$this->attachmentService->delete($previousSiteLogo);
|
||||
foreach ($previousLogos as $field => $previousLogo) {
|
||||
if (
|
||||
$previousLogo instanceof Attachment
|
||||
&& $previousLogo->id !== $websiteType->{$field}
|
||||
&& $previousLogo->id !== $websiteType->site_logo
|
||||
&& $previousLogo->id !== $websiteType->footer_logo
|
||||
) {
|
||||
$this->attachmentService->delete($previousLogo);
|
||||
}
|
||||
}
|
||||
|
||||
return $websiteType;
|
||||
|
||||
Reference in New Issue
Block a user