feat(website-type): add new color fields and update related methods in WebsiteType model and service
This commit is contained in:
@@ -17,6 +17,12 @@ use Illuminate\Database\Eloquent\Relations\HasMany;
|
||||
'secondary_color',
|
||||
'danger_color',
|
||||
'success_color',
|
||||
'warning_color',
|
||||
'body_color',
|
||||
'darker_body_color',
|
||||
'surface_color',
|
||||
'background_color',
|
||||
'border_color',
|
||||
'login_header_footer_color',
|
||||
'site_logo',
|
||||
])]
|
||||
|
||||
@@ -21,27 +21,58 @@ class WebsiteTypeService
|
||||
*/
|
||||
public function create(array $data): WebsiteType
|
||||
{
|
||||
return DB::transaction(function () use ($data): WebsiteType {
|
||||
return $this->save(new WebsiteType, $data);
|
||||
}
|
||||
|
||||
/**
|
||||
* Create or update a website type and replace its site logo when provided.
|
||||
*
|
||||
* @param array<string, mixed> $attributes
|
||||
* @param array<string, mixed> $values
|
||||
*/
|
||||
public function updateOrCreate(array $attributes, array $values = []): WebsiteType
|
||||
{
|
||||
/** @var WebsiteType $websiteType */
|
||||
$websiteType = WebsiteType::query()->firstOrNew($attributes);
|
||||
|
||||
return $this->save($websiteType, [...$attributes, ...$values]);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param array<string, mixed> $data
|
||||
*/
|
||||
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;
|
||||
|
||||
unset($data['site_logo']);
|
||||
|
||||
$siteLogoAttachmentId = null;
|
||||
if ($hasSiteLogo) {
|
||||
$attachment = null;
|
||||
|
||||
if ($siteLogo) {
|
||||
$attachment = Str::isUuid($siteLogo)
|
||||
? Attachment::query()->where('key', $siteLogo)->first()
|
||||
: $this->attachmentService->store($siteLogo, 'website-types');
|
||||
|
||||
if ($attachment) {
|
||||
$siteLogoAttachmentId = $attachment->id;
|
||||
if ($siteLogo) {
|
||||
$attachment = is_string($siteLogo) && Str::isUuid($siteLogo)
|
||||
? Attachment::query()->where('key', $siteLogo)->first()
|
||||
: $this->attachmentService->store($siteLogo, 'website-types');
|
||||
}
|
||||
|
||||
$data['site_logo'] = $attachment?->id;
|
||||
}
|
||||
|
||||
$data['site_logo'] = $siteLogoAttachmentId;
|
||||
$websiteType->fill($data)->save();
|
||||
|
||||
/** @var WebsiteType $websiteType */
|
||||
$websiteType = WebsiteType::query()->create($data);
|
||||
if (
|
||||
$hasSiteLogo
|
||||
&& $previousSiteLogo instanceof Attachment
|
||||
&& $previousSiteLogo->id !== $websiteType->site_logo
|
||||
) {
|
||||
$this->attachmentService->delete($previousSiteLogo);
|
||||
}
|
||||
|
||||
return $websiteType;
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user