mirror of
				https://github.com/brunofontes/shareit.git
				synced 2025-10-31 01:51:06 -03:00 
			
		
		
		
	I made the code more passive, avoiding issued at taking, returning, storing alerts or removing alerts from an item. Now they all check if it is with you before returning/deleting alert etc. I am not sure if all cases are covered, but they are better than before. I had one only issued on this on that time, but I prefer to prioritize safety/security. I took the opportunitie to move some code from Controllers to the model itself, as they were changing with the DB.
		
			
				
	
	
		
			65 lines
		
	
	
		
			1.3 KiB
		
	
	
	
		
			PHP
		
	
	
	
	
	
			
		
		
	
	
			65 lines
		
	
	
		
			1.3 KiB
		
	
	
	
		
			PHP
		
	
	
	
	
	
| <?php
 | |
| 
 | |
| namespace App\Http\Controllers;
 | |
| 
 | |
| use Auth;
 | |
| use Lang;
 | |
| use App\Item;
 | |
| use App\User;
 | |
| use App\Events\ReturnItem;
 | |
| use Illuminate\Http\Request;
 | |
| use PhpParser\Node\Stmt\TryCatch;
 | |
| 
 | |
| /**
 | |
|  * Responsible to Take and Return an Item.
 | |
|  */
 | |
| class TakeController extends Controller
 | |
| {
 | |
|     /**
 | |
|      * The user take an item
 | |
|      *
 | |
|      * @param Request $request The form data
 | |
|      * 
 | |
|      * @return home view
 | |
|      */
 | |
|     public function store(Request $request)
 | |
|     {
 | |
|         $item = User::loggedIn()->items()->find(request('item'));
 | |
| 
 | |
|         try {
 | |
|             $item->takeItem();
 | |
|         } catch (\Exception $e) {
 | |
|             return back()->withErrors(
 | |
|                 Lang::getFromJson('This item is already taken')
 | |
|             );
 | |
|         }
 | |
| 
 | |
|         return redirect('home');
 | |
|     }
 | |
| 
 | |
|     /**
 | |
|      * User return an item
 | |
|      * Trigger an event: ReturnItem
 | |
|      *
 | |
|      * @param Request $request Form data
 | |
|      * 
 | |
|      * @return View home
 | |
|      */
 | |
|     public function delete(Request $request)
 | |
|     {
 | |
|         $item = User::loggedIn()->items()->find(request('item'));
 | |
| 
 | |
|         try {
 | |
|             $item->returnItem();
 | |
| 
 | |
|         } catch (\Exception $e) {
 | |
|             return back()->withErrors(
 | |
|                 Lang::getFromJson("You cannot return an item that is not with you")
 | |
|             );
 | |
|         }
 | |
| 
 | |
|         event(new ReturnItem($item));
 | |
|         return redirect('home');
 | |
|     }
 | |
| }
 |