shareit/app/Http/Controllers/TakeController.php

65 lines
1.4 KiB
PHP
Raw Normal View History

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