close #2799 Fixed: Company delete can not delete sub categories

This commit is contained in:
Cüneyt Şentürk 2022-12-07 11:43:38 +03:00
parent cba5dd519c
commit 86a44fb3e5
13 changed files with 186 additions and 28 deletions

View File

@ -0,0 +1,24 @@
<?php
namespace App\Events\Setting;
use App\Abstracts\Event;
class CategoryCreated extends Event
{
public $category;
public $request;
/**
* Create a new event instance.
*
* @param $category
* @param $request
*/
public function __construct($category, $request)
{
$this->category = $category;
$this->request = $request;
}
}

View File

@ -0,0 +1,22 @@
<?php
namespace App\Events\Setting;
use Illuminate\Queue\SerializesModels;
class CategoryCreating
{
use SerializesModels;
public $request;
/**
* Create a new event instance.
*
* @param $request
*/
public function __construct($request)
{
$this->request = $request;
}
}

View File

@ -0,0 +1,20 @@
<?php
namespace App\Events\Setting;
use App\Abstracts\Event;
class CategoryDeleted extends Event
{
public $category;
/**
* Create a new event instance.
*
* @param $category
*/
public function __construct($category)
{
$this->category = $category;
}
}

View File

@ -0,0 +1,20 @@
<?php
namespace App\Events\Setting;
use App\Abstracts\Event;
class CategoryDeleting extends Event
{
public $category;
/**
* Create a new event instance.
*
* @param $category
*/
public function __construct($category)
{
$this->category = $category;
}
}

View File

@ -0,0 +1,24 @@
<?php
namespace App\Events\Setting;
use App\Abstracts\Event;
class CategoryUpdated extends Event
{
public $category;
public $request;
/**
* Create a new event instance.
*
* @param $category
* @param $request
*/
public function __construct($category, $request)
{
$this->category = $category;
$this->request = $request;
}
}

View File

@ -0,0 +1,24 @@
<?php
namespace App\Events\Setting;
use App\Abstracts\Event;
class CategoryUpdating extends Event
{
public $category;
public $request;
/**
* Create a new event instance.
*
* @param $category
* @param $request
*/
public function __construct($category, $request)
{
$this->category = $category;
$this->request = $request;
}
}

View File

@ -3,6 +3,8 @@
namespace App\Jobs\Setting; namespace App\Jobs\Setting;
use App\Abstracts\Job; use App\Abstracts\Job;
use App\Events\Setting\CategoryCreated;
use App\Events\Setting\CategoryCreating;
use App\Interfaces\Job\HasOwner; use App\Interfaces\Job\HasOwner;
use App\Interfaces\Job\HasSource; use App\Interfaces\Job\HasSource;
use App\Interfaces\Job\ShouldCreate; use App\Interfaces\Job\ShouldCreate;
@ -12,10 +14,14 @@ class CreateCategory extends Job implements HasOwner, HasSource, ShouldCreate
{ {
public function handle(): Category public function handle(): Category
{ {
event(new CategoryCreating($this->request));
\DB::transaction(function () { \DB::transaction(function () {
$this->model = Category::create($this->request->all()); $this->model = Category::create($this->request->all());
}); });
event(new CategoryCreated($this->model, $this->request));
return $this->model; return $this->model;
} }
} }

View File

@ -3,6 +3,8 @@
namespace App\Jobs\Setting; namespace App\Jobs\Setting;
use App\Abstracts\Job; use App\Abstracts\Job;
use App\Events\Setting\CategoryDeleted;
use App\Events\Setting\CategoryDeleting;
use App\Exceptions\Settings\LastCategoryDelete; use App\Exceptions\Settings\LastCategoryDelete;
use App\Interfaces\Job\ShouldDelete; use App\Interfaces\Job\ShouldDelete;
use App\Models\Setting\Category; use App\Models\Setting\Category;
@ -13,10 +15,14 @@ class DeleteCategory extends Job implements ShouldDelete
{ {
$this->authorize(); $this->authorize();
event(new CategoryDeleting($this->model));
\DB::transaction(function () { \DB::transaction(function () {
$this->model->delete(); $this->model->delete();
}); });
event(new CategoryDeleted($this->model));
return true; return true;
} }

View File

@ -3,6 +3,8 @@
namespace App\Jobs\Setting; namespace App\Jobs\Setting;
use App\Abstracts\Job; use App\Abstracts\Job;
use App\Events\Setting\CategoryUpdated;
use App\Events\Setting\CategoryUpdating;
use App\Interfaces\Job\ShouldUpdate; use App\Interfaces\Job\ShouldUpdate;
use App\Models\Setting\Category; use App\Models\Setting\Category;
@ -12,10 +14,14 @@ class UpdateCategory extends Job implements ShouldUpdate
{ {
$this->authorize(); $this->authorize();
event(new CategoryUpdating($this->model, $this->request));
\DB::transaction(function () { \DB::transaction(function () {
$this->model->update($this->request->all()); $this->model->update($this->request->all());
}); });
event(new CategoryUpdated($this->model, $this->request));
return $this->model; return $this->model;
} }

View File

@ -0,0 +1,31 @@
<?php
namespace App\Listeners\Setting;
use App\Events\Setting\CategoryDeleted as Event;
use App\Jobs\Setting\DeleteCategory;
use App\Traits\Jobs;
class DeleteCategoryDeletedSubCategories
{
use Jobs;
/**
* Handle the event.
*
* @param $event
* @return void
*/
public function handle(Event $event)
{
$category = $event->category;
if (empty($category->sub_categories)) {
return;
}
foreach ($category->sub_categories as $sub_category) {
$this->dispatch(new DeleteCategory($sub_category));
}
}
}

View File

@ -1,26 +0,0 @@
<?php
namespace App\Observers;
use App\Abstracts\Observer;
use App\Jobs\Setting\DeleteCategory;
use App\Models\Setting\Category as Model;
use App\Traits\Jobs;
class Category extends Observer
{
use Jobs;
/**
* Listen to the deleted event.
*
* @param Model $category
* @return void
*/
public function deleting(Model $category)
{
foreach ($category->sub_categories as $sub_category) {
$this->dispatch(new DeleteCategory($sub_category));
}
}
}

View File

@ -100,6 +100,9 @@ class Event extends Provider
'App\Events\Banking\TransactionCreated' => [ 'App\Events\Banking\TransactionCreated' => [
'App\Listeners\Banking\IncreaseNextTransactionNumber', 'App\Listeners\Banking\IncreaseNextTransactionNumber',
], ],
'App\Events\Setting\CategoryDeleted' => [
'App\Listeners\Setting\DeleteCategoryDeletedSubCategories',
],
]; ];
/** /**

View File

@ -3,7 +3,6 @@
namespace App\Providers; namespace App\Providers;
use App\Models\Banking\Transaction; use App\Models\Banking\Transaction;
use App\Models\Setting\Category;
use Illuminate\Support\ServiceProvider as Provider; use Illuminate\Support\ServiceProvider as Provider;
class Observer extends Provider class Observer extends Provider
@ -26,6 +25,5 @@ class Observer extends Provider
public function boot() public function boot()
{ {
Transaction::observe('App\Observers\Transaction'); Transaction::observe('App\Observers\Transaction');
Category::observe('App\Observers\Category');
} }
} }