Extract controller delete blocks to methods

This commit is contained in:
Igor Scheller 2024-04-13 14:36:46 +02:00 committed by xuwhite
parent dd096b0f46
commit f397d809de
4 changed files with 32 additions and 20 deletions

View File

@ -47,13 +47,7 @@ class FaqController extends BaseController
$faq = $this->faq->findOrNew($faqId);
if ($request->request->has('delete')) {
$faq->delete();
$this->log->info('Deleted faq "{question}"', ['question' => $faq->question]);
$this->addNotification('faq.delete.success');
return $this->redirect->to('/faq');
return $this->delete($faq);
}
$data = $this->validate($request, [
@ -79,6 +73,17 @@ class FaqController extends BaseController
return $this->redirect->to('/faq#faq-' . $faq->id);
}
protected function delete(Faq $faq): Response
{
$faq->delete();
$this->log->info('Deleted faq "{question}"', ['question' => $faq->question]);
$this->addNotification('faq.delete.success');
return $this->redirect->to('/faq');
}
protected function showEdit(?Faq $faq): Response
{
return $this->response->withView(

View File

@ -67,19 +67,7 @@ class NewsController extends BaseController
$news = $this->news->findOrNew($newsId);
if ($request->request->has('delete')) {
$news->delete();
$this->log->info(
'Deleted {type} "{news}"',
[
'type' => $news->is_meeting ? 'meeting' : 'news',
'news' => $news->title,
]
);
$this->addNotification('news.delete.success');
return $this->redirect->to('/news');
return $this->delete($news);
}
$data = $this->validate($request, [
@ -138,4 +126,21 @@ class NewsController extends BaseController
return $this->redirect->to('/news');
}
protected function delete(News $news): Response
{
$news->delete();
$this->log->info(
'Deleted {type} "{news}"',
[
'type' => $news->is_meeting ? 'meeting' : 'news',
'news' => $news->title,
]
);
$this->addNotification('news.delete.success');
return $this->redirect->to('/news');
}
}

View File

@ -123,6 +123,7 @@ class FaqControllerTest extends ControllerTest
/**
* @covers \Engelsystem\Controllers\Admin\FaqController::save
* @covers \Engelsystem\Controllers\Admin\FaqController::delete
*/
public function testSaveDelete(): void
{

View File

@ -224,6 +224,7 @@ class NewsControllerTest extends ControllerTest
/**
* @covers \Engelsystem\Controllers\Admin\NewsController::save
* @covers \Engelsystem\Controllers\Admin\NewsController::delete
*/
public function testSaveDelete(): void
{