114 lines
2.7 KiB
PHP
114 lines
2.7 KiB
PHP
<?php
|
|
|
|
namespace App\Http\Controllers;
|
|
|
|
use App\Models\Addres;
|
|
use App\Models\Order;
|
|
use App\Models\Product;
|
|
use Error;
|
|
use Illuminate\Http\Request;
|
|
use Illuminate\Support\Facades\Auth;
|
|
use Illuminate\Support\Facades\DB;
|
|
use Symfony\Component\HttpKernel\Exception\AccessDeniedHttpException;
|
|
|
|
class OrderController 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()
|
|
{
|
|
return view("order/create");
|
|
//
|
|
}
|
|
|
|
/**
|
|
* Store a newly created resource in storage.
|
|
*
|
|
* @param \Illuminate\Http\Request $request
|
|
* @return \Illuminate\Http\Response
|
|
*/
|
|
public function store(Request $request)
|
|
{
|
|
$user = Auth::user();
|
|
if (!$user) {
|
|
throw new AccessDeniedHttpException("Not logged in");
|
|
}
|
|
|
|
$validated = $request->validate([
|
|
'products' => 'required|array',
|
|
'products.*' => 'exists:products,uuid'
|
|
]);
|
|
|
|
DB::transaction(function () use ($validated, $user) {
|
|
$products = array_map(fn ($v) => Product::query()->where("uuid", $v)->first(), $validated["products"]);
|
|
$order = new Order($products);
|
|
|
|
$order->user()->associate($user);
|
|
$order->cost = array_reduce($products,fn ($c, $i) => $c+=$i->price,0);
|
|
|
|
return $order->save();
|
|
});
|
|
}
|
|
|
|
/**
|
|
* Display the specified resource.
|
|
*
|
|
* @return \Illuminate\Http\Response
|
|
*/
|
|
public function show(Order $order)
|
|
{
|
|
//
|
|
}
|
|
|
|
/**
|
|
* Show the form for editing the specified resource.
|
|
*
|
|
* @return \Illuminate\Http\Response
|
|
*/
|
|
public function edit(Order $order)
|
|
{
|
|
//
|
|
}
|
|
|
|
/**
|
|
* Update the specified resource in storage.
|
|
*
|
|
* @param \Illuminate\Http\Request $request
|
|
* @return \Illuminate\Http\Response
|
|
*/
|
|
public function update(Request $request, Order $order)
|
|
{
|
|
$validated = $request->validate([
|
|
'address' => 'string',
|
|
]);
|
|
if(isset($validated["address"])) {
|
|
$address = $order->address()->withDefault(fn() => new Addres($validated));
|
|
$address->address = $validated["address"];
|
|
$order->save();
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Remove the specified resource from storage.
|
|
*
|
|
* @return \Illuminate\Http\Response
|
|
*/
|
|
public function destroy(Order $order)
|
|
{
|
|
throw new Error("TODO");
|
|
//
|
|
}
|
|
}
|