diff --git a/.php-cs-fixer.cache b/.php-cs-fixer.cache new file mode 100644 index 0000000..805fc12 --- /dev/null +++ b/.php-cs-fixer.cache @@ -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}} \ No newline at end of file diff --git a/app/Http/Controllers/OrderController.php b/app/Http/Controllers/OrderController.php index e445bd2..2c6a925 100644 --- a/app/Http/Controllers/OrderController.php +++ b/app/Http/Controllers/OrderController.php @@ -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); - + $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); $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); + } } /** diff --git a/app/Models/Addres.php b/app/Models/Address.php similarity index 55% rename from app/Models/Addres.php rename to app/Models/Address.php index 2d90314..08ab1c4 100644 --- a/app/Models/Addres.php +++ b/app/Models/Address.php @@ -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', ]; diff --git a/app/Models/Order.php b/app/Models/Order.php index 00973f6..a525ad8 100644 --- a/app/Models/Order.php +++ b/app/Models/Order.php @@ -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"); } } diff --git a/app/Models/User.php b/app/Models/User.php index 0365590..a0d4446 100644 --- a/app/Models/User.php +++ b/app/Models/User.php @@ -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"); + } } diff --git a/database/migrations/2021_11_01_204536_create_orders_table.php b/database/migrations/2021_11_01_204536_create_orders_table.php index 097b6f5..fc26720 100644 --- a/database/migrations/2021_11_01_204536_create_orders_table.php +++ b/database/migrations/2021_11_01_204536_create_orders_table.php @@ -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(); }); diff --git a/resources/scss/app.scss b/resources/scss/app.scss index 05ce550..93c0659 100644 --- a/resources/scss/app.scss +++ b/resources/scss/app.scss @@ -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; diff --git a/resources/views/components/product.blade.php b/resources/views/components/product.blade.php index 2ab8dcf..7d147d6 100644 --- a/resources/views/components/product.blade.php +++ b/resources/views/components/product.blade.php @@ -2,7 +2,10 @@
- -

{{ $product->name }}

+ +
+

{{ $product->name }}

+

{{ ($product->price) }}

+
diff --git a/resources/views/order/create.blade.php b/resources/views/order/create.blade.php index 0bab395..a07d6ea 100644 --- a/resources/views/order/create.blade.php +++ b/resources/views/order/create.blade.php @@ -14,9 +14,14 @@ @endif
-
- - + + @foreach($products as $product) + + + @endforeach + + + @csrf
diff --git a/resources/views/order/index.blade.php b/resources/views/order/index.blade.php new file mode 100644 index 0000000..789aad9 --- /dev/null +++ b/resources/views/order/index.blade.php @@ -0,0 +1,15 @@ +@extends('layouts.app') + +@section('title', "Zamówienia") + +@section('main') + @foreach ($orders as $order) +
+
+ @foreach ($order->products as $product) + + @endforeach +
+
+ @endforeach +@endsection() diff --git a/resources/views/user.blade.php b/resources/views/user.blade.php index ff2b9cd..e250c6f 100644 --- a/resources/views/user.blade.php +++ b/resources/views/user.blade.php @@ -12,9 +12,16 @@

Your cart:

+
+ @csrf + @foreach($user->cart as $product) + + @endforeach() + +
@foreach($user->cart as $product)
- +
@csrf diff --git a/routes/web.php b/routes/web.php index a87a229..6c8dbfe 100644 --- a/routes/web.php +++ b/routes/web.php @@ -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");