mirror of
https://github.com/brunofontes/shareit.git
synced 2024-11-24 04:14:57 +00:00
Refactoring: TakeController returned item Mail moved to a listener
The TakeController was manually sending the email message as well as dealing with item return, so I moved the email message to it's own listener, I created an event for it and moved the return part to the item model.
This commit is contained in:
parent
4ec2b24885
commit
528d3b4caf
38
app/Events/ReturnItem.php
Normal file
38
app/Events/ReturnItem.php
Normal file
@ -0,0 +1,38 @@
|
||||
<?php
|
||||
|
||||
namespace App\Events;
|
||||
|
||||
use \App\Item;
|
||||
use Illuminate\Broadcasting\Channel;
|
||||
use Illuminate\Queue\SerializesModels;
|
||||
use Illuminate\Broadcasting\PrivateChannel;
|
||||
use Illuminate\Broadcasting\PresenceChannel;
|
||||
use Illuminate\Foundation\Events\Dispatchable;
|
||||
use Illuminate\Broadcasting\InteractsWithSockets;
|
||||
use Illuminate\Contracts\Broadcasting\ShouldBroadcast;
|
||||
|
||||
class ReturnItem
|
||||
{
|
||||
use Dispatchable, InteractsWithSockets, SerializesModels;
|
||||
public $item;
|
||||
|
||||
/**
|
||||
* Create a new event instance.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function __construct(Item $item)
|
||||
{
|
||||
$this->item = $item;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the channels the event should broadcast on.
|
||||
*
|
||||
* @return \Illuminate\Broadcasting\Channel|array
|
||||
*/
|
||||
public function broadcastOn()
|
||||
{
|
||||
return new PrivateChannel('channel-name');
|
||||
}
|
||||
}
|
@ -4,8 +4,8 @@ namespace App\Http\Controllers;
|
||||
|
||||
use \App\Item;
|
||||
use \App\User;
|
||||
use App\Mail\ItemAvailable;
|
||||
use Illuminate\Http\Request;
|
||||
use App\Events\ReturnItem;
|
||||
|
||||
class TakeController extends Controller
|
||||
{
|
||||
@ -27,19 +27,8 @@ class TakeController extends Controller
|
||||
public function delete(Request $request)
|
||||
{
|
||||
$item = User::loggedIn()->items()->find(request('item'));
|
||||
$waiting_id = $item->waiting_user_id;
|
||||
$item->used_by = null;
|
||||
$item->waiting_user_id = null;
|
||||
$item->save();
|
||||
|
||||
//Send e-mail to waiting user
|
||||
if ($waiting_id) {
|
||||
$user = User::find($waiting_id);
|
||||
\Mail::to($user)->send(
|
||||
new ItemAvailable($user->name, $item)
|
||||
);
|
||||
}
|
||||
|
||||
event(new ReturnItem($item));
|
||||
$item->returnItem();
|
||||
return redirect('home');
|
||||
}
|
||||
}
|
||||
|
12
app/Item.php
12
app/Item.php
@ -38,4 +38,16 @@ class Item extends Model
|
||||
{
|
||||
return (new static)->where('user_id', \Auth::id());
|
||||
}
|
||||
|
||||
/**
|
||||
* Return a specified item
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function returnItem()
|
||||
{
|
||||
$this->used_by = null;
|
||||
$this->waiting_user_id = null;
|
||||
$this->save();
|
||||
}
|
||||
}
|
||||
|
40
app/Listeners/AlertReturnedItem.php
Normal file
40
app/Listeners/AlertReturnedItem.php
Normal file
@ -0,0 +1,40 @@
|
||||
<?php
|
||||
|
||||
namespace App\Listeners;
|
||||
|
||||
use Mail;
|
||||
use App\User;
|
||||
use App\Events\ReturnItem;
|
||||
use App\Mail\ItemAvailable;
|
||||
use Illuminate\Queue\InteractsWithQueue;
|
||||
use Illuminate\Contracts\Queue\ShouldQueue;
|
||||
|
||||
class AlertReturnedItem
|
||||
{
|
||||
/**
|
||||
* Create the event listener.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function __construct()
|
||||
{
|
||||
//
|
||||
}
|
||||
|
||||
/**
|
||||
* Send an email to the user that
|
||||
* is waiting for the item
|
||||
*
|
||||
* @param ReturnItem $item
|
||||
* @return void
|
||||
*/
|
||||
public function handle(ReturnItem $event)
|
||||
{
|
||||
if ($event->item->waiting_user_id) {
|
||||
$user = User::find($event->item->waiting_user_id);
|
||||
Mail::to($user)->send(
|
||||
new ItemAvailable($user->name, $event->item)
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
@ -6,6 +6,8 @@ use Illuminate\Support\Facades\Event;
|
||||
use Illuminate\Auth\Events\Registered;
|
||||
use Illuminate\Auth\Listeners\SendEmailVerificationNotification;
|
||||
use Illuminate\Foundation\Support\Providers\EventServiceProvider as ServiceProvider;
|
||||
use App\Events\ReturnItem;
|
||||
use App\Listeners\AlertReturnedItem;
|
||||
|
||||
class EventServiceProvider extends ServiceProvider
|
||||
{
|
||||
@ -18,6 +20,10 @@ class EventServiceProvider extends ServiceProvider
|
||||
Registered::class => [
|
||||
SendEmailVerificationNotification::class,
|
||||
],
|
||||
|
||||
ReturnItem::class => [
|
||||
AlertReturnedItem::class,
|
||||
],
|
||||
];
|
||||
|
||||
/**
|
||||
|
Loading…
Reference in New Issue
Block a user