generated from liutk/owl-admin-base
80 lines
2.0 KiB
PHP
80 lines
2.0 KiB
PHP
<?php
|
|
|
|
namespace App\Admin\Services;
|
|
|
|
use App\Admin\Filters\ArticleFilter;
|
|
use App\Models\Article;
|
|
use App\Traits\UploadTrait;
|
|
use Illuminate\Support\Arr;
|
|
|
|
/**
|
|
* @method Article getModel()
|
|
* @method Article|\Illuminate\Database\Query\Builder query()
|
|
*/
|
|
class ArticleService extends BaseService
|
|
{
|
|
use UploadTrait;
|
|
|
|
protected string $modelName = Article::class;
|
|
|
|
protected string $modelFilterName = ArticleFilter::class;
|
|
|
|
protected bool $modelSortAble = true;
|
|
|
|
public function store($data): bool
|
|
{
|
|
$columns = $this->getTableColumns();
|
|
$model = $this->getModel();
|
|
|
|
$isEnable = Arr::get($data, 'is_enabled');
|
|
$publishedAt = Arr::get($data, 'published_at');
|
|
if ($isEnable && empty($publishedAt)) {
|
|
$data['published_at'] = now();
|
|
}
|
|
|
|
$data['cover'] = $this->saveImage('cover', 'articles/cover')[0] ?? '';
|
|
$data['appendixes'] = $this->saveFile('appendixes', 'articles/appendixes');
|
|
|
|
foreach ($data as $k => $v) {
|
|
if (! in_array($k, $columns)) {
|
|
continue;
|
|
}
|
|
|
|
$model->setAttribute($k, $v);
|
|
}
|
|
|
|
return $model->save();
|
|
}
|
|
|
|
public function update($primaryKey, $data): bool
|
|
{
|
|
$columns = $this->getTableColumns();
|
|
$model = $this->query()->whereKey($primaryKey)->first();
|
|
|
|
$isEnable = Arr::get($data, 'is_enable');
|
|
$publishedAt = Arr::get($data, 'published_at');
|
|
|
|
if ($isEnable && empty($publishedAt) && empty($model->published_at)) {
|
|
$data['published_at'] = now();
|
|
}
|
|
|
|
if (isset($data['cover'])) {
|
|
$data['cover'] = $this->saveImage('cover', 'articles/cover')[0] ?? '';
|
|
}
|
|
|
|
if (isset($data['appendixes'])) {
|
|
$data['appendixes'] = $this->saveFile('appendixes', 'articles/appendixes');
|
|
}
|
|
|
|
foreach ($data as $k => $v) {
|
|
if (! in_array($k, $columns)) {
|
|
continue;
|
|
}
|
|
|
|
$model->setAttribute($k, $v);
|
|
}
|
|
|
|
return $model->save();
|
|
}
|
|
}
|