Skip to content

Commit

Permalink
Product Checkout feature: wip
Browse files Browse the repository at this point in the history
  • Loading branch information
Slymee committed Mar 20, 2024
1 parent 96701de commit 37584e5
Show file tree
Hide file tree
Showing 11 changed files with 190 additions and 19 deletions.
71 changes: 71 additions & 0 deletions app/Http/Controllers/CheckoutController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
<?php

namespace App\Http\Controllers;

use App\Http\Requests\ProductCheckoutRequest;
use App\Repositories\Interfaces\CheckoutRepositoryInterface;
use Illuminate\Http\Request;

class CheckoutController extends Controller
{
protected $checkoutRepository;
public function __construct(CheckoutRepositoryInterface $checkoutRepository)
{
$this->checkoutRepository = $checkoutRepository;
}
/**
* Display a listing of the resource.
*/
public function index(ProductCheckoutRequest $request)
{
$cheeckoutProducts = $this->checkoutRepository->getCheckoutProducts($request->all());
}

/**
* Show the form for creating a new resource.
*/
public function create()
{
//
}

/**
* Store a newly created resource in storage.
*/
public function store(Request $request)
{
//
}

/**
* Display the specified resource.
*/
public function show(string $id)
{
//
}

/**
* Show the form for editing the specified resource.
*/
public function edit(string $id)
{
//
}

/**
* Update the specified resource in storage.
*/
public function update(Request $request, string $id)
{
//
}

/**
* Remove the specified resource from storage.
*/
public function destroy(string $id)
{
//
}
}
30 changes: 30 additions & 0 deletions app/Http/Requests/ProductCheckoutRequest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<?php

namespace App\Http\Requests;

use Illuminate\Foundation\Http\FormRequest;

class ProductCheckoutRequest extends FormRequest
{
/**
* Determine if the user is authorized to make this request.
*/
public function authorize(): bool
{
return true;
}

/**
* Get the validation rules that apply to the request.
*
* @return array<string, \Illuminate\Contracts\Validation\ValidationRule|array<mixed>|string>
*/
public function rules(): array
{
return [
'buyer_id' => ['required', 'bail', 'integer'],
'product_id' => ['required', 'bail', 'integer'],
'quantity' => ['required', 'bail', 'integer']
];
}
}
3 changes: 3 additions & 0 deletions app/Providers/RepositoryServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,11 @@

use App\Repositories\AdminCategoryRepository;
use App\Repositories\CartRepository;
use App\Repositories\CheckoutRepository;
use App\Repositories\CommentRepository;
use App\Repositories\Interfaces\AdminCategoryRepositoryInterface;
use App\Repositories\Interfaces\CartRepositoryInterface;
use App\Repositories\Interfaces\CheckoutRepositoryInterface;
use App\Repositories\Interfaces\CommentRepositoryInterface;
use App\Repositories\Interfaces\ProductRepositoryInterface;
use App\Repositories\Interfaces\SellerProductRepositoryInterface;
Expand All @@ -30,6 +32,7 @@ public function register(): void
$this->app->bind(AdminCategoryRepositoryInterface::class, AdminCategoryRepository::class);
$this->app->bind(UserRepositoryInterface::class, UserRepository::class);
$this->app->bind(CartRepositoryInterface::class, CartRepository::class);
$this->app->bind(CheckoutRepositoryInterface::class, CheckoutRepository::class);
}

/**
Expand Down
2 changes: 0 additions & 2 deletions app/Repositories/CartRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -70,8 +70,6 @@ public function updateQuantity(array $data): bool
$cartItem->save();
return true;
}catch (\Exception $e){
DB::rollBack();
dd($e->getMessage());
Log::error('Caught Exception: ' . $e->getMessage());
Log::error('Exception Details: ' . $e);
return false;
Expand Down
15 changes: 15 additions & 0 deletions app/Repositories/CheckoutRepository.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?php

declare(strict_types=1);

namespace App\Repositories;

use App\Repositories\Interfaces\CheckoutRepositoryInterface;

class CheckoutRepository implements CheckoutRepositoryInterface
{
public function getCheckoutProducts(array $data)
{
dd($data);
}
}
8 changes: 8 additions & 0 deletions app/Repositories/Interfaces/CheckoutRepositoryInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?php

namespace App\Repositories\Interfaces;

interface CheckoutRepositoryInterface
{
public function getCheckoutProducts(array $data);
}
14 changes: 1 addition & 13 deletions database/migrations/2024_03_12_084348_create_orders_table.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,24 +19,12 @@ public function up(): void
->onUpdate('cascade')
->onDelete('cascade');

$table->unsignedBigInteger('seller_id');
$table->foreign('seller_id')->references('id')
->on('users')
->onUpdate('cascade')
->onDelete('cascade');

$table->unsignedBigInteger('buyer_id');
$table->foreign('seller_id')->references('id')
$table->foreign('buyer_id')->references('id')
->on('users')
->onUpdate('cascade')
->onDelete('cascade');

$table->unsignedTinyInteger('quantity');

$table->unsignedSmallInteger('total_amount');

$table->string('state');

$table->timestamps();
});
}
Expand Down
36 changes: 36 additions & 0 deletions database/migrations/2024_03_20_030104_order_products.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

return new class extends Migration
{
/**
* Run the migrations.
*/
public function up(): void
{
Schema::create('order_products', function (Blueprint $table){
$table->id();

$table->unsignedBigInteger('order_id');
$table->foreign('order_id')->on('orders')
->references('id')
->onDelete('cascade');

$table->integer('quantity');
$table->decimal('price', 8, 2);

$table->timestamps();
});
}

/**
* Reverse the migrations.
*/
public function down(): void
{
//
}
};
5 changes: 5 additions & 0 deletions resources/views/userend/checkout-page.blade.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
@extends('userend.layouts.index-template')

@section('page-title')
Brand-Product Checkout
@endsection
13 changes: 9 additions & 4 deletions resources/views/userend/product-page.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,12 +32,17 @@
</div>
@else
<div class="forms-container">
<form method="post" action="#">
<form method="post" action="{{ route('checkout-page') }}">
@csrf
<input type="hidden" name="buyer_id" value="{{ auth()->id() }}">
<input type="hidden" id="purchase-number-of-items" value="1" name="quantity">
<input type="hidden" name="" value="{{ $product->id }}">
<input type="hidden" name="product_id" value="{{ $product->id }}">
<div class="button-container">
<a href="#"><button type="submit">Purchase</button></a>
@if(auth()->id()==$product->user_id)
<a href="#"><button onclick="cantBuySelfProduct(event)">Purchase</button></a>
@else
<a href="#"><button type="submit">Purchase</button></a>
@endif
</div>
</form>

Expand Down Expand Up @@ -128,7 +133,7 @@ function toggleComment(commentId){
function cantBuySelfProduct(event){
event.preventDefault();
alert("You can't add your own products to cart!!");
alert("You can't purchase your own products!!");
}
</script>

Expand Down
12 changes: 12 additions & 0 deletions routes/product-order-cart.php
Original file line number Diff line number Diff line change
@@ -1,11 +1,23 @@
<?php

use App\Http\Controllers\CartController;
use App\Http\Controllers\CheckoutController;
use Illuminate\Support\Facades\Route;

/**
* Routes for cart operations
*/
Route::middleware(['guest.authenticate'])->group(function (){
Route::get('{userId}/cart-items', [CartController::class, 'index'])->name('cart-items');
Route::post('add-to-cart', [CartController::class, 'store'])->name('add-to-cart');
Route::put('cart-quantity-change', [CartController::class, 'update'])->name('change-cart-item-quantity');
Route::post('remove-from-cart', [CartController::class, 'destroy'])->name('remove-from-cart');
});


/**
* Routes for checkout operations
*/
Route::middleware(['guest.authenticate'])->group(function (){
Route::post('items-checkout', [CheckoutController::class, 'index'])->name('checkout-page');
});

0 comments on commit 37584e5

Please sign in to comment.