mirror of
https://github.com/brunofontes/shareit.git
synced 2025-01-19 06:50:48 +00:00
Bruno Fontes
1f9da456a5
It happend once: a user asked to be added to the waiting list at the same time other user was returning the item. So the user ended up waiting for the user he was already using.
54 lines
1.1 KiB
PHP
54 lines
1.1 KiB
PHP
<?php
|
|
|
|
namespace App\Http\Controllers;
|
|
|
|
use Auth;
|
|
use Lang;
|
|
use App\Item;
|
|
use App\User;
|
|
use App\Events\ReturnItem;
|
|
use Illuminate\Http\Request;
|
|
|
|
/**
|
|
* Responsible to Take and Return an Item.
|
|
*/
|
|
class TakeController extends Controller
|
|
{
|
|
/**
|
|
* The user take an item
|
|
*
|
|
* @param Request $request The form data
|
|
*
|
|
* @return home view
|
|
*/
|
|
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->waiting_user_id = null;
|
|
$item->save();
|
|
return redirect('home');
|
|
}
|
|
|
|
/**
|
|
* User return an item
|
|
* Trigger an event: ReturnItem
|
|
*
|
|
* @param Request $request Form data
|
|
*
|
|
* @return View home
|
|
*/
|
|
public function delete(Request $request)
|
|
{
|
|
$item = User::loggedIn()->items()->find(request('item'));
|
|
event(new ReturnItem($item));
|
|
$item->returnItem();
|
|
return redirect('home');
|
|
}
|
|
}
|