Skip to content

Commit f49a712

Browse files
committed
Delete series
1 parent 0775cef commit f49a712

File tree

7 files changed

+92
-0
lines changed

7 files changed

+92
-0
lines changed

app/Http/Controllers/Articles/SeriesController.php

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
use App\Http\Middleware\RedirectIfUnconfirmed;
88
use App\Http\Requests\SeriesRequest;
99
use App\Jobs\CreateSeries;
10+
use App\Jobs\DeleteSeries;
1011
use App\Jobs\UpdateSeries;
1112
use App\Models\Series;
1213
use App\Models\Tag;
@@ -19,6 +20,11 @@ public function __construct()
1920
$this->middleware([Authenticate::class, RedirectIfUnconfirmed::class], ['except' => ['index', 'show']]);
2021
}
2122

23+
public function index()
24+
{
25+
26+
}
27+
2228
public function show(Series $series)
2329
{
2430

@@ -59,4 +65,15 @@ public function update(SeriesRequest $request, Series $series)
5965

6066
return redirect()->route('series.show', $series->slug());
6167
}
68+
69+
public function delete(Series $series)
70+
{
71+
$this->authorize(SeriesPolicy::DELETE, $series);
72+
73+
$this->dispatchNow(new DeleteSeries($series));
74+
75+
$this->success('series.deleted');
76+
77+
return redirect()->route('series');
78+
}
6279
}

app/Jobs/DeleteSeries.php

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
<?php
2+
3+
namespace App\Jobs;
4+
5+
use App\Models\Series;
6+
7+
final class DeleteSeries
8+
{
9+
private $series;
10+
11+
public function __construct(Series $series)
12+
{
13+
$this->series = $series;
14+
}
15+
16+
public function handle()
17+
{
18+
$this->series->delete();
19+
}
20+
}

database/factories/SeriesFactory.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
<?php
22

33
use App\Models\Series;
4+
use App\User;
45
use Faker\Generator as Faker;
56

67
$factory->define(Series::class, function (Faker $faker) {
@@ -9,5 +10,6 @@
910
return factory(User::class)->create()->id;
1011
},
1112
'title' => $faker->word,
13+
'slug' => $faker->unique()->slug,
1214
];
1315
});

resources/lang/en/series.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,5 +4,6 @@
44

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

89
];

routes/web.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,11 +90,13 @@
9090

9191
// Series
9292
Route::prefix('series')->namespace('Articles')->group(function () {
93+
Route::get('/', 'SeriesController@index')->name('series');
9394
Route::get('/create', 'SeriesController@create')->name('series.create');
9495
Route::post('/', 'SeriesController@store')->name('series.store');
9596
Route::get('{series}', 'SeriesController@show')->name('series.show');
9697
Route::get('{series}/edit', 'SeriesController@edit')->name('series.edit');
9798
Route::put('{series}', 'SeriesController@update')->name('series.update');
99+
Route::delete('{series}', 'SeriesController@delete')->name('series.delete');
98100
});
99101

100102
// Admin

tests/Feature/SeriesTest.php

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,4 +83,31 @@ public function users_cannot_edit_a_series_with_a_title_that_is_too_long()
8383
$response->assertSessionHas('error', 'Something went wrong. Please review the fields below.');
8484
$response->assertSessionHasErrors(['title' => 'The title may not be greater than 100 characters.']);
8585
}
86+
87+
/** @test */
88+
public function users_can_delete_their_own_series()
89+
{
90+
$user = $this->createUser();
91+
factory(Series::class)->create([
92+
'author_id' => $user->id(),
93+
'slug' => 'my-first-series',
94+
]);
95+
96+
$this->loginAs($user);
97+
98+
$this->delete('/series/my-first-series')
99+
->assertRedirectedTo('/series')
100+
->assertSessionHas('success', 'Series successfully deleted!');
101+
}
102+
103+
/** @test */
104+
public function users_cannot_delete_a_series_they_do_not_own()
105+
{
106+
factory(Series::class)->create(['slug' => 'my-first-thread']);
107+
108+
$this->login();
109+
110+
$this->delete('/series/my-first-thread')
111+
->assertForbidden();
112+
}
86113
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
<?php
2+
3+
namespace Tests\Integration\Jobs;
4+
5+
use App\Jobs\DeleteSeries;
6+
use App\Models\Series;
7+
use Illuminate\Foundation\Testing\DatabaseMigrations;
8+
use Tests\TestCase;
9+
10+
class DeleteSeriesTest extends TestCase
11+
{
12+
use DatabaseMigrations;
13+
14+
/** @test */
15+
public function a_series_can_be_deleted()
16+
{
17+
$series = factory(Series::class)->create();
18+
19+
$this->dispatch(new DeleteSeries($series));
20+
21+
$this->assertDatabaseMissing('series', ['id' => $series->id()]);
22+
}
23+
}

0 commit comments

Comments
 (0)