From e3669914fd3c41e16d23e7f549802ca3fdc5fe2b Mon Sep 17 00:00:00 2001 From: Bruno Fontes Date: Thu, 13 Sep 2018 21:16:16 -0300 Subject: [PATCH] Including the option to add users to items --- app/Http/Controllers/ItemController.php | 7 +- app/Http/Controllers/UserController.php | 102 ++++++++++++++++++++++++ resources/views/item.blade.php | 42 ++++++++++ routes/web.php | 3 + 4 files changed, 151 insertions(+), 3 deletions(-) create mode 100644 app/Http/Controllers/UserController.php diff --git a/app/Http/Controllers/ItemController.php b/app/Http/Controllers/ItemController.php index 1094b15..fa304e0 100644 --- a/app/Http/Controllers/ItemController.php +++ b/app/Http/Controllers/ItemController.php @@ -11,9 +11,10 @@ class ItemController extends Controller { $item = Item::find($id); if (!$item || $item->product->user_id != \Auth::id()) return back(); + $users = $item->users()->get(); $otherItems = Item::where([['product_id', $item->product_id], ['id', '!=', $id]])->get(); - return view('item', compact('item', 'otherItems')); + return view('item', compact('item', 'otherItems', 'users')); } public function index() @@ -39,8 +40,8 @@ class ItemController extends Controller ] ); - $user = \App\User::find(\Auth::id()); - $user->items()->create(['name' => request('item'), 'product_id' => request('product_id')]); + $authUser = \App\User::find(\Auth::id()); + $authUser->items()->create(['name' => request('item'), 'product_id' => request('product_id')]); return redirect('product/'.request('product_id')); } diff --git a/app/Http/Controllers/UserController.php b/app/Http/Controllers/UserController.php new file mode 100644 index 0000000..96f7984 --- /dev/null +++ b/app/Http/Controllers/UserController.php @@ -0,0 +1,102 @@ +validate($request, ['email' => 'required', 'item_id' => 'required']); + + $user = User::where('email', request('email'))->get(); + + if (count($user) == 0) { + return back()->withErrors("The e-mail address is not registered yet."); + } + + $item = Item::findOrFail(request('item_id')); + if ($item->product->user_id == \Auth::id()) { + User::findOrFail($user[0]->id)->items()->attach(request('item_id')); + //$user->items()->attach(request('item_id')); + } else { + return back()->withErrors("You cannot add a user to a product that is not yourse."); + } + return back(); + } + + /** + * Display the specified resource. + * + * @param int $id + * @return \Illuminate\Http\Response + */ + public function show($id) + { + // + } + + /** + * Show the form for editing the specified resource. + * + * @param int $id + * @return \Illuminate\Http\Response + */ + public function edit($id) + { + // + } + + /** + * Update the specified resource in storage. + * + * @param \Illuminate\Http\Request $request + * @param int $id + * @return \Illuminate\Http\Response + */ + public function update(Request $request, $id) + { + // + } + + /** + * Remove the specified resource from storage. + * + * @param int $id + * @return \Illuminate\Http\Response + */ + public function destroy($id) + { + // + } +} diff --git a/resources/views/item.blade.php b/resources/views/item.blade.php index 95a4d4b..1f5a95c 100644 --- a/resources/views/item.blade.php +++ b/resources/views/item.blade.php @@ -28,6 +28,48 @@ +
+
+ Users of this item +
+ +
+ Users that has access to this item: +
    + @forelse ($users as $user) +
    +
  • + {{ $user->name }} ({{ $user->email}}) +
    +
    + {{ csrf_field() }} + @method('DELETE') + + + +
    +
  • + @empty +

    There are no items yet. Include one with the form above.

    + @endforelse +
+ +
+ Add user +
+
+ {{ csrf_field() }} +
+
+ +
+
+
+
+ @include ('layouts.errors') + +
+
BACK
diff --git a/routes/web.php b/routes/web.php index d4289eb..e780b69 100644 --- a/routes/web.php +++ b/routes/web.php @@ -27,6 +27,9 @@ Route::delete('/item', 'ItemController@delete')->middleware('auth'); Route::post('/take', 'TakeController@store')->middleware('auth'); Route::post('/return', 'ReturnController@store')->middleware('auth'); +Route::post('/user', 'UserController@store')->middleware('auth'); +Route::delete('/user', 'UserController@delete')->middleware('auth'); + Auth::routes(); Route::get('/home', 'HomeController@index')->name('home')->middleware('auth');