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

137 lines
3.6 KiB
PHP
Raw Normal View History

2021-11-14 14:51:49 +01:00
<?php
namespace App\Http\Controllers;
2021-12-07 12:22:46 +01:00
use App\Models\Address;
2021-11-14 14:51:49 +01:00
use App\Models\Order;
use App\Models\Product;
2021-12-07 12:22:46 +01:00
use App\Models\User;
2021-11-14 14:51:49 +01:00
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()
{
2021-12-07 12:22:46 +01:00
$user = Auth::user();
if (!$user) {
throw new AccessDeniedHttpException("Not logged in");
}
return view("order/index", ["orders" => $user->orders]);
2021-11-14 14:51:49 +01:00
}
/**
* Show the form for creating a new resource.
*
* @return \Illuminate\Http\Response
*/
2021-12-07 12:22:46 +01:00
public function create(Request $request)
2021-11-14 14:51:49 +01:00
{
2021-12-07 12:22:46 +01:00
$validated = $request->validate([
'products' => 'required|array',
'products.*' => 'exists:products,uuid'
]);
$products = array_map(fn ($v) => Product::query()->where("uuid", $v)->first(), $validated["products"]);
return view("order/create", ["products" => $products]);
2021-11-14 14:51:49 +01:00
//
}
/**
* 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',
2021-12-07 12:22:46 +01:00
'products.*' => 'exists:products,uuid',
'address' => 'required',
2021-11-14 14:51:49 +01:00
]);
2021-12-07 12:22:46 +01:00
$products = array_map(fn ($v) => Product::query()->where("uuid", $v)->first(), $validated["products"]);
DB::transaction(function () use ($validated, $user, $products) {
$address = new Address($validated);
$address->user()->associate($user);
$address->save();
$order = new Order();
$order->address()->associate($address);
2021-11-14 14:51:49 +01:00
$order->user()->associate($user);
2021-12-07 12:22:46 +01:00
$order->cost = array_reduce($products, fn ($c, $i) => bcadd($c,$i->price), 0);
2021-11-14 14:51:49 +01:00
2021-12-07 12:22:46 +01:00
$order->save();
foreach($products as $product) {
$order->products()->attach($product);
}
2021-11-14 14:51:49 +01:00
});
2021-12-07 12:22:46 +01:00
foreach($products as $product) {
$user->cart()->detach($product);
}
2021-11-14 14:51:49 +01:00
}
/**
* 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");
//
}
}