shareit/app/Http/Controllers/TakeController.php
Bruno Fontes 528d3b4caf
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.
2018-09-30 21:37:44 -03:00

35 lines
808 B
PHP

<?php
namespace App\Http\Controllers;
use \App\Item;
use \App\User;
use Illuminate\Http\Request;
use App\Events\ReturnItem;
class TakeController extends Controller
{
public function store(Request $request)
{
$item = User::loggedIn()->items()->find(request('item'));
if ($item->used_by) {
return back()->withErrors(
\Lang::getFromJson(
"This item is already taken"
)
);
}
$item->used_by = \Auth::id();
$item->save();
return redirect('home');
}
public function delete(Request $request)
{
$item = User::loggedIn()->items()->find(request('item'));
event(new ReturnItem($item));
$item->returnItem();
return redirect('home');
}
}