Skip to content

Commit 1d6d829

Browse files
committed
Add articles to series
1 parent f49a712 commit 1d6d829

File tree

14 files changed

+153
-45
lines changed

14 files changed

+153
-45
lines changed

app/Http/Controllers/Articles/ArticlesController.php

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
use App\Jobs\DeleteArticle;
1111
use App\Jobs\UpdateArticle;
1212
use App\Models\Article;
13+
use App\Models\Series;
1314
use App\Models\Tag;
1415
use App\Policies\ArticlePolicy;
1516

@@ -35,8 +36,13 @@ public function create()
3536
{
3637
$tags = Tag::all();
3738
$selectedTags = old('tags') ?: [];
39+
$series = Series::all();
3840

39-
return view('articles.create', ['tags' => $tags, 'selectedTags' => $selectedTags]);
41+
return view('articles.create', [
42+
'tags' => $tags,
43+
'selectedTags' => $selectedTags,
44+
'series' => $series,
45+
]);
4046
}
4147

4248
public function store(ArticleRequest $request)
@@ -52,8 +58,14 @@ public function edit(Article $article)
5258
{
5359
$this->authorize(ArticlePolicy::UPDATE, $article);
5460
$selectedTags = $article->tags()->pluck('id')->toArray();
61+
$series = Series::all();
5562

56-
return view('articles.edit', ['article' => $article, 'tags' => Tag::all(), 'selectedTags' => $selectedTags]);
63+
return view('articles.edit', [
64+
'article' => $article,
65+
'tags' => Tag::all(),
66+
'selectedTags' => $selectedTags,
67+
'series' => $series,
68+
]);
5769
}
5870

5971
public function update(ArticleRequest $request, Article $article)

app/Http/Controllers/Articles/SeriesController.php

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -22,12 +22,10 @@ public function __construct()
2222

2323
public function index()
2424
{
25-
2625
}
2726

2827
public function show(Series $series)
2928
{
30-
3129
}
3230

3331
public function create()
@@ -69,11 +67,11 @@ public function update(SeriesRequest $request, Series $series)
6967
public function delete(Series $series)
7068
{
7169
$this->authorize(SeriesPolicy::DELETE, $series);
72-
70+
7371
$this->dispatchNow(new DeleteSeries($series));
74-
72+
7573
$this->success('series.deleted');
7674

7775
return redirect()->route('series');
7876
}
79-
}
77+
}

app/Http/Requests/ArticleRequest.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,4 +36,9 @@ public function tags(): array
3636
{
3737
return $this->get('tags', []);
3838
}
39+
40+
public function series(): ?string
41+
{
42+
return $this->get('series');
43+
}
3944
}

app/Jobs/CreateArticle.php

Lines changed: 8 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -4,36 +4,28 @@
44

55
use App\Http\Requests\ArticleRequest;
66
use App\Models\Article;
7+
use App\Models\Series;
78
use App\User;
89

910
final class CreateArticle
1011
{
11-
/**
12-
* @var string
13-
*/
1412
private $title;
1513

16-
/**
17-
* @var string
18-
*/
1914
private $body;
2015

21-
/**
22-
* @var \App\User
23-
*/
2416
private $author;
2517

26-
/**
27-
* @var array
28-
*/
2918
private $tags;
3019

31-
public function __construct(string $title, string $body, User $author, array $tags = [])
20+
private $series;
21+
22+
public function __construct(string $title, string $body, User $author, array $tags = [], string $series = null)
3223
{
3324
$this->title = $title;
3425
$this->body = $body;
3526
$this->author = $author;
3627
$this->tags = $tags;
28+
$this->series = $series;
3729
}
3830

3931
public static function fromRequest(ArticleRequest $request): self
@@ -42,7 +34,8 @@ public static function fromRequest(ArticleRequest $request): self
4234
$request->title(),
4335
$request->body(),
4436
$request->author(),
45-
$request->tags()
37+
$request->tags(),
38+
$request->series()
4639
);
4740
}
4841

@@ -55,6 +48,7 @@ public function handle(): Article
5548
]);
5649
$article->authoredBy($this->author);
5750
$article->syncTags($this->tags);
51+
$article->updateSeries(Series::find($this->series));
5852
$article->save();
5953

6054
return $article;

app/Jobs/UpdateArticle.php

Lines changed: 27 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -4,38 +4,49 @@
44

55
use App\Http\Requests\ArticleRequest;
66
use App\Models\Article;
7-
use Illuminate\Support\Arr;
7+
use App\Models\Series;
88

99
final class UpdateArticle
1010
{
1111
private $article;
1212

13-
private $attributes;
13+
private $title;
1414

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)
1622
{
1723
$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;
1928
}
2029

2130
public static function fromRequest(Article $article, ArticleRequest $request): self
2231
{
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+
);
2939
}
3040

3141
public function handle(): Article
3242
{
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));
3950
$this->article->save();
4051

4152
return $this->article;

app/Models/Article.php

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,4 +41,29 @@ public function series()
4141
{
4242
return $this->belongsTo(Series::class);
4343
}
44+
45+
public function updateSeries(Series $series = null): self
46+
{
47+
if (is_null($series)) {
48+
return $this->removeSeries();
49+
}
50+
51+
return $this->addToSeries($series);
52+
}
53+
54+
public function addToSeries(Series $series): self
55+
{
56+
$this->series()->associate($series);
57+
$this->save();
58+
59+
return $this;
60+
}
61+
62+
public function removeSeries(): self
63+
{
64+
$this->series()->dissociate();
65+
$this->save();
66+
67+
return $this;
68+
}
4469
}

resources/css/forms.css

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,9 @@ label {
99
input[type=text],
1010
input[type=password],
1111
input[type=email],
12-
textarea {
13-
@apply rounded border-2 p-3 w-full;
12+
textarea,
13+
select {
14+
@apply rounded border-2 p-3 w-full appearance-none;
1415
}
1516

1617
input.nav-search {

resources/lang/en/series.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,6 @@
44

55
'created' => 'Series successfully created!',
66
'updated' => 'Series successfully updated!',
7-
'deleted' => 'Series successfully deleted!'
7+
'deleted' => 'Series successfully deleted!',
88

9-
];
9+
];

resources/views/articles/_form.blade.php

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,21 @@ class="form-control"
3838
@error('tags')
3939
@endFormGroup
4040

41+
@if($series->count() > 0)
42+
@formGroup('series')
43+
<label for="series">Series</label>
44+
45+
<select name="series">
46+
<option value="">Select a series</option>
47+
@foreach($series as $s)
48+
<option value="{{ $s->id }}" @if(isset($article) && $article->series_id === $s->id) selected @endif>{{ $s->title }}</option>
49+
@endforeach
50+
</select>
51+
52+
@error('tags')
53+
@endFormGroup
54+
@endif
55+
4156
<div class="flex justify-end items-center">
4257
<a href="{{ isset($article) ? route('articles.show', $article->slug()) : route('articles') }}" class="text-green-darker mr-4">Cancel</a>
4358
<button type="submit" class="button button-primary">{{ isset($article) ? 'Update Article' : 'Create Article' }}</button>

resources/views/series/create.blade.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
<div class="container mx-auto p-4 flex justify-center">
77
<div class="w-full md:w-2/3 xl:w-1/2">
88
<div class="md:p-4 md:border-2 md:rounded md:bg-gray-100">
9-
@include('articles.series._form', [
9+
@include('series._form', [
1010
'route' => ['series.store'],
1111
])
1212
</div>

0 commit comments

Comments
 (0)