mirror of
https://github.com/ad-aures/castopod.git
synced 2026-04-11 10:46:43 +02:00
fix: unpublish episode before deleting it + add validation step before deletion
fixes #112, closes #55
This commit is contained in:
parent
b9db936461
commit
f75bd76458
9 changed files with 114 additions and 5 deletions
|
|
@ -684,9 +684,63 @@ class EpisodeController extends BaseController
|
|||
return redirect()->route('episode-view', [$this->podcast->id, $this->episode->id]);
|
||||
}
|
||||
|
||||
public function delete(): RedirectResponse
|
||||
public function delete(): string
|
||||
{
|
||||
(new EpisodeModel())->delete($this->episode->id);
|
||||
helper(['form']);
|
||||
|
||||
$data = [
|
||||
'podcast' => $this->podcast,
|
||||
'episode' => $this->episode,
|
||||
];
|
||||
|
||||
replace_breadcrumb_params([
|
||||
0 => $this->podcast->title,
|
||||
1 => $this->episode->title,
|
||||
]);
|
||||
return view('episode/delete', $data);
|
||||
}
|
||||
|
||||
public function attemptDelete(): RedirectResponse
|
||||
{
|
||||
$rules = [
|
||||
'understand' => 'required',
|
||||
];
|
||||
|
||||
if (! $this->validate($rules)) {
|
||||
return redirect()
|
||||
->back()
|
||||
->withInput()
|
||||
->with('errors', $this->validator->getErrors());
|
||||
}
|
||||
|
||||
$db = db_connect();
|
||||
|
||||
$db->transStart();
|
||||
|
||||
$allPostsLinkedToEpisode = (new PostModel())
|
||||
->where([
|
||||
'episode_id' => $this->episode->id,
|
||||
])
|
||||
->findAll();
|
||||
foreach ($allPostsLinkedToEpisode as $post) {
|
||||
(new PostModel())->removePost($post);
|
||||
}
|
||||
|
||||
// set episode published_at to null to unpublish before deletion
|
||||
$this->episode->published_at = null;
|
||||
|
||||
$episodeModel = new EpisodeModel();
|
||||
if (! $episodeModel->update($this->episode->id, $this->episode)) {
|
||||
$db->transRollback();
|
||||
return redirect()
|
||||
->back()
|
||||
->withInput()
|
||||
->with('errors', $episodeModel->errors());
|
||||
}
|
||||
|
||||
$episodeModel->delete($this->episode->id);
|
||||
|
||||
$db->transComplete();
|
||||
|
||||
return redirect()->route('episode-list', [$this->podcast->id]);
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue