Including Alert me!

This commit is contained in:
Bruno F. Fontes 2018-09-15 02:09:07 -03:00
parent c4bd6a8a1a
commit a173e3923a
7 changed files with 107 additions and 4 deletions

View File

@ -0,0 +1,28 @@
<?php
namespace App\Http\Controllers;
use \App\User;
use Illuminate\Http\Request;
class AlertController extends Controller
{
public function store(Request $request)
{
$item = User::find(\Auth::id())->items()->find(request('item'));
$item->waiting_user_id = \Auth::id();
$item->timestamps = false;
$item->save();
return redirect('home');
}
public function delete(Request $request)
{
$item = User::find(\Auth::id())->items()->find(request('item'));
$item->waiting_user_id = null;
$item->timestamps = false;
$item->save();
return redirect('home');
}
}

View File

@ -50,7 +50,7 @@ class HomeController extends Controller
} }
if ($item->waiting_user_id && !isset($users[$item->waiting_user_id])) { if ($item->waiting_user_id && !isset($users[$item->waiting_user_id])) {
$users[$item->waiting_user_id] = $this->findName($item, $item->waiting_user_id); $users[$item->waiting_user_id] = $this->findName($item->users, $item->waiting_user_id);
} }
} }
return view('home', compact('items', 'users')); return view('home', compact('items', 'users'));

View File

@ -4,6 +4,7 @@ namespace App\Http\Controllers;
use \App\Item; use \App\Item;
use \App\User; use \App\User;
use App\Mail\ItemAvailable;
use Illuminate\Http\Request; use Illuminate\Http\Request;
class TakeController extends Controller class TakeController extends Controller
@ -19,9 +20,19 @@ class TakeController extends Controller
public function delete(Request $request) public function delete(Request $request)
{ {
$item = User::find(\Auth::id())->items()->find(request('item')); $item = User::find(\Auth::id())->items()->find(request('item'));
$waiting_id = $item->waiting_user_id;
$item->used_by = null; $item->used_by = null;
$item->waiting_user_id = null;
$item->save(); $item->save();
//Send e-mail to waiting user
if ($waiting_id) {
$user = User::find($waiting_id);
\Mail::to($user)->send(
new ItemAvailable($user->name, $item)
);
}
return redirect('home'); return redirect('home');
} }
} }

View File

@ -0,0 +1,38 @@
<?php
namespace App\Mail;
use \App\Item;
use Illuminate\Bus\Queueable;
use Illuminate\Mail\Mailable;
use Illuminate\Queue\SerializesModels;
use Illuminate\Contracts\Queue\ShouldQueue;
class ItemAvailable extends Mailable
{
public $item;
public $username;
use Queueable, SerializesModels;
/**
* Create a new message instance.
*
* @return void
*/
public function __construct($username, Item $item)
{
$this->item = $item;
$this->username = $username;
}
/**
* Build the message.
*
* @return $this
*/
public function build()
{
return $this->subject($this->item->name . ' is available!')
->markdown('emails.itemAvailable');
}
}

View File

@ -0,0 +1,12 @@
@component('mail::message')
Hi, {{$username}},
&nbsp;<br>
&nbsp;<br>
#Good news: {{$item->name}} is available!
&nbsp;<br>
The item <em>{{$item->name}} ({{$item->product->name}})</em> is now available on **Share&nbsp;It**.
&nbsp;<br>
&nbsp;<br>
<a href="https://shareit.brunofontes.net/home">Take it</a> before anyone else.
@endcomponent

View File

@ -15,10 +15,21 @@
<form action="/alert" method="POST"> <form action="/alert" method="POST">
{{ csrf_field() }} {{ csrf_field() }}
<input type="hidden" name="item" value="{{$item->id}}"> <input type="hidden" name="item" value="{{$item->id}}">
@if ($item->waiting_user_id == \Auth::id())
@method('DELETE')
<button type="submit" class="btn btn-sm btn-outline-danger">Cancel Alert</button>
@elseif (!$item->waiting_user_id)
<button type="submit" class="btn btn-sm btn-outline-secondary">Alert me</button> <button type="submit" class="btn btn-sm btn-outline-secondary">Alert me</button>
@endif
</form> </form>
</span> </span>
<span class="float-right mr-3"> <span class="float-right mr-3">
<em>{{str_limit($users[$item->used_by], 15, '...')}} ({{$item->updated_at->diffForHumans()}})</em> <em>
{{str_limit($users[$item->used_by], 15, '...')}}
@if ($item->waiting_user_id && $item->waiting_user_id != \Auth::id())
<strong>> {{str_limit($users[$item->waiting_user_id], 15, '...')}}</strong>
@endif
<small>({{$item->updated_at->diffForHumans()}})</small>
</em>
</span> </span>
@endif @endif

View File

@ -25,7 +25,10 @@ Route::post('/item', 'ItemController@store')->middleware('auth');
Route::delete('/item', 'ItemController@delete')->middleware('auth'); Route::delete('/item', 'ItemController@delete')->middleware('auth');
Route::post('/take', 'TakeController@store')->middleware('auth'); Route::post('/take', 'TakeController@store')->middleware('auth');
Route::post('/return', 'ReturnController@store')->middleware('auth'); Route::delete('/take', 'TakeController@delete')->middleware('auth');
Route::post('/alert', 'AlertController@store')->middleware('auth');
Route::delete('/alert', 'AlertController@delete')->middleware('auth');
Route::post('/user', 'UserController@store')->middleware('auth'); Route::post('/user', 'UserController@store')->middleware('auth');
Route::delete('/user', 'UserController@delete')->middleware('auth'); Route::delete('/user', 'UserController@delete')->middleware('auth');