mirror of
https://github.com/brunofontes/shareit.git
synced 2024-11-24 12:17:10 +00:00
Bruno Fontes
1350c62f14
The user can only open their own products. If the user tried to open a product that belongs to other person, the site were returning an error message. Now it just go back to the last page.
79 lines
2.0 KiB
PHP
79 lines
2.0 KiB
PHP
<?php
|
|
|
|
namespace App\Http\Controllers;
|
|
|
|
use \App\Item;
|
|
use \App\User;
|
|
use \App\Product;
|
|
use Illuminate\Http\Request;
|
|
|
|
class ProductController extends Controller
|
|
{
|
|
public function index()
|
|
{
|
|
$products = Product::fromAuthUser()->get();
|
|
return view('product.index', compact('products'));
|
|
}
|
|
|
|
/**
|
|
* Stores the included product into database
|
|
*
|
|
* @return (view) The product view
|
|
*/
|
|
public function store(Request $request)
|
|
{
|
|
$request->validate(['product' => 'required']);
|
|
Product::create(['name' => request('product'), 'user_id' => \Auth::id(), 'url' => request('url') ?? '']); //Just remember to add the fillable on Model to make this work
|
|
return redirect('product');
|
|
}
|
|
|
|
/**
|
|
* Delete a specified Product
|
|
*
|
|
* @param (int) $id The product id
|
|
*/
|
|
public function delete(Request $request)
|
|
{
|
|
$request->validate(['product' => 'required']);
|
|
$product = User::loggedIn()
|
|
->products()->with('items')->find(request('product'));
|
|
|
|
//Detach users from this item
|
|
foreach ($product->items as $item) {
|
|
Item::deleteAndDetach($item);
|
|
}
|
|
|
|
//Delete product
|
|
$product->delete();
|
|
return redirect('product');
|
|
}
|
|
|
|
public function patch(Request $request)
|
|
{
|
|
$request->validate(['product' => 'required', 'name' => 'required']);
|
|
$product = User::loggedIn()->products()->find(request('product'));
|
|
$product->name = request('name');
|
|
$product->url = request('url');
|
|
$product->save();
|
|
return redirect('product/'.request('product'));
|
|
}
|
|
|
|
/**
|
|
* Show a specified Product
|
|
*
|
|
* @param (int) $id The product id
|
|
*
|
|
* @return \Illuminate\Http\Response
|
|
*/
|
|
public function show($id)
|
|
{
|
|
$product = Product::fromAuthUser()->find($id);
|
|
|
|
if (!$product) {
|
|
return back();
|
|
|
|
}
|
|
return view('product.show', compact('product'));
|
|
}
|
|
}
|