From 0f15e60c95178765cb91b7b0b5aaabd693af32fa Mon Sep 17 00:00:00 2001 From: Bruno Fontes Date: Tue, 11 Sep 2018 20:48:09 -0300 Subject: [PATCH] Imrpoving Item, Product and Users --- app/Http/Controllers/HomeController.php | 6 ++- app/Http/Controllers/ItemController.php | 26 +++++++--- app/Http/Controllers/ProductController.php | 20 ++------ app/Item.php | 14 +++-- app/Product.php | 21 +++++++- app/User.php | 5 ++ ...018_09_08_192040_create_products_table.php | 2 +- .../2018_09_08_192152_create_items_table.php | 6 +-- ...em.php => 2018_09_10_233609_item_user.php} | 10 ++-- resources/views/home.blade.php | 13 ++++- resources/views/item.blade.php | 51 ++++++++++++++++--- resources/views/item/buttons.blade.php | 22 ++++++++ resources/views/product.blade.php | 6 --- resources/views/product/show.blade.php | 5 +- resources/views/welcome.blade.php | 1 - routes/web.php | 4 ++ start_vagrant.sh | 2 +- 17 files changed, 155 insertions(+), 59 deletions(-) rename database/migrations/{2018_09_10_233609_users_per_item.php => 2018_09_10_233609_item_user.php} (65%) create mode 100644 resources/views/item/buttons.blade.php delete mode 100644 resources/views/product.blade.php 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 @@

Your itens

@forelse ($items as $item) -
  • {{$item.name}}
  • +
  • + {{$item->name}} + @if ($item->usedSince) +
    + In use by {{$item->usedBy}}, since {{$item->usedSince->diffForHumans()}} +
    + @else + TAKE IT + @endif +
  • @empty -

    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 @@ - -

    Item!

    -
    - @if (isset($item)) - Item: {{$item}} - @endif - \ No newline at end of file +@extends('layouts.app') + +@section('content') +
    +
    +
    + +
    +
    + {{ csrf_field() }} +
    +
    +
    +
    + @include ('layouts.errors') +
    + +
    +
    + Item: {{$item[0]->name}} + @include ('item.buttons') +
    + +
    + @if (session('status')) + + @endif + +
      + @forelse ($otherItems as $otherItem) + @if (!$otherItem->usedBy) +
    • {{ $otherItem->name }}
    • + @endif + @empty +

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

      + @endforelse +
    +
    +
    +
    +
    +
    +@endsection diff --git a/resources/views/item/buttons.blade.php b/resources/views/item/buttons.blade.php new file mode 100644 index 0000000..2b9a753 --- /dev/null +++ b/resources/views/item/buttons.blade.php @@ -0,0 +1,22 @@ + +
    + + + + @if ($item[0]->admin_id == \Auth::id()) +
    + @method('PATCH') + +
    + @endif + + + +
    + @method('DELETE') + +
    + + +
    +
    \ No newline at end of file diff --git a/resources/views/product.blade.php b/resources/views/product.blade.php deleted file mode 100644 index 82ece44..0000000 --- a/resources/views/product.blade.php +++ /dev/null @@ -1,6 +0,0 @@ - -

    Product!

    -@if (isset($productID)) - {{$productID}} -@endif - \ No newline at end of file diff --git a/resources/views/product/show.blade.php b/resources/views/product/show.blade.php index 1a0c510..b1d43df 100644 --- a/resources/views/product/show.blade.php +++ b/resources/views/product/show.blade.php @@ -10,6 +10,7 @@ {{ csrf_field() }}
    +
    @include ('layouts.errors') @@ -28,8 +29,8 @@ @endif