|
4 | 4 |
|
5 | 5 | use App\Http\Requests\ArticleRequest; |
6 | 6 | use App\Models\Article; |
7 | | -use Illuminate\Support\Arr; |
| 7 | +use App\Models\Series; |
8 | 8 |
|
9 | 9 | final class UpdateArticle |
10 | 10 | { |
11 | 11 | private $article; |
12 | 12 |
|
13 | | - private $attributes; |
| 13 | + private $title; |
14 | 14 |
|
15 | | - public function __construct(Article $article, array $attributes = []) |
| 15 | + private $body; |
| 16 | + |
| 17 | + private $tags; |
| 18 | + |
| 19 | + private $series; |
| 20 | + |
| 21 | + public function __construct(Article $article, string $title, string $body, array $tags = [], string $series = null) |
16 | 22 | { |
17 | 23 | $this->article = $article; |
18 | | - $this->attributes = Arr::only($attributes, ['title', 'body', 'slug', 'tags']); |
| 24 | + $this->title = $title; |
| 25 | + $this->body = $body; |
| 26 | + $this->tags = $tags; |
| 27 | + $this->series = $series; |
19 | 28 | } |
20 | 29 |
|
21 | 30 | public static function fromRequest(Article $article, ArticleRequest $request): self |
22 | 31 | { |
23 | | - return new static($article, [ |
24 | | - 'title' => $request->title(), |
25 | | - 'body' => $request->body(), |
26 | | - 'slug' => $request->title(), |
27 | | - 'tags' => $request->tags(), |
28 | | - ]); |
| 32 | + return new static( |
| 33 | + $article, |
| 34 | + $request->title(), |
| 35 | + $request->body(), |
| 36 | + $request->tags(), |
| 37 | + $request->series() |
| 38 | + ); |
29 | 39 | } |
30 | 40 |
|
31 | 41 | public function handle(): Article |
32 | 42 | { |
33 | | - $this->article->update($this->attributes); |
34 | | - |
35 | | - if (Arr::has($this->attributes, 'tags')) { |
36 | | - $this->article->syncTags($this->attributes['tags']); |
37 | | - } |
38 | | - |
| 43 | + $this->article->update([ |
| 44 | + 'title' => $this->title, |
| 45 | + 'body' => $this->body, |
| 46 | + 'slug' => $this->title, |
| 47 | + ]); |
| 48 | + $this->article->syncTags($this->tags); |
| 49 | + $this->article->updateSeries(Series::find($this->series)); |
39 | 50 | $this->article->save(); |
40 | 51 |
|
41 | 52 | return $this->article; |
|
0 commit comments