feat(website-type): add footer logo support in WebsiteType model, controller, and resource; update seeder and tests
This commit is contained in:
@@ -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