shareit/app/Http/Controllers/AlertController.php

61 lines
1.4 KiB
PHP
Raw Normal View History

2018-09-15 05:09:07 +00:00
<?php
namespace App\Http\Controllers;
use Auth;
use Mail;
2018-09-15 05:09:07 +00:00
use \App\User;
use App\FlashMessage;
use \App\Mail\UserWaiting;
2018-09-15 05:09:07 +00:00
use Illuminate\Http\Request;
class AlertController extends Controller
{
/**
* Store the waiting_user_id on db
* so the user can be alerted when
* the item is free
*
* @param Request $request Form data
*
* @return redirect to home
*/
2018-09-15 05:09:07 +00:00
public function store(Request $request)
{
$item = User::loggedIn()->items()->find(request('item'));
if (!$item->used_by) {
session()->flash(
FlashMessage::PRIMARY,
__('Oh! This item has just being returned. Take it before anyone else!')
);
return redirect('home');
}
if ($item->used_by == Auth::id()) {
return redirect('home');
}
$item->storeAlert();
$loggedUser = Auth::user()->name;
$userWithItem = User::find($item->used_by);
Mail::to($userWithItem)
->locale($userWithItem->language)
->send(new UserWaiting($loggedUser, $userWithItem->name, $item));
2018-09-15 05:09:07 +00:00
return redirect('home');
}
public function delete(Request $request)
{
$item = User::loggedIn()->items()->find(request('item'));
2018-09-15 05:09:07 +00:00
if ($item->waiting_user_id != Auth::id()) {
return redirect('home');
}
$item->removeAlert();
2018-09-15 05:09:07 +00:00
return redirect('home');
}
}