Order backend

This commit is contained in:
bad 2021-12-07 12:22:46 +01:00
parent 931b8dea5a
commit 9ff9243944
12 changed files with 99 additions and 21 deletions

1
.php-cs-fixer.cache Normal file
View file

@ -0,0 +1 @@
{"php":"8.0.13","version":"3.0.0","indent":" ","lineEnding":"\n","rules":{"blank_line_after_opening_tag":true,"braces":{"allow_single_line_anonymous_class_with_empty_body":true},"compact_nullable_typehint":true,"declare_equal_normalize":true,"lowercase_cast":true,"lowercase_static_reference":true,"new_with_braces":true,"no_blank_lines_after_class_opening":true,"no_leading_import_slash":true,"no_whitespace_in_blank_line":true,"ordered_class_elements":{"order":["use_trait"]},"ordered_imports":{"imports_order":["class","function","const"],"sort_algorithm":"none"},"return_type_declaration":true,"short_scalar_cast":true,"single_blank_line_before_namespace":true,"single_trait_insert_per_statement":true,"ternary_operator_spaces":true,"visibility_required":{"elements":["const","method","property"]},"blank_line_after_namespace":true,"class_definition":true,"constant_case":true,"elseif":true,"function_declaration":true,"indentation_type":true,"line_ending":true,"lowercase_keywords":true,"method_argument_space":{"on_multiline":"ensure_fully_multiline"},"no_break_comment":true,"no_closing_tag":true,"no_spaces_after_function_name":true,"no_spaces_inside_parenthesis":true,"no_trailing_whitespace":true,"no_trailing_whitespace_in_comment":true,"single_blank_line_at_eof":true,"single_class_element_per_statement":{"elements":["property"]},"single_import_per_statement":true,"single_line_after_imports":true,"switch_case_semicolon_to_colon":true,"switch_case_space":true,"encoding":true,"full_opening_tag":true},"hashes":{"\/run\/user\/1000\/neoformat\/product.blade.php":1493321553}}

View file

@ -2,9 +2,10 @@
namespace App\Http\Controllers;
use App\Models\Addres;
use App\Models\Address;
use App\Models\Order;
use App\Models\Product;
use App\Models\User;
use Error;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
@ -20,7 +21,11 @@ class OrderController extends Controller
*/
public function index()
{
//
$user = Auth::user();
if (!$user) {
throw new AccessDeniedHttpException("Not logged in");
}
return view("order/index", ["orders" => $user->orders]);
}
/**
@ -28,9 +33,14 @@ class OrderController extends Controller
*
* @return \Illuminate\Http\Response
*/
public function create()
public function create(Request $request)
{
return view("order/create");
$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]);
//
}
@ -49,18 +59,30 @@ class OrderController extends Controller
$validated = $request->validate([
'products' => 'required|array',
'products.*' => 'exists:products,uuid'
'products.*' => 'exists:products,uuid',
'address' => 'required',
]);
DB::transaction(function () use ($validated, $user) {
$products = array_map(fn ($v) => Product::query()->where("uuid", $v)->first(), $validated["products"]);
$order = new Order($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);
$order->user()->associate($user);
$order->cost = array_reduce($products, fn ($c, $i) => $c+=$i->price, 0);
$order->cost = array_reduce($products, fn ($c, $i) => bcadd($c,$i->price), 0);
return $order->save();
$order->save();
foreach($products as $product) {
$order->products()->attach($product);
}
});
foreach($products as $product) {
$user->cart()->detach($product);
}
}
/**

View file

@ -5,10 +5,15 @@ namespace App\Models;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
class Addres extends Model
class Address extends Model
{
use HasFactory;
protected $table = "addresses";
public function user()
{
return $this->belongsTo(User::class, "userId", "uuid");
}
protected $fillable = [
'address',
];

View file

@ -13,7 +13,7 @@ class Order extends Model
protected $fillable = ['products'];
public function address()
{
return $this->belongsTo(Addres::class, "id", "addressId");
return $this->belongsTo(Address::class, "addressId", "id");
}
public function user()
@ -23,6 +23,6 @@ class Order extends Model
public function products()
{
return $this->belongsToMany("App\Product", "orderProduct", "productId", "orderId");
return $this->belongsToMany(Product::class, "orderProduct", "orderId" ,"productId");
}
}

View file

@ -54,4 +54,9 @@ class User extends Authenticatable
{
return $this->belongsToMany(Product::class, "cart_items", "userID", "productID");
}
public function orders()
{
return $this->hasMany(Order::class, "userID");
}
}

View file

@ -18,7 +18,7 @@ class CreateOrdersTable extends Migration
$table->foreignUuid("userId")->references("uuid")->on("users");
// Address validation for international users is basically impossible so just use a string
$table->string("addres");
$table->string("address");
$table->timestamps();
});

View file

@ -50,6 +50,18 @@ main {
max-width: 100%;
}
.name-price-container {
display: flex;
justify-content: space-between;
align-items: flex-end;
}
.price-currency {
font-size: 0.85em;
opacity: 0.55;
font-weight: normal;
}
footer {
display: flex;
justify-content: center;

View file

@ -3,6 +3,9 @@
<a href="{{ route('product.show', ['product' => $product]) }}">
<div class="product-container">
<img class="product-image" src="@if(isset($product->images[0])) {{ $product->images[0]->URL() }} @else {{ asset("test.txt") }} @endif">
<p class="product-name"> {{ $product->name }} </p>
<div class="name-price-container">
<h2 class="product-name"> {{ $product->name }} </h2>
<h3>{{ ($product->price) }}<span class="price-currency"></span></h3>
</div>
</div>
</a>

View file

@ -14,8 +14,13 @@
</div>
@endif
<div>
<form method="post" action="./">
<input type="text" name="products[0]">
<form method="post" action="{{ route("order.store") }}">
@foreach($products as $product)
<input type="hidden" name="products[{{ $loop->index }}]" value="{{ $product->uuid }}">
<x-product :product="$product" />
@endforeach
<textarea name="address"></textarea>
<input type="submit">
@csrf
</form>

View file

@ -0,0 +1,15 @@
@extends('layouts.app')
@section('title', "Zamówienia")
@section('main')
@foreach ($orders as $order)
<hr>
<div class="order-container" style="height: 10vh;">
@foreach ($order->products as $product)
<x-product :product="$product" />
@endforeach
</div>
<hr>
@endforeach
@endsection()

View file

@ -12,6 +12,13 @@
<div>
<h2>Your cart:</h2>
<div>
<form action="{{ route("order.create") }}" method="post">
@csrf
@foreach($user->cart as $product)
<input type="hidden" name="products[{{ $loop->index }}]" value="{{ $product->uuid }}">
@endforeach()
<button type="submit"> Order </button>
</form>
@foreach($user->cart as $product)
<div>
<x-product :product="$product" />

View file

@ -38,5 +38,8 @@ Route::post("/product/{product}/removeFromCart", [CartController::class, "remove
Route::resource("product", ProductController::class)->middleware("auth.admin");
Route::resource("product", ProductController::class)->only(["show"]);
Route::resource("order", OrderController::class)->middleware("auth");
Route::post('/order/create', [OrderController::class, "create"])->name("order.create");
Route::resource("image", ImageController::class)->only(["store", "delete", "create"])->middleware("auth.admin");