pai-sklep/app/Http/Controllers/OrderController.php

115 lines
2.7 KiB
PHP
Raw Normal View History

2021-11-14 14:51:49 +01:00
<?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);
2021-11-24 11:34:46 +01:00
$order->cost = array_reduce($products, fn ($c, $i) => $c+=$i->price, 0);
2021-11-14 14:51:49 +01:00
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',
]);
2021-11-24 11:34:46 +01:00
if (isset($validated["address"])) {
$address = $order->address()->withDefault(fn () => new Addres($validated));
2021-11-14 14:51:49 +01:00
$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");
//
}
}