From ea3ffaad393e9ef102cf7b9fc01e9394be6fa13c Mon Sep 17 00:00:00 2001 From: Bruno Fontes Date: Fri, 14 Sep 2018 12:52:07 -0300 Subject: [PATCH] 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. --- app/Http/Controllers/HomeController.php | 15 ++++++-- app/Http/Controllers/ReturnController.php | 2 +- app/Http/Controllers/TakeController.php | 2 +- app/Http/Controllers/UserController.php | 2 +- app/Item.php | 2 +- .../2018_09_08_192152_create_items_table.php | 4 +-- readme.md | 2 +- resources/views/home.blade.php | 36 ++++++------------- resources/views/home/unusedItem.blade.php | 7 ++++ resources/views/home/usedItem.blade.php | 21 +++++++++++ resources/views/item.blade.php | 2 +- 11 files changed, 59 insertions(+), 36 deletions(-) create mode 100644 resources/views/home/unusedItem.blade.php create mode 100644 resources/views/home/usedItem.blade.php diff --git a/app/Http/Controllers/HomeController.php b/app/Http/Controllers/HomeController.php index a4407f2..0c77010 100644 --- a/app/Http/Controllers/HomeController.php +++ b/app/Http/Controllers/HomeController.php @@ -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')); } } diff --git a/app/Http/Controllers/ReturnController.php b/app/Http/Controllers/ReturnController.php index 4643f24..d0acd62 100644 --- a/app/Http/Controllers/ReturnController.php +++ b/app/Http/Controllers/ReturnController.php @@ -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'); diff --git a/app/Http/Controllers/TakeController.php b/app/Http/Controllers/TakeController.php index bf84b53..ab30be1 100644 --- a/app/Http/Controllers/TakeController.php +++ b/app/Http/Controllers/TakeController.php @@ -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'); } diff --git a/app/Http/Controllers/UserController.php b/app/Http/Controllers/UserController.php index 070a4e0..8d81573 100644 --- a/app/Http/Controllers/UserController.php +++ b/app/Http/Controllers/UserController.php @@ -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 { diff --git a/app/Item.php b/app/Item.php index ba75eeb..23dfbb4 100644 --- a/app/Item.php +++ b/app/Item.php @@ -23,7 +23,7 @@ class Item extends Model return $query->where([ ['user_id', $user_id], ['product_id', $product_id], - ['usedBy', ''], + ['used_by', ''], ]); } 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 4cb3e6c..294d932 100644 --- a/database/migrations/2018_09_08_192152_create_items_table.php +++ b/database/migrations/2018_09_08_192152_create_items_table.php @@ -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(); }); } diff --git a/readme.md b/readme.md index 48b57b5..6f0d6ed 100644 --- a/readme.md +++ b/readme.md @@ -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 diff --git a/resources/views/home.blade.php b/resources/views/home.blade.php index 5d17104..0347eda 100644 --- a/resources/views/home.blade.php +++ b/resources/views/home.blade.php @@ -5,7 +5,7 @@
-
Dashboard
+

Your items

@if (session('status')) @@ -14,37 +14,21 @@
@endif -

Your itens

@forelse ($items as $item) -
-
  • + @if (!$loop->first) +
    + @endif +
    {{$item->name}} ({{$item->product->name}}) - @if ($item->usedBy) - @if ($item->usedBy == \Auth::id()) - -
    - {{ csrf_field() }} - - -
    -
    - {{\Carbon\Carbon::parse($item->updated_at)->diffForHumans()}} - @else - - @endif + @if ($item->used_by) + @include('home.usedItem') @else - -
    - {{ csrf_field() }} - - -
    -
    + @include('home.unusedItem') @endif -
  • +
    @empty -

    There are no items for you yet. Include a product or an item here.

    +

    There are no items shared with you yet. Share an item!

    @endforelse
    diff --git a/resources/views/home/unusedItem.blade.php b/resources/views/home/unusedItem.blade.php new file mode 100644 index 0000000..2d4d8b6 --- /dev/null +++ b/resources/views/home/unusedItem.blade.php @@ -0,0 +1,7 @@ + +
    + {{ csrf_field() }} + + +
    +
    \ No newline at end of file diff --git a/resources/views/home/usedItem.blade.php b/resources/views/home/usedItem.blade.php new file mode 100644 index 0000000..42f106e --- /dev/null +++ b/resources/views/home/usedItem.blade.php @@ -0,0 +1,21 @@ +@if ($item->used_by == \Auth::id()) + +
    + {{ csrf_field() }} + + +
    +
    + {{\Carbon\Carbon::parse($item->updated_at)->diffForHumans()}} +@else + +
    + {{ csrf_field() }} + + +
    +
    + + {{str_limit($users[$item->used_by], 15, '...')}} ({{$item->updated_at->diffForHumans()}}) + +@endif \ No newline at end of file diff --git a/resources/views/item.blade.php b/resources/views/item.blade.php index 1f5a95c..7462e8c 100644 --- a/resources/views/item.blade.php +++ b/resources/views/item.blade.php @@ -19,7 +19,7 @@ Other items from the same product: