Refactoring

Including some use for classes;
REfactoring the HomeController, to make it cleaner
and avoid repeating code.
This commit is contained in:
Bruno F. Fontes 2018-10-10 00:46:28 -03:00
parent 69f0722c79
commit 8c4fe0a489
Signed by: brunofontes
GPG Key ID: EE3447CE80048495
6 changed files with 84 additions and 47 deletions

View File

@ -19,6 +19,8 @@ class ReturnItem
/**
* Create a new event instance.
*
* @param Item $item The returned item.
*
* @return void
*/
public function __construct(Item $item)

View File

@ -3,12 +3,22 @@
namespace App\Http\Controllers;
use Auth;
use Mail;
use \App\User;
use \App\Mail\UserWaiting;
use Illuminate\Http\Request;
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)
{
$item = User::loggedIn()->items()->find(request('item'));
@ -18,7 +28,7 @@ class AlertController extends Controller
$loggedUser = Auth::user()->name;
$userWithItem = User::find($item->used_by);
\Mail::to($userWithItem)
Mail::to($userWithItem)
->locale($userWithItem->language)
->send(new UserWaiting($loggedUser, $userWithItem->name, $item));

View File

@ -6,6 +6,8 @@ use \App\User;
class HomeController extends Controller
{
protected $activeUsers = [];
/**
* Create a new controller instance.
*
@ -16,6 +18,46 @@ class HomeController extends Controller
$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
*
@ -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'));
}
}

View File

@ -2,28 +2,46 @@
namespace App\Http\Controllers;
use Auth;
use Lang;
use \App\Item;
use \App\User;
use Illuminate\Http\Request;
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"
)
Lang::getFromJson("This item is already taken")
);
}
$item->used_by = \Auth::id();
$item->used_by = Auth::id();
$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'));

View File

@ -2,6 +2,7 @@
namespace App\Http\Controllers;
use \Lang;
use \App\User;
use \App\Item;
use \App\Product;
@ -43,7 +44,7 @@ class UserController extends Controller
if (count($userArray) == 0) {
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')]);
} else {
return back()->withErrors(
\Lang::getFromJson(
Lang::getFromJson(
"You cannot add a user to a product that is not yourse."
)
);
@ -85,7 +86,7 @@ class UserController extends Controller
->detach([request('item_id')]);
} else {
return back()->withErrors(
\Lang::getFromJson(
Lang::getFromJson(
"You cannot remove a user from a product that is not yourse."
)
);

View File

@ -25,7 +25,8 @@ class AlertReturnedItem
* Send an email to the user that
* is waiting for the item
*
* @param ReturnItem $item
* @param ReturnItem $event The return event that contains an item
*
* @return void
*/
public function handle(ReturnItem $event)