Including occupied username and renaming DB field

Now it shows the username of who is using an item.
Item db field 'usedBy' was renamed to 'used_by' to keep consistence.
This commit is contained in:
Bruno F. Fontes 2018-09-14 12:52:07 -03:00
parent 925c378d1e
commit ea3ffaad39
11 changed files with 59 additions and 36 deletions

View File

@ -2,6 +2,7 @@
namespace App\Http\Controllers;
use \App\User;
use Illuminate\Http\Request;
class HomeController extends Controller
@ -23,7 +24,17 @@ class HomeController extends Controller
*/
public function index()
{
$items = \App\User::find(\Auth::id())->items()->get();
return view('home', compact('items'));
$items = User::find(\Auth::id())->items()->get();
foreach ($items as $item) {
if ($item->used_by) {
$users[$item->used_by] = User::find($item->used_by)->name;
if ($item->waiting_user_id) {
$users[$item->waiting_user_id] = User::find($item->waiting_user_id)->name;
}
}
}
return view('home', compact('items', 'users'));
}
}

View File

@ -10,7 +10,7 @@ class ReturnController extends Controller
public function store(Request $request)
{
$item = User::find(\Auth::id())->items()->find(request('item'));
$item->usedBy = null;
$item->used_by = null;
$item->save();
return redirect('home');

View File

@ -11,7 +11,7 @@ class TakeController extends Controller
public function store(Request $request)
{
$item = User::find(\Auth::id())->items()->find(request('item'));
$item->usedBy = \Auth::id();
$item->used_by = \Auth::id();
$item->save();
return redirect('home');
}

View File

@ -68,7 +68,7 @@ class UserController extends Controller
if ($item->product->user_id == \Auth::id()) {
$returnItem = User::findOrFail(request('user_id'))->items()->findOrFail(request('item_id'));
$returnItem->usedBy = null;
$returnItem->used_by = null;
$returnItem->save();
User::findOrFail(request('user_id'))->items()->detach([request('item_id')]);
} else {

View File

@ -23,7 +23,7 @@ class Item extends Model
return $query->where([
['user_id', $user_id],
['product_id', $product_id],
['usedBy', ''],
['used_by', ''],
]);
}

View File

@ -17,8 +17,8 @@ class CreateItemsTable extends Migration
$table->increments('id');
$table->string('name');
$table->integer('product_id');
$table->integer('usedBy')->nullable();
$table->dateTime('usedSince')->nullable();
$table->integer('used_by')->nullable();
$table->integer('waiting_user_id')->nullable();
$table->timestamps();
});
}

View File

@ -7,7 +7,7 @@ Cada usuário, identificado por e-mail, pode ter outros amigos
[x] usuário(nome, email)
[x] product[site/software](nome, admin)
[ ] usersPerItem(productID, userID)
[x] item[licença](nome, productID, usedBy, usedSince)
[x] item[licença](nome, productID, used_by, usedSince)
[ ] waiting(userID, itemID, waitingSince)
## VIEWS

View File

@ -5,7 +5,7 @@
<div class="row justify-content-center">
<div class="col-md-8">
<div class="card">
<div class="card-header">Dashboard</div>
<div class="card-header"><h3>Your items</h3></div>
<div class="card-body">
@if (session('status'))
@ -14,37 +14,21 @@
</div>
@endif
<h4>Your itens</h4>
@forelse ($items as $item)
<hr class="ml-5 mr-5">
<li class="mt-1 mb-1">
@if (!$loop->first)
<hr class="m-3">
@endif
<div class="my-4 p-2">
<strong>{{$item->name}}</strong> ({{$item->product->name}})
@if ($item->usedBy)
@if ($item->usedBy == \Auth::id())
<span class="float-right">
<form action="/return" method="POST">
{{ csrf_field() }}
<input type="hidden" name="item" value="{{$item->id}}">
<button type="submit" class="btn-xm btn-danger">Return It</button>
</form>
</span>
<span class="float-right mr-3"><em>{{\Carbon\Carbon::parse($item->updated_at)->diffForHumans()}}</em></span>
@if ($item->used_by)
@include('home.usedItem')
@else
<div class="alert alert-danger" role="alert">In use by {{$item->usedBy}}, since {{$item->updated_at->diffForHumans()}}</div>
@include('home.unusedItem')
@endif
@else
<span class="float-right">
<form action="/take" method="POST">
{{ csrf_field() }}
<input type="hidden" name="item" value="{{$item->id}}">
<button type="submit" class="btn-xm btn-success">Take It</button>
</form>
</span>
@endif
</li>
</div>
@empty
<p>There are no items for you yet. Include a product or an item <a href="/product">here.</a></p>
<p>There are no items shared with you yet. <a href="/product">Share an item!</a></p>
@endforelse
</div>
</div>

View File

@ -0,0 +1,7 @@
<span class="float-right">
<form action="/take" method="POST">
{{ csrf_field() }}
<input type="hidden" name="item" value="{{$item->id}}">
<button type="submit" class="btn btn-sm btn-success">Take It</button>
</form>
</span>

View File

@ -0,0 +1,21 @@
@if ($item->used_by == \Auth::id())
<span class="float-right">
<form action="/return" method="POST">
{{ csrf_field() }}
<input type="hidden" name="item" value="{{$item->id}}">
<button type="submit" class="btn btn-sm btn-danger">Return It</button>
</form>
</span>
<span class="float-right mr-3"><em>{{\Carbon\Carbon::parse($item->updated_at)->diffForHumans()}}</em></span>
@else
<span class="float-right">
<form action="/alert" method="POST">
{{ csrf_field() }}
<input type="hidden" name="item" value="{{$item->id}}">
<button type="submit" class="btn btn-sm btn-outline-secondary">Alert me</button>
</form>
</span>
<span class="float-right mr-3">
<em>{{str_limit($users[$item->used_by], 15, '...')}} ({{$item->updated_at->diffForHumans()}})</em>
</span>
@endif

View File

@ -19,7 +19,7 @@
<strong>Other items from the same product:</strong>
<ul>
@forelse ($otherItems as $otherItem)
@if (!$otherItem->usedBy)
@if (!$otherItem->used_by)
<li><a href="/item/{{ $otherItem->id }}">{{ $otherItem->name }}</a></li>
@endif
@empty