From 8c4fe0a489460603892fd7d3c452ab6741969481 Mon Sep 17 00:00:00 2001 From: Bruno Fontes Date: Wed, 10 Oct 2018 00:46:28 -0300 Subject: [PATCH] Refactoring Including some use for classes; REfactoring the HomeController, to make it cleaner and avoid repeating code. --- app/Events/ReturnItem.php | 2 + app/Http/Controllers/AlertController.php | 12 +++- app/Http/Controllers/HomeController.php | 79 +++++++++++++----------- app/Http/Controllers/TakeController.php | 28 +++++++-- app/Http/Controllers/UserController.php | 7 ++- app/Listeners/AlertReturnedItem.php | 3 +- 6 files changed, 84 insertions(+), 47 deletions(-) diff --git a/app/Events/ReturnItem.php b/app/Events/ReturnItem.php index e580c7e..6a76d6d 100644 --- a/app/Events/ReturnItem.php +++ b/app/Events/ReturnItem.php @@ -19,6 +19,8 @@ class ReturnItem /** * Create a new event instance. * + * @param Item $item The returned item. + * * @return void */ public function __construct(Item $item) diff --git a/app/Http/Controllers/AlertController.php b/app/Http/Controllers/AlertController.php index e46aea8..43cbd27 100644 --- a/app/Http/Controllers/AlertController.php +++ b/app/Http/Controllers/AlertController.php @@ -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)); diff --git a/app/Http/Controllers/HomeController.php b/app/Http/Controllers/HomeController.php index fc226fb..34a3737 100644 --- a/app/Http/Controllers/HomeController.php +++ b/app/Http/Controllers/HomeController.php @@ -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')); - } } diff --git a/app/Http/Controllers/TakeController.php b/app/Http/Controllers/TakeController.php index 2822684..0f700df 100644 --- a/app/Http/Controllers/TakeController.php +++ b/app/Http/Controllers/TakeController.php @@ -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')); diff --git a/app/Http/Controllers/UserController.php b/app/Http/Controllers/UserController.php index cfaf577..9e8be30 100644 --- a/app/Http/Controllers/UserController.php +++ b/app/Http/Controllers/UserController.php @@ -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." ) ); diff --git a/app/Listeners/AlertReturnedItem.php b/app/Listeners/AlertReturnedItem.php index 1db13fe..7dc7855 100644 --- a/app/Listeners/AlertReturnedItem.php +++ b/app/Listeners/AlertReturnedItem.php @@ -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)