Skip to content

Commit 3f12b65

Browse files
committed
CheckoutService implementation added
1 parent 852d5b2 commit 3f12b65

File tree

4 files changed

+66
-1
lines changed

4 files changed

+66
-1
lines changed
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
<?php
2+
3+
namespace App\Http\Controllers;
4+
5+
use App\Services\CheckoutService;
6+
use Illuminate\Http\RedirectResponse;
7+
8+
class ProductController extends Controller
9+
{
10+
private CheckoutService $service;
11+
12+
public function __construct()
13+
{
14+
$this->service = new CheckoutService([
15+
'FR1' => ['get_one_free', 1, 3.11],
16+
'SR1' => ['discount', 3, 4.5]
17+
]);
18+
}
19+
20+
public function index()
21+
{
22+
return view('products', [
23+
'products' => $this->service->get_products(),
24+
'total_price' => $this->service->calculateTotal()
25+
]);
26+
}
27+
28+
public function scan($product_code): RedirectResponse
29+
{
30+
$this->service->scan($product_code);
31+
32+
return redirect()->route('products.index');
33+
}
34+
}

app/Services/CheckoutService.php

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ class CheckoutService
99
private array $pricing_rules;
1010
private object $products;
1111
private object $cart;
12-
public float $total;
12+
private float $total;
1313

1414
public function __construct(array $pricing_rules)
1515
{
@@ -35,6 +35,11 @@ public function __construct(array $pricing_rules)
3535
$this->cart = collect();
3636
}
3737

38+
public function get_products(): object
39+
{
40+
return $this->products;
41+
}
42+
3843
public function scan(string $item): void
3944
{
4045
$product = $this->find($item);

resources/views/products.blade.php

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
@extends('layout')
2+
3+
@section('content')
4+
<div class="container mt-5 row">
5+
<div class="col-3">
6+
<ul class="list-group">
7+
@foreach($products as $product)
8+
<li class="list-group-item">
9+
{{$product['name']}} - ${{$product['price']}} |
10+
<a href="{{route('products.scan', $product['code'])}}">Scan</a>
11+
</li>
12+
@endforeach
13+
</ul>
14+
</div>
15+
16+
<div class="col-3 mt-1">
17+
<strong>Total price: {{$total_price}}</strong>
18+
</div>
19+
</div>
20+
@endsection

routes/web.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
<?php
22

3+
use App\Http\Controllers\ProductController;
34
use App\Http\Controllers\UserController;
45
use Illuminate\Support\Facades\Route;
56

@@ -22,3 +23,8 @@
2223
Route::get('/', [UserController::class, 'index'])->name('index');
2324
Route::post('/search', [UserController::class, 'search'])->name('search');
2425
});
26+
27+
Route::prefix('products')->name('products.')->group(function() {
28+
Route::get('/', [ProductController::class, 'index'])->name('index');
29+
Route::get('/scan/{product_code}', [ProductController::class, 'scan'])->name('scan');
30+
});

0 commit comments

Comments
 (0)