Skip to content

Commit 4d06d97

Browse files
committed
Added Events and Fixed Tests
1 parent 72f9370 commit 4d06d97

11 files changed

+172
-31
lines changed

app/Events/UserRegistered.php

+30
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
<?php
2+
3+
namespace App\Events;
4+
5+
use App\Models\User;
6+
use Illuminate\Broadcasting\Channel;
7+
use Illuminate\Broadcasting\InteractsWithSockets;
8+
use Illuminate\Broadcasting\PresenceChannel;
9+
use Illuminate\Broadcasting\PrivateChannel;
10+
use Illuminate\Contracts\Broadcasting\ShouldBroadcast;
11+
use Illuminate\Foundation\Events\Dispatchable;
12+
use Illuminate\Queue\SerializesModels;
13+
14+
class UserRegistered
15+
{
16+
use Dispatchable, InteractsWithSockets, SerializesModels;
17+
18+
/**
19+
* @var User
20+
*/
21+
protected User $user;
22+
23+
/**
24+
* @param User $user
25+
*/
26+
public function __construct(User $user)
27+
{
28+
$this->user = $user;
29+
}
30+
}

app/Http/Controllers/API/Auth/RegisterController.php

+3
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
use App\Http\Requests\RegisterRequest;
66
use App\Constants\AuthConstants;
7+
use App\Events\UserRegistered;
78
use App\Http\Controllers\Controller;
89
use App\Http\Traits\HttpResponses;
910
use App\Models\User;
@@ -25,6 +26,8 @@ public function __invoke(RegisterRequest $request): JsonResponse
2526
$success['token'] = $user->createToken('MyApp')->plainTextToken;
2627
$success['name'] = $user->name;
2728

29+
event(new UserRegistered($user));
30+
2831
return $this->success($success, AuthConstants::REGISTER);
2932
}
3033
}

app/Http/Controllers/API/CategoryController.php

+2-3
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,7 @@
1414

1515
class CategoryController extends Controller
1616
{
17-
use HttpResponses;
18-
use Access;
17+
use HttpResponses, Access;
1918

2019
/**
2120
* @return JsonResponse
@@ -34,7 +33,7 @@ public function index(): JsonResponse
3433
public function store(CategoryRequest $request): JsonResponse
3534
{
3635
return $this->success(
37-
new CategoryResource(Category::create($request->all())),
36+
new CategoryResource(auth()->user()->categories()->create($request->all())),
3837
CategoryConstants::STORE
3938
);
4039
}

app/Http/Controllers/API/ProductController.php

+2-3
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,7 @@
1515

1616
class ProductController extends Controller
1717
{
18-
use HttpResponses;
19-
use Access;
18+
use HttpResponses, Access;
2019

2120
/**
2221
* @return JsonResponse
@@ -32,7 +31,7 @@ public function index(): JsonResponse
3231
*/
3332
public function store(ProductRequest $request): JsonResponse
3433
{
35-
$product = Product::create($request->all());
34+
$product = auth()->user()->products()->create($request->all());
3635

3736
if (isset($request->categories)) {
3837
$categories = Category::ForUserByIds($request->categories);
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
<?php
2+
3+
namespace App\Listeners;
4+
5+
use App\Events\UserRegistered;
6+
use Illuminate\Contracts\Queue\ShouldQueue;
7+
use Illuminate\Queue\InteractsWithQueue;
8+
9+
class UserRegisteredEmailNotification implements ShouldQueue
10+
{
11+
/**
12+
* Create the event listener.
13+
*/
14+
public function __construct()
15+
{
16+
//
17+
}
18+
19+
/**
20+
* Handle the event.
21+
*/
22+
public function handle(UserRegistered $event): void
23+
{
24+
// TODO: Send Email
25+
}
26+
}

app/Models/Category.php

-12
Original file line numberDiff line numberDiff line change
@@ -21,18 +21,6 @@ class Category extends Model
2121
'name',
2222
];
2323

24-
/**
25-
* @return void
26-
*/
27-
protected static function booted(): void
28-
{
29-
if (Auth::check()) {
30-
static::creating(function ($product) {
31-
$product->user_id = Auth::id();
32-
});
33-
}
34-
}
35-
3624
/**
3725
* @return BelongsToMany
3826
*/

app/Models/Product.php

-12
Original file line numberDiff line numberDiff line change
@@ -20,18 +20,6 @@ class Product extends Model
2020
'price',
2121
];
2222

23-
/**
24-
* @return void
25-
*/
26-
protected static function booted(): void
27-
{
28-
if (Auth::check()) {
29-
static::creating(function ($product) {
30-
$product->user_id = Auth::id();
31-
});
32-
}
33-
}
34-
3523
/**
3624
* @return BelongsToMany
3725
*/

app/Observers/CategoryObserver.php

+48
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
<?php
2+
3+
namespace App\Observers;
4+
5+
use App\Models\Category;
6+
7+
class CategoryObserver
8+
{
9+
/**
10+
* Handle the Category "created" event.
11+
*/
12+
public function created(Category $category): void
13+
{
14+
//
15+
}
16+
17+
/**
18+
* Handle the Category "updated" event.
19+
*/
20+
public function updated(Category $category): void
21+
{
22+
//
23+
}
24+
25+
/**
26+
* Handle the Category "deleted" event.
27+
*/
28+
public function deleted(Category $category): void
29+
{
30+
//
31+
}
32+
33+
/**
34+
* Handle the Category "restored" event.
35+
*/
36+
public function restored(Category $category): void
37+
{
38+
//
39+
}
40+
41+
/**
42+
* Handle the Category "force deleted" event.
43+
*/
44+
public function forceDeleted(Category $category): void
45+
{
46+
//
47+
}
48+
}

app/Observers/ProductObserver.php

+48
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
<?php
2+
3+
namespace App\Observers;
4+
5+
use App\Models\Product;
6+
7+
class ProductObserver
8+
{
9+
/**
10+
* Handle the Product "created" event.
11+
*/
12+
public function created(Product $product): void
13+
{
14+
//
15+
}
16+
17+
/**
18+
* Handle the Product "updated" event.
19+
*/
20+
public function updated(Product $product): void
21+
{
22+
//
23+
}
24+
25+
/**
26+
* Handle the Product "deleted" event.
27+
*/
28+
public function deleted(Product $product): void
29+
{
30+
//
31+
}
32+
33+
/**
34+
* Handle the Product "restored" event.
35+
*/
36+
public function restored(Product $product): void
37+
{
38+
//
39+
}
40+
41+
/**
42+
* Handle the Product "force deleted" event.
43+
*/
44+
public function forceDeleted(Product $product): void
45+
{
46+
//
47+
}
48+
}

app/Providers/EventServiceProvider.php

+11-1
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,12 @@
22

33
namespace App\Providers;
44

5+
use App\Events\UserRegistered;
6+
use App\Listeners\UserRegisteredEmailNotification;
7+
use App\Models\Category;
8+
use App\Models\Product;
9+
use App\Observers\CategoryObserver;
10+
use App\Observers\ProductObserver;
511
use Illuminate\Auth\Events\Registered;
612
use Illuminate\Auth\Listeners\SendEmailVerificationNotification;
713
use Illuminate\Foundation\Support\Providers\EventServiceProvider as ServiceProvider;
@@ -18,14 +24,18 @@ class EventServiceProvider extends ServiceProvider
1824
Registered::class => [
1925
SendEmailVerificationNotification::class,
2026
],
27+
UserRegistered::class => [
28+
UserRegisteredEmailNotification::class,
29+
],
2130
];
2231

2332
/**
2433
* Register any events for your application.
2534
*/
2635
public function boot(): void
2736
{
28-
//
37+
Category::observe(new CategoryObserver());
38+
Product::observe(new ProductObserver());
2939
}
3040

3141
/**

database/factories/ProductFactory.php

+2
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
namespace Database\Factories;
44

5+
use App\Models\User;
56
use Illuminate\Database\Eloquent\Factories\Factory;
67

78
/**
@@ -19,6 +20,7 @@ public function definition(): array
1920
return [
2021
'name' => fake()->name(),
2122
'price' => fake()->numberBetween(1, 100),
23+
'user_id' => User::factory(),
2224
];
2325
}
2426
}

0 commit comments

Comments
 (0)