diff --git a/app/Http/Controllers/HomeController.php b/app/Http/Controllers/HomeController.php index e8c7929..2d91ddb 100644 --- a/app/Http/Controllers/HomeController.php +++ b/app/Http/Controllers/HomeController.php @@ -23,7 +23,11 @@ class HomeController extends Controller */ public function index() { - $items = \App\Item::where('userID'); + $items = \DB::table('items') + ->join('item_user', 'item_user.item_id', '=', 'items.id') + ->where('item_user.user_id', \Auth::id()) + ->select('items.*') + ->get(); return view('home', compact('items')); } } diff --git a/app/Http/Controllers/ItemController.php b/app/Http/Controllers/ItemController.php index c74f90c..a4f5700 100644 --- a/app/Http/Controllers/ItemController.php +++ b/app/Http/Controllers/ItemController.php @@ -9,15 +9,19 @@ class ItemController extends Controller { public function show($id) { - //TODO: Fazer innerjoint com tabela de users por item - $item = Item::where([['id', $id], ['userID', \Auth::id()]]); - return view('item', compact('item')); + $item = \DB::table('items') + ->join('products', 'items.product_id', '=', 'products.id') + ->where([['products.admin_id', \Auth::id()], ['items.id', $id]]) + ->select('items.*', 'products.admin_id') + ->get(); + $otherItems = Item::where([['product_id', $item[0]->product_id], ['id', '!=', $id]])->get(); + return view('item', compact('item', 'otherItems')); } public function index() { //TODO: Fazer innerjoint com tabela de users por item - $items = Item::where('adminID', \Auth::id())->get(); + $items = Item::where('admin_id', \Auth::id())->get(); return view('item.index', compact('items')); } @@ -28,12 +32,18 @@ class ItemController extends Controller * * @return (view) The product view */ - public function store() + public function store(Request $request) { - $this->validate(request(), ['name' => 'required']); - Item::create(['name' => request('item'), 'productID' => request('productID')]); //Just remember to add the fillable on Model to make this work + $request->validate( + [ + 'item' => 'required', + 'product_id' => 'required' + ] + ); + $id = Item::insertGetId(['name' => request('item'), 'product_id' => request('product_id')]); //Just remember to add the fillable on Model to make this work + \DB::table('item_user')->insert([ 'user_id' => \Auth::id(), 'item_id' => $id]); - return redirect('product'); + return redirect('product/'.request('product_id')); } } diff --git a/app/Http/Controllers/ProductController.php b/app/Http/Controllers/ProductController.php index eb756fc..893df8f 100644 --- a/app/Http/Controllers/ProductController.php +++ b/app/Http/Controllers/ProductController.php @@ -10,7 +10,7 @@ class ProductController extends Controller { public function index() { - $products = Product::where('adminID', \Auth::id())->get(); + $products = Product::where('admin_id', \Auth::id())->get(); return view('product.index', compact('products')); } @@ -21,14 +21,7 @@ class ProductController extends Controller */ public function store() { - /* - $product = new Product; - $product->name = request('product'); - $product->adminID = $userID; - $product->save(); - */ - Product::create(['name' => request('product'), 'adminID' => \Auth::id()]); //Just remember to add the fillable on Model to make this work - + Product::create(['name' => request('product'), 'admin_id' => \Auth::id()]); //Just remember to add the fillable on Model to make this work return redirect('product'); } @@ -48,12 +41,7 @@ class ProductController extends Controller */ public function show($id) { - $product = Product::find($id); - $items = \DB::table('items') - ->join('products', 'items.productID', '=', 'products.id') - ->where('products.adminID', \Auth::id()) - ->select('items.*', 'products.*') - ->get(); - return view('product.show', compact('items', 'product')); + $product = Product::where('admin_id', \Auth::id())->find($id); + return view('product.show', compact('product')); } } diff --git a/app/Item.php b/app/Item.php index f2780bd..9c3856d 100644 --- a/app/Item.php +++ b/app/Item.php @@ -6,12 +6,18 @@ use Illuminate\Database\Eloquent\Model; class Item extends Model { - // - public function free($productID, $userID) + protected $fillable = ['product_id', 'name']; + + public function product() + { + return $this->belongsTo(Product::class); + } + + public function free($product_id, $user_id) { return $query->where([ - ['userID', $userID], - ['productID', $productID], + ['user_id', $user_id], + ['product_id', $product_id], ['usedBy', ''], ]); } diff --git a/app/Product.php b/app/Product.php index afa05e9..bbf440e 100644 --- a/app/Product.php +++ b/app/Product.php @@ -6,6 +6,23 @@ use Illuminate\Database\Eloquent\Model; class Product extends Model { - protected $fillable = ['adminID', 'name']; - // + protected $fillable = ['admin_id', 'name']; + + public function items() + { + return $this->hasMany(Item::class); + } + + public function user() + { + return $this->belongsTo(User::class); + } + + //Ótima forma de adicionar um item por dentro do produto. Mas não estou usando por não saber como lidar com a table de UsuáriosPorItem + /* + public function addItem($name) + { + $this->items()->create(compact('name')); + } + */ } diff --git a/app/User.php b/app/User.php index fbc0e58..b51116e 100644 --- a/app/User.php +++ b/app/User.php @@ -27,4 +27,9 @@ class User extends Authenticatable protected $hidden = [ 'password', 'remember_token', ]; + + public function products() + { + return $this->hasMany(Product::class); + } } diff --git a/database/migrations/2018_09_08_192040_create_products_table.php b/database/migrations/2018_09_08_192040_create_products_table.php index 6479472..56d8e79 100644 --- a/database/migrations/2018_09_08_192040_create_products_table.php +++ b/database/migrations/2018_09_08_192040_create_products_table.php @@ -16,7 +16,7 @@ class CreateProductsTable extends Migration Schema::create('products', function (Blueprint $table) { $table->increments('id'); $table->string('name'); - $table->integer('adminID'); + $table->integer('admin_id'); $table->timestamps(); }); } diff --git a/database/migrations/2018_09_08_192152_create_items_table.php b/database/migrations/2018_09_08_192152_create_items_table.php index 4b53636..4cb3e6c 100644 --- a/database/migrations/2018_09_08_192152_create_items_table.php +++ b/database/migrations/2018_09_08_192152_create_items_table.php @@ -16,9 +16,9 @@ class CreateItemsTable extends Migration Schema::create('items', function (Blueprint $table) { $table->increments('id'); $table->string('name'); - $table->integer('productID'); - $table->integer('usedBy'); - $table->dateTime('usedSince'); + $table->integer('product_id'); + $table->integer('usedBy')->nullable(); + $table->dateTime('usedSince')->nullable(); $table->timestamps(); }); } diff --git a/database/migrations/2018_09_10_233609_users_per_item.php b/database/migrations/2018_09_10_233609_item_user.php similarity index 65% rename from database/migrations/2018_09_10_233609_users_per_item.php rename to database/migrations/2018_09_10_233609_item_user.php index 85e81a3..3308188 100644 --- a/database/migrations/2018_09_10_233609_users_per_item.php +++ b/database/migrations/2018_09_10_233609_item_user.php @@ -4,7 +4,7 @@ use Illuminate\Support\Facades\Schema; use Illuminate\Database\Schema\Blueprint; use Illuminate\Database\Migrations\Migration; -class UsersPerItem extends Migration +class ItemUser extends Migration { /** * Run the migrations. @@ -13,10 +13,10 @@ class UsersPerItem extends Migration */ public function up() { - Schema::create('users_per_item', function (Blueprint $table) { + Schema::create('item_user', function (Blueprint $table) { $table->increments('id'); - $table->integer('productID'); - $table->integer('userID'); + $table->integer('item_id'); + $table->integer('user_id'); $table->timestamps(); }); } @@ -28,6 +28,6 @@ class UsersPerItem extends Migration */ public function down() { - Schema::dropIfExists('users_per_item'); + Schema::dropIfExists('item_user'); } } diff --git a/resources/views/home.blade.php b/resources/views/home.blade.php index e05d426..99b1262 100644 --- a/resources/views/home.blade.php +++ b/resources/views/home.blade.php @@ -16,9 +16,18 @@
There are no items for you yet. Include one here.
+There are no items for you yet. Include a product or an item here.
@endforelse diff --git a/resources/views/item.blade.php b/resources/views/item.blade.php index dbf1c00..edc70fc 100644 --- a/resources/views/item.blade.php +++ b/resources/views/item.blade.php @@ -1,7 +1,44 @@ - -There are no items yet. Include one with the form above.
+ @endforelse +There are no items yet. Include one with the form above.
@endforelse diff --git a/resources/views/welcome.blade.php b/resources/views/welcome.blade.php index cb38763..5cf1423 100644 --- a/resources/views/welcome.blade.php +++ b/resources/views/welcome.blade.php @@ -96,7 +96,6 @@ @auth Home Products - Items @else Register Login diff --git a/routes/web.php b/routes/web.php index 4cac891..daa1840 100644 --- a/routes/web.php +++ b/routes/web.php @@ -18,8 +18,12 @@ Route::get('/', function () { Route::get('/product', 'ProductController@index')->middleware('auth'); Route::get('/product/{product}', 'ProductController@show')->middleware('auth'); Route::post('/product', 'ProductController@store')->middleware('auth'); + Route::get('/item', 'ItemController@index')->middleware('auth'); Route::get('/item/{item}', 'ItemController@show')->middleware('auth'); +Route::post('/item', 'ItemController@store')->middleware('auth'); +Route::delete('/item/{item}', 'ItemController@delete')->middleware('auth'); + Auth::routes(); Route::get('/home', 'HomeController@index')->name('home')->middleware('auth'); diff --git a/start_vagrant.sh b/start_vagrant.sh index 14a528f..3204bb2 100644 --- a/start_vagrant.sh +++ b/start_vagrant.sh @@ -1,3 +1,3 @@ -source ~/.zshrc +#source ~/.zshrc vagrant up ssh homestead -t "cd code/Bruno\ Fontes/shareit; bash --login"