mirror of
https://github.com/brunofontes/shareit.git
synced 2024-11-24 04:14:57 +00:00
Imrpoving Item, Product and Users
This commit is contained in:
parent
4b3c2089f5
commit
0f15e60c95
@ -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'));
|
||||
}
|
||||
}
|
||||
|
@ -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'));
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -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'));
|
||||
}
|
||||
}
|
||||
|
14
app/Item.php
14
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', ''],
|
||||
]);
|
||||
}
|
||||
|
@ -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'));
|
||||
}
|
||||
*/
|
||||
}
|
||||
|
@ -27,4 +27,9 @@ class User extends Authenticatable
|
||||
protected $hidden = [
|
||||
'password', 'remember_token',
|
||||
];
|
||||
|
||||
public function products()
|
||||
{
|
||||
return $this->hasMany(Product::class);
|
||||
}
|
||||
}
|
||||
|
@ -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();
|
||||
});
|
||||
}
|
||||
|
@ -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();
|
||||
});
|
||||
}
|
||||
|
@ -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');
|
||||
}
|
||||
}
|
@ -16,9 +16,18 @@
|
||||
|
||||
<h4>Your itens</h4>
|
||||
@forelse ($items as $item)
|
||||
<li>{{$item.name}}</li>
|
||||
<li>
|
||||
{{$item->name}}
|
||||
@if ($item->usedSince)
|
||||
<div class="alert">
|
||||
In use by {{$item->usedBy}}, since {{$item->usedSince->diffForHumans()}}
|
||||
</div>
|
||||
@else
|
||||
<a href="">TAKE IT</a>
|
||||
@endif
|
||||
</li>
|
||||
@empty
|
||||
<p>There are no items for you yet. Include one <a href="/product">here.</a></p>
|
||||
<p>There are no items for you yet. Include a product or an item <a href="/product">here.</a></p>
|
||||
@endforelse
|
||||
</div>
|
||||
</div>
|
||||
|
@ -1,7 +1,44 @@
|
||||
<html>
|
||||
<h1>Item!</h1>
|
||||
<div>
|
||||
@if (isset($item))
|
||||
Item: {{$item}}
|
||||
@extends('layouts.app')
|
||||
|
||||
@section('content')
|
||||
<div class="container">
|
||||
<div class="row justify-content-center">
|
||||
<div class="col-md-8">
|
||||
|
||||
<form method="POST" action="/item" class="form-inline">
|
||||
<div class="form-group">
|
||||
{{ csrf_field() }}
|
||||
<div class="col"><label for="product">Add Item: </label></div>
|
||||
<div class="col-6"><input type="text" class="form-control" name="item" id="item" placeholder="One Hundred Years of Solitude" required></div>
|
||||
<div class="col"><button type="submit" class="btn btn-primary">Insert</button></div>
|
||||
</div>
|
||||
@include ('layouts.errors')
|
||||
</form>
|
||||
|
||||
<div class="card mt-4">
|
||||
<div class="card-header">
|
||||
Item: <strong>{{$item[0]->name}}</strong>
|
||||
@include ('item.buttons')
|
||||
</div>
|
||||
|
||||
<div class="card-body">
|
||||
@if (session('status'))
|
||||
<div class="alert alert-success" role="alert"> {{ session('status') }}
|
||||
</div>
|
||||
@endif
|
||||
</html>
|
||||
|
||||
<ul>
|
||||
@forelse ($otherItems as $otherItem)
|
||||
@if (!$otherItem->usedBy)
|
||||
<li><a href="/item/{{ $otherItem->id }}">{{ $otherItem->name }}</a></li>
|
||||
@endif
|
||||
@empty
|
||||
<p>There are no items yet. Include one with the form above.</p>
|
||||
@endforelse
|
||||
</ul>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
@endsection
|
||||
|
22
resources/views/item/buttons.blade.php
Normal file
22
resources/views/item/buttons.blade.php
Normal file
@ -0,0 +1,22 @@
|
||||
<span class="d-inline-block text-truncat float-right">
|
||||
<div class="btn-group btn-group-sm" role="group">
|
||||
|
||||
|
||||
<!-- EDIT BUTTON -->
|
||||
@if ($item[0]->admin_id == \Auth::id())
|
||||
<form action="/item/{{$item[0]->id}}" method="POST">
|
||||
@method('PATCH')
|
||||
<button type="button" class="btn-sm btn-secondary mr-1">Edit</button>
|
||||
</form>
|
||||
@endif
|
||||
|
||||
|
||||
<!-- DELETE BUTTON -->
|
||||
<form action="/item/{{$item[0]->id}}" method="POST">
|
||||
@method('DELETE')
|
||||
<button type="button" class="btn-sm btn-danger">Delete</button>
|
||||
</form>
|
||||
|
||||
|
||||
</div>
|
||||
</span>
|
@ -1,6 +0,0 @@
|
||||
<html>
|
||||
<h1>Product!</h1>
|
||||
@if (isset($productID))
|
||||
{{$productID}}
|
||||
@endif
|
||||
</html>
|
@ -10,6 +10,7 @@
|
||||
{{ csrf_field() }}
|
||||
<div class="col"><label for="product">Add Item: </label></div>
|
||||
<div class="col-6"><input type="text" class="form-control" name="item" id="item" placeholder="One Hundred Years of Solitude" required></div>
|
||||
<input type="hidden" name="product_id" id="product_id" value="{{ $product['id'] }}" required>
|
||||
<div class="col"><button type="submit" class="btn btn-primary">Insert</button></div>
|
||||
</div>
|
||||
@include ('layouts.errors')
|
||||
@ -28,8 +29,8 @@
|
||||
@endif
|
||||
|
||||
<ul>
|
||||
@forelse ($items as $item)
|
||||
<li><a href="/product/{{$item['id']}}">{{$item['name']}}</a></li>
|
||||
@forelse ($product->items as $item)
|
||||
<li><a href="/item/{{ $item->id }}">{{ $item->name }}</a></li>
|
||||
@empty
|
||||
<p>There are no items yet. Include one with the form above.</p>
|
||||
@endforelse
|
||||
|
@ -96,7 +96,6 @@
|
||||
@auth
|
||||
<a href="/home">Home</a>
|
||||
<a href="/product">Products</a>
|
||||
<a href="/item">Items</a>
|
||||
@else
|
||||
<a href="/register">Register</a>
|
||||
<a href="/login">Login</a>
|
||||
|
@ -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');
|
||||
|
@ -1,3 +1,3 @@
|
||||
source ~/.zshrc
|
||||
#source ~/.zshrc
|
||||
vagrant up
|
||||
ssh homestead -t "cd code/Bruno\ Fontes/shareit; bash --login"
|
||||
|
Loading…
Reference in New Issue
Block a user