mirror of
https://github.com/brunofontes/shareit.git
synced 2025-12-13 11:10:42 -03:00
Compare commits
4 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
954820f46b
|
|||
|
8157a183c7
|
|||
|
528d3b4caf
|
|||
|
4ec2b24885
|
38
app/Events/ReturnItem.php
Normal file
38
app/Events/ReturnItem.php
Normal file
@@ -0,0 +1,38 @@
|
||||
<?php
|
||||
|
||||
namespace App\Events;
|
||||
|
||||
use \App\Item;
|
||||
use Illuminate\Broadcasting\Channel;
|
||||
use Illuminate\Queue\SerializesModels;
|
||||
use Illuminate\Broadcasting\PrivateChannel;
|
||||
use Illuminate\Broadcasting\PresenceChannel;
|
||||
use Illuminate\Foundation\Events\Dispatchable;
|
||||
use Illuminate\Broadcasting\InteractsWithSockets;
|
||||
use Illuminate\Contracts\Broadcasting\ShouldBroadcast;
|
||||
|
||||
class ReturnItem
|
||||
{
|
||||
use Dispatchable, InteractsWithSockets, SerializesModels;
|
||||
public $item;
|
||||
|
||||
/**
|
||||
* Create a new event instance.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function __construct(Item $item)
|
||||
{
|
||||
$this->item = $item;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the channels the event should broadcast on.
|
||||
*
|
||||
* @return \Illuminate\Broadcasting\Channel|array
|
||||
*/
|
||||
public function broadcastOn()
|
||||
{
|
||||
return new PrivateChannel('channel-name');
|
||||
}
|
||||
}
|
||||
@@ -2,6 +2,7 @@
|
||||
|
||||
namespace App\Http\Controllers;
|
||||
|
||||
use Auth;
|
||||
use \App\User;
|
||||
use \App\Mail\UserWaiting;
|
||||
use Illuminate\Http\Request;
|
||||
@@ -11,15 +12,15 @@ class AlertController extends Controller
|
||||
public function store(Request $request)
|
||||
{
|
||||
$item = User::loggedIn()->items()->find(request('item'));
|
||||
$item->waiting_user_id = \Auth::id();
|
||||
$item->waiting_user_id = Auth::id();
|
||||
$item->timestamps = false;
|
||||
$item->save();
|
||||
|
||||
$loggedUser = \Auth::user()->name;
|
||||
$loggedUser = Auth::user()->name;
|
||||
$userWithItem = User::find($item->used_by);
|
||||
\Mail::to($userWithItem)->send(
|
||||
new UserWaiting($loggedUser, $userWithItem->name, $item)
|
||||
);
|
||||
\Mail::to($userWithItem)
|
||||
->locale($userWithItem->language)
|
||||
->send(new UserWaiting($loggedUser, $userWithItem->name, $item));
|
||||
|
||||
return redirect('home');
|
||||
}
|
||||
|
||||
91
app/Http/Controllers/LanguageController.php
Normal file
91
app/Http/Controllers/LanguageController.php
Normal file
@@ -0,0 +1,91 @@
|
||||
<?php
|
||||
|
||||
namespace App\Http\Controllers;
|
||||
|
||||
use App;
|
||||
use Lang;
|
||||
use App\User;
|
||||
use Illuminate\Http\Request;
|
||||
|
||||
class LanguageController extends Controller
|
||||
{
|
||||
/**
|
||||
* Display a listing of the resource.
|
||||
*
|
||||
* @return \Illuminate\Http\Response
|
||||
*/
|
||||
public function index()
|
||||
{
|
||||
//
|
||||
}
|
||||
|
||||
/**
|
||||
* Show the form for creating a new resource.
|
||||
*
|
||||
* @return \Illuminate\Http\Response
|
||||
*/
|
||||
public function create()
|
||||
{
|
||||
//
|
||||
}
|
||||
|
||||
/**
|
||||
* Store a newly created resource in storage.
|
||||
*
|
||||
* @param \Illuminate\Http\Request $request
|
||||
* @return \Illuminate\Http\Response
|
||||
*/
|
||||
public function store(Request $request)
|
||||
{
|
||||
//
|
||||
}
|
||||
|
||||
/**
|
||||
* Display the specified resource.
|
||||
*
|
||||
* @param int $id
|
||||
* @return \Illuminate\Http\Response
|
||||
*/
|
||||
public function show($id)
|
||||
{
|
||||
//
|
||||
}
|
||||
|
||||
/**
|
||||
* Show the form for editing the specified resource.
|
||||
*
|
||||
* @param int $id
|
||||
* @return \Illuminate\Http\Response
|
||||
*/
|
||||
public function edit($id)
|
||||
{
|
||||
//
|
||||
}
|
||||
|
||||
/**
|
||||
* Update the specified resource in storage.
|
||||
*
|
||||
* @param int $locale
|
||||
* @return \Illuminate\Http\Response
|
||||
*/
|
||||
public function update($locale)
|
||||
{
|
||||
App::setLocale($locale);
|
||||
User::setLanguage($locale);
|
||||
|
||||
session(['lang' => $locale]);
|
||||
session()->save();
|
||||
return back();
|
||||
}
|
||||
|
||||
/**
|
||||
* Remove the specified resource from storage.
|
||||
*
|
||||
* @param int $id
|
||||
* @return \Illuminate\Http\Response
|
||||
*/
|
||||
public function destroy($id)
|
||||
{
|
||||
//
|
||||
}
|
||||
}
|
||||
@@ -4,8 +4,8 @@ namespace App\Http\Controllers;
|
||||
|
||||
use \App\Item;
|
||||
use \App\User;
|
||||
use App\Mail\ItemAvailable;
|
||||
use Illuminate\Http\Request;
|
||||
use App\Events\ReturnItem;
|
||||
|
||||
class TakeController extends Controller
|
||||
{
|
||||
@@ -27,19 +27,8 @@ class TakeController extends Controller
|
||||
public function delete(Request $request)
|
||||
{
|
||||
$item = User::loggedIn()->items()->find(request('item'));
|
||||
$waiting_id = $item->waiting_user_id;
|
||||
$item->used_by = null;
|
||||
$item->waiting_user_id = null;
|
||||
$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)
|
||||
);
|
||||
}
|
||||
|
||||
event(new ReturnItem($item));
|
||||
$item->returnItem();
|
||||
return redirect('home');
|
||||
}
|
||||
}
|
||||
|
||||
@@ -2,6 +2,7 @@
|
||||
|
||||
namespace App\Http\Middleware;
|
||||
|
||||
use App;
|
||||
use Closure;
|
||||
|
||||
class Locale
|
||||
@@ -15,7 +16,7 @@ class Locale
|
||||
*/
|
||||
public function handle($request, Closure $next)
|
||||
{
|
||||
\App::setLocale(session('lang'));
|
||||
App::setLocale(session('lang'));
|
||||
return $next($request);
|
||||
}
|
||||
}
|
||||
|
||||
12
app/Item.php
12
app/Item.php
@@ -38,4 +38,16 @@ class Item extends Model
|
||||
{
|
||||
return (new static)->where('user_id', \Auth::id());
|
||||
}
|
||||
|
||||
/**
|
||||
* Return a specified item
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function returnItem()
|
||||
{
|
||||
$this->used_by = null;
|
||||
$this->waiting_user_id = null;
|
||||
$this->save();
|
||||
}
|
||||
}
|
||||
|
||||
40
app/Listeners/AlertReturnedItem.php
Normal file
40
app/Listeners/AlertReturnedItem.php
Normal file
@@ -0,0 +1,40 @@
|
||||
<?php
|
||||
|
||||
namespace App\Listeners;
|
||||
|
||||
use Mail;
|
||||
use App\User;
|
||||
use App\Events\ReturnItem;
|
||||
use App\Mail\ItemAvailable;
|
||||
use Illuminate\Queue\InteractsWithQueue;
|
||||
use Illuminate\Contracts\Queue\ShouldQueue;
|
||||
|
||||
class AlertReturnedItem
|
||||
{
|
||||
/**
|
||||
* Create the event listener.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function __construct()
|
||||
{
|
||||
//
|
||||
}
|
||||
|
||||
/**
|
||||
* Send an email to the user that
|
||||
* is waiting for the item
|
||||
*
|
||||
* @param ReturnItem $item
|
||||
* @return void
|
||||
*/
|
||||
public function handle(ReturnItem $event)
|
||||
{
|
||||
if ($event->item->waiting_user_id) {
|
||||
$user = User::find($event->item->waiting_user_id);
|
||||
Mail::to($user)
|
||||
->locale($user->language)
|
||||
->send(new ItemAvailable($user->name, $event->item));
|
||||
}
|
||||
}
|
||||
}
|
||||
34
app/Listeners/SetLanguage.php
Normal file
34
app/Listeners/SetLanguage.php
Normal file
@@ -0,0 +1,34 @@
|
||||
<?php
|
||||
|
||||
namespace App\Listeners;
|
||||
|
||||
use App\User;
|
||||
use IlluminateAuthEventsLogin;
|
||||
use Illuminate\Queue\InteractsWithQueue;
|
||||
use Illuminate\Contracts\Queue\ShouldQueue;
|
||||
use Illuminate\Auth\Events\Login;
|
||||
|
||||
class SetLanguage
|
||||
{
|
||||
/**
|
||||
* Create the event listener.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function __construct()
|
||||
{
|
||||
//
|
||||
}
|
||||
|
||||
/**
|
||||
* Handle the event.
|
||||
*
|
||||
* @param IlluminateAuthEventsLogin $event
|
||||
* @return void
|
||||
*/
|
||||
public function handle(Login $event)
|
||||
{
|
||||
session(['lang' => User::loggedIn()->language]);
|
||||
session()->save();
|
||||
}
|
||||
}
|
||||
@@ -2,6 +2,7 @@
|
||||
|
||||
namespace App\Mail;
|
||||
|
||||
use Lang;
|
||||
use \App\Item;
|
||||
use Illuminate\Bus\Queueable;
|
||||
use Illuminate\Mail\Mailable;
|
||||
@@ -35,7 +36,7 @@ class UserWaiting extends Mailable
|
||||
public function build()
|
||||
{
|
||||
return $this->subject(
|
||||
\Lang::getFromJson(
|
||||
Lang::getFromJson(
|
||||
':waitinguser wants to use :itemname',
|
||||
[
|
||||
'waitinguser' => $this->waitingUser,
|
||||
|
||||
@@ -2,6 +2,9 @@
|
||||
|
||||
namespace App\Providers;
|
||||
|
||||
use App\Events\ReturnItem;
|
||||
use App\Listeners\SetLanguage;
|
||||
use App\Listeners\AlertReturnedItem;
|
||||
use Illuminate\Support\Facades\Event;
|
||||
use Illuminate\Auth\Events\Registered;
|
||||
use Illuminate\Auth\Listeners\SendEmailVerificationNotification;
|
||||
@@ -18,6 +21,14 @@ class EventServiceProvider extends ServiceProvider
|
||||
Registered::class => [
|
||||
SendEmailVerificationNotification::class,
|
||||
],
|
||||
|
||||
ReturnItem::class => [
|
||||
AlertReturnedItem::class,
|
||||
],
|
||||
|
||||
'Illuminate\Auth\Events\Login' => [
|
||||
SetLanguage::class,
|
||||
],
|
||||
];
|
||||
|
||||
/**
|
||||
|
||||
20
app/User.php
20
app/User.php
@@ -2,6 +2,7 @@
|
||||
|
||||
namespace App;
|
||||
|
||||
use Auth;
|
||||
use Illuminate\Notifications\Notifiable;
|
||||
use Illuminate\Contracts\Auth\MustVerifyEmail;
|
||||
use Illuminate\Foundation\Auth\User as Authenticatable;
|
||||
@@ -46,6 +47,23 @@ class User extends Authenticatable implements MustVerifyEmail
|
||||
*/
|
||||
public static function loggedIn()
|
||||
{
|
||||
return (new static)->findOrFail(\Auth::id());
|
||||
return (new static)->findOrFail(Auth::id());
|
||||
}
|
||||
|
||||
/**
|
||||
* Set the default website language
|
||||
* for the acual user
|
||||
*
|
||||
* @param string $language The language code
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public static function setLanguage(string $language)
|
||||
{
|
||||
if (Auth::check()) {
|
||||
$user = self::loggedIn();
|
||||
$user->language = $language;
|
||||
$user->save();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,32 @@
|
||||
<?php
|
||||
|
||||
use Illuminate\Support\Facades\Schema;
|
||||
use Illuminate\Database\Schema\Blueprint;
|
||||
use Illuminate\Database\Migrations\Migration;
|
||||
|
||||
class AddLocationToUsers extends Migration
|
||||
{
|
||||
/**
|
||||
* Run the migrations.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function up()
|
||||
{
|
||||
Schema::table('users', function (Blueprint $table) {
|
||||
$table->string('language')->after('email_verified_at')->default('en');
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Reverse the migrations.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function down()
|
||||
{
|
||||
Schema::table('users', function (Blueprint $table) {
|
||||
$table->dropColumn('language');
|
||||
});
|
||||
}
|
||||
}
|
||||
@@ -33,11 +33,11 @@
|
||||
</div>
|
||||
</div>
|
||||
@empty
|
||||
<p>@lang('home.no_messages')<a href="/product">@lang('home.share_item')</a></p>
|
||||
@endforelse
|
||||
</div>
|
||||
</div>
|
||||
@empty
|
||||
<p>@lang('home.no_messages') <a href="/product">@lang('home.share_item')</a></p>
|
||||
@endforelse
|
||||
</div>
|
||||
</div>
|
||||
|
||||
@@ -15,11 +15,7 @@ Route::get('/', function () {
|
||||
return view('welcome');
|
||||
});
|
||||
|
||||
Route::get('/lang/{locale}', function ($locale) {
|
||||
session(['lang' => $locale]);
|
||||
session()->save();
|
||||
return back();
|
||||
});
|
||||
Route::get('/lang/{locale}', 'LanguageController@update')->name('language');
|
||||
|
||||
Route::get('/product', 'ProductController@index')->middleware('verified');
|
||||
Route::get('/product/{product}', 'ProductController@show')->middleware('verified');
|
||||
|
||||
Reference in New Issue
Block a user