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:
2018-09-30 21:32:33 -03:00
parent 8575895341
commit 1e1c178214
5 changed files with 99 additions and 14 deletions

View File

@@ -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');
}
}