2018-09-13 00:27:35 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace App\Http\Controllers;
|
|
|
|
|
2022-11-14 00:04:45 +00:00
|
|
|
use App\Events\RefreshPage;
|
2021-05-20 23:37:35 +00:00
|
|
|
use App\Events\ReturnItem;
|
2018-10-16 23:18:10 +00:00
|
|
|
use App\Item;
|
|
|
|
use App\User;
|
2018-10-10 03:46:28 +00:00
|
|
|
use Illuminate\Http\Request;
|
2021-05-20 23:37:35 +00:00
|
|
|
use Lang;
|
2018-09-13 00:27:35 +00:00
|
|
|
|
2018-10-10 03:46:28 +00:00
|
|
|
/**
|
|
|
|
* Responsible to Take and Return an Item.
|
|
|
|
*/
|
2018-09-13 00:27:35 +00:00
|
|
|
class TakeController extends Controller
|
|
|
|
{
|
2018-10-10 03:46:28 +00:00
|
|
|
/**
|
|
|
|
* The user take an item
|
|
|
|
*
|
|
|
|
* @param Request $request The form data
|
2021-05-20 23:37:35 +00:00
|
|
|
*
|
2018-10-10 03:46:28 +00:00
|
|
|
* @return home view
|
|
|
|
*/
|
2018-09-13 00:27:35 +00:00
|
|
|
public function store(Request $request)
|
|
|
|
{
|
2018-09-19 17:58:10 +00:00
|
|
|
$item = User::loggedIn()->items()->find(request('item'));
|
2018-10-21 15:36:02 +00:00
|
|
|
|
|
|
|
try {
|
|
|
|
$item->takeItem();
|
|
|
|
} catch (\Exception $e) {
|
2018-09-27 16:00:03 +00:00
|
|
|
return back()->withErrors(
|
2018-10-21 15:36:02 +00:00
|
|
|
Lang::getFromJson('This item is already taken')
|
2018-09-27 16:00:03 +00:00
|
|
|
);
|
2018-09-17 14:45:05 +00:00
|
|
|
}
|
2018-10-21 15:36:02 +00:00
|
|
|
|
2022-11-14 00:04:45 +00:00
|
|
|
RefreshPage::dispatch($item);
|
2018-09-13 00:27:35 +00:00
|
|
|
return redirect('home');
|
|
|
|
}
|
2018-09-15 03:22:57 +00:00
|
|
|
|
2018-10-10 03:46:28 +00:00
|
|
|
/**
|
|
|
|
* User return an item
|
|
|
|
* Trigger an event: ReturnItem
|
|
|
|
*
|
|
|
|
* @param Request $request Form data
|
2021-05-20 23:37:35 +00:00
|
|
|
*
|
2018-10-10 03:46:28 +00:00
|
|
|
* @return View home
|
|
|
|
*/
|
2018-09-15 03:22:57 +00:00
|
|
|
public function delete(Request $request)
|
|
|
|
{
|
2018-09-19 17:58:10 +00:00
|
|
|
$item = User::loggedIn()->items()->find(request('item'));
|
2018-10-21 15:36:02 +00:00
|
|
|
|
|
|
|
try {
|
|
|
|
$item->returnItem();
|
|
|
|
} catch (\Exception $e) {
|
|
|
|
return back()->withErrors(
|
|
|
|
Lang::getFromJson("You cannot return an item that is not with you")
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2022-11-14 00:04:45 +00:00
|
|
|
RefreshPage::dispatch($item);
|
|
|
|
ReturnItem::dispatch($item);
|
2018-09-15 03:22:57 +00:00
|
|
|
return redirect('home');
|
|
|
|
}
|
2018-09-13 00:27:35 +00:00
|
|
|
}
|