mirror of
https://github.com/brunofontes/shareit.git
synced 2024-11-23 20:10:52 +00:00
Refactoring
Including some use for classes; REfactoring the HomeController, to make it cleaner and avoid repeating code.
This commit is contained in:
parent
a85b971ec5
commit
1c1c42df7b
@ -19,6 +19,8 @@ class ReturnItem
|
|||||||
/**
|
/**
|
||||||
* Create a new event instance.
|
* Create a new event instance.
|
||||||
*
|
*
|
||||||
|
* @param Item $item The returned item.
|
||||||
|
*
|
||||||
* @return void
|
* @return void
|
||||||
*/
|
*/
|
||||||
public function __construct(Item $item)
|
public function __construct(Item $item)
|
||||||
|
@ -3,12 +3,22 @@
|
|||||||
namespace App\Http\Controllers;
|
namespace App\Http\Controllers;
|
||||||
|
|
||||||
use Auth;
|
use Auth;
|
||||||
|
use Mail;
|
||||||
use \App\User;
|
use \App\User;
|
||||||
use \App\Mail\UserWaiting;
|
use \App\Mail\UserWaiting;
|
||||||
use Illuminate\Http\Request;
|
use Illuminate\Http\Request;
|
||||||
|
|
||||||
class AlertController extends Controller
|
class AlertController extends Controller
|
||||||
{
|
{
|
||||||
|
/**
|
||||||
|
* Store the waiting_user_id on db
|
||||||
|
* so the user can be alerted when
|
||||||
|
* the item is free
|
||||||
|
*
|
||||||
|
* @param Request $request Form data
|
||||||
|
*
|
||||||
|
* @return redirect to home
|
||||||
|
*/
|
||||||
public function store(Request $request)
|
public function store(Request $request)
|
||||||
{
|
{
|
||||||
$item = User::loggedIn()->items()->find(request('item'));
|
$item = User::loggedIn()->items()->find(request('item'));
|
||||||
@ -18,7 +28,7 @@ class AlertController extends Controller
|
|||||||
|
|
||||||
$loggedUser = Auth::user()->name;
|
$loggedUser = Auth::user()->name;
|
||||||
$userWithItem = User::find($item->used_by);
|
$userWithItem = User::find($item->used_by);
|
||||||
\Mail::to($userWithItem)
|
Mail::to($userWithItem)
|
||||||
->locale($userWithItem->language)
|
->locale($userWithItem->language)
|
||||||
->send(new UserWaiting($loggedUser, $userWithItem->name, $item));
|
->send(new UserWaiting($loggedUser, $userWithItem->name, $item));
|
||||||
|
|
||||||
|
@ -6,6 +6,8 @@ use \App\User;
|
|||||||
|
|
||||||
class HomeController extends Controller
|
class HomeController extends Controller
|
||||||
{
|
{
|
||||||
|
protected $activeUsers = [];
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Create a new controller instance.
|
* Create a new controller instance.
|
||||||
*
|
*
|
||||||
@ -16,6 +18,46 @@ class HomeController extends Controller
|
|||||||
$this->middleware('auth');
|
$this->middleware('auth');
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Show the application dashboard.
|
||||||
|
*
|
||||||
|
* @return \Illuminate\Http\Response
|
||||||
|
*/
|
||||||
|
public function index()
|
||||||
|
{
|
||||||
|
$items = User::loggedIn()->items()->with('users')->get();
|
||||||
|
|
||||||
|
foreach ($items as $item) {
|
||||||
|
$this->getUsername($item->users, $item->used_by);
|
||||||
|
$this->getUsername($item->users, $item->waiting_user_id);
|
||||||
|
}
|
||||||
|
|
||||||
|
$products = $items
|
||||||
|
->sortBy('product.name', SORT_NATURAL | SORT_FLAG_CASE)
|
||||||
|
->groupBy('product.name');
|
||||||
|
|
||||||
|
return view(
|
||||||
|
'home',
|
||||||
|
['products' => $products, 'users' => $this->activeUsers]
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Get the username from an specified user id.
|
||||||
|
*
|
||||||
|
* @param object $itemUsers Array with IDs and usernames
|
||||||
|
* @param int $id The user id to search for
|
||||||
|
*
|
||||||
|
* @return void
|
||||||
|
*/
|
||||||
|
public function getUsername(object $itemUsers, ?int $id)
|
||||||
|
{
|
||||||
|
if ($id && !isset($this->activeUsers[$id])) {
|
||||||
|
$this->activeUsers[$id] = $this->findName($itemUsers, $id);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Get a name from a specific id in a array
|
* Get a name from a specific id in a array
|
||||||
*
|
*
|
||||||
@ -32,41 +74,4 @@ class HomeController extends Controller
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* Check if the user_id alreay exists on $users array.
|
|
||||||
*
|
|
||||||
* @param array $users The array with users
|
|
||||||
* @param int $user_id The user_id to try to find on array
|
|
||||||
*
|
|
||||||
* @return boolean
|
|
||||||
*/
|
|
||||||
public function isNewUser($users, $user_id)
|
|
||||||
{
|
|
||||||
return ($user_id && !isset($users[$user_id]));
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Show the application dashboard.
|
|
||||||
*
|
|
||||||
* @return \Illuminate\Http\Response
|
|
||||||
*/
|
|
||||||
public function index()
|
|
||||||
{
|
|
||||||
$users = [];
|
|
||||||
$items = User::loggedIn()->items()->with('users')->get();
|
|
||||||
|
|
||||||
foreach ($items as $item) {
|
|
||||||
if ($this->isNewUser($users, $item->used_by)) {
|
|
||||||
$users[$item->used_by] = $this->findName($item->users, $item->used_by);
|
|
||||||
}
|
|
||||||
|
|
||||||
if ($this->isNewUser($users, $item->waiting_user_id)) {
|
|
||||||
$users[$item->waiting_user_id] = $this->findName($item->users, $item->waiting_user_id);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
$products = $items->sortBy('product.name', SORT_NATURAL | SORT_FLAG_CASE)->groupBy('product.name');
|
|
||||||
return view('home', compact('products', 'users'));
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
@ -2,28 +2,46 @@
|
|||||||
|
|
||||||
namespace App\Http\Controllers;
|
namespace App\Http\Controllers;
|
||||||
|
|
||||||
|
use Auth;
|
||||||
|
use Lang;
|
||||||
use \App\Item;
|
use \App\Item;
|
||||||
use \App\User;
|
use \App\User;
|
||||||
use Illuminate\Http\Request;
|
|
||||||
use App\Events\ReturnItem;
|
use App\Events\ReturnItem;
|
||||||
|
use Illuminate\Http\Request;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Responsible to Take and Return an Item.
|
||||||
|
*/
|
||||||
class TakeController extends Controller
|
class TakeController extends Controller
|
||||||
{
|
{
|
||||||
|
/**
|
||||||
|
* The user take an item
|
||||||
|
*
|
||||||
|
* @param Request $request The form data
|
||||||
|
*
|
||||||
|
* @return home view
|
||||||
|
*/
|
||||||
public function store(Request $request)
|
public function store(Request $request)
|
||||||
{
|
{
|
||||||
$item = User::loggedIn()->items()->find(request('item'));
|
$item = User::loggedIn()->items()->find(request('item'));
|
||||||
if ($item->used_by) {
|
if ($item->used_by) {
|
||||||
return back()->withErrors(
|
return back()->withErrors(
|
||||||
\Lang::getFromJson(
|
Lang::getFromJson("This item is already taken")
|
||||||
"This item is already taken"
|
|
||||||
)
|
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
$item->used_by = \Auth::id();
|
$item->used_by = Auth::id();
|
||||||
$item->save();
|
$item->save();
|
||||||
return redirect('home');
|
return redirect('home');
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* User return an item
|
||||||
|
* Trigger an event: ReturnItem
|
||||||
|
*
|
||||||
|
* @param Request $request Form data
|
||||||
|
*
|
||||||
|
* @return View home
|
||||||
|
*/
|
||||||
public function delete(Request $request)
|
public function delete(Request $request)
|
||||||
{
|
{
|
||||||
$item = User::loggedIn()->items()->find(request('item'));
|
$item = User::loggedIn()->items()->find(request('item'));
|
||||||
|
@ -2,6 +2,7 @@
|
|||||||
|
|
||||||
namespace App\Http\Controllers;
|
namespace App\Http\Controllers;
|
||||||
|
|
||||||
|
use \Lang;
|
||||||
use \App\User;
|
use \App\User;
|
||||||
use \App\Item;
|
use \App\Item;
|
||||||
use \App\Product;
|
use \App\Product;
|
||||||
@ -43,7 +44,7 @@ class UserController extends Controller
|
|||||||
|
|
||||||
if (count($userArray) == 0) {
|
if (count($userArray) == 0) {
|
||||||
return back()->withErrors(
|
return back()->withErrors(
|
||||||
\Lang::getFromJson("The e-mail address is not registered yet.")
|
Lang::getFromJson("The e-mail address is not registered yet.")
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -54,7 +55,7 @@ class UserController extends Controller
|
|||||||
->syncWithoutDetaching([request('item_id')]);
|
->syncWithoutDetaching([request('item_id')]);
|
||||||
} else {
|
} else {
|
||||||
return back()->withErrors(
|
return back()->withErrors(
|
||||||
\Lang::getFromJson(
|
Lang::getFromJson(
|
||||||
"You cannot add a user to a product that is not yourse."
|
"You cannot add a user to a product that is not yourse."
|
||||||
)
|
)
|
||||||
);
|
);
|
||||||
@ -85,7 +86,7 @@ class UserController extends Controller
|
|||||||
->detach([request('item_id')]);
|
->detach([request('item_id')]);
|
||||||
} else {
|
} else {
|
||||||
return back()->withErrors(
|
return back()->withErrors(
|
||||||
\Lang::getFromJson(
|
Lang::getFromJson(
|
||||||
"You cannot remove a user from a product that is not yourse."
|
"You cannot remove a user from a product that is not yourse."
|
||||||
)
|
)
|
||||||
);
|
);
|
||||||
|
@ -25,7 +25,8 @@ class AlertReturnedItem
|
|||||||
* Send an email to the user that
|
* Send an email to the user that
|
||||||
* is waiting for the item
|
* is waiting for the item
|
||||||
*
|
*
|
||||||
* @param ReturnItem $item
|
* @param ReturnItem $event The return event that contains an item
|
||||||
|
*
|
||||||
* @return void
|
* @return void
|
||||||
*/
|
*/
|
||||||
public function handle(ReturnItem $event)
|
public function handle(ReturnItem $event)
|
||||||
|
Loading…
Reference in New Issue
Block a user