Skip to content

Commit b2d431c

Browse files
authored
Merge pull request #55 from Abhishke391/feature/market-place
creating cart crud
2 parents dfe051e + 2fe2ae9 commit b2d431c

17 files changed

+827
-54
lines changed

controllers/CartController.php

+85
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
<?php
2+
3+
namespace app\controllers;
4+
use app\core\Application;
5+
use app\core\Controller;
6+
use app\core\Request;
7+
use app\core\Response;
8+
use app\models\cart;
9+
10+
class CartController extends Controller {
11+
12+
// add product to the cart
13+
public function addToCartController (Request $request, Response $response) {
14+
$user_id = Application::$app->session->get('customer');
15+
16+
if (!$user_id) {
17+
Application::$app->response->redirect('/customer-login');
18+
}
19+
20+
$product_id = $request->getBody()['product_id'];
21+
$quantity = $request->getBody()['quantity'] ?? 1;
22+
23+
if (!$product_id) {
24+
Application::$app->session->setFlash('error','no id getting ');
25+
}
26+
27+
$cart = new Cart();
28+
if ($cart->addToCart($user_id, $product_id, $quantity)) {
29+
Application::$app->session->setFlash('success','Product add to cart');
30+
// http_response_code(200); // Set HTTP status code
31+
// header('Content-Type: application/json'); // Set response header
32+
// echo json_encode(['message' => 'Product added to cart']);
33+
Application::$app->response->redirect('/market-place-home');
34+
35+
} else {
36+
Application::$app->session->setFlash('error','Prodcut add to cart fail');
37+
// http_response_code(400); // Set HTTP status code
38+
// header('Content-Type: application/json'); // Set response header
39+
// echo json_encode(['message' => 'Failed to add product to cart']);
40+
Application::$app->response->redirect('/market-place-home');
41+
}
42+
}
43+
44+
//function to retrive products
45+
public function viewCart() {
46+
if (!Application::$app->session->get('customer')) {
47+
Application::$app->response->redirect('/customer-login');
48+
}
49+
50+
$user_id = Application::$app->session->get('customer');
51+
$cart = new Cart();
52+
$cartItems = $cart->getCartItems($user_id);
53+
54+
if(empty($cartItems)){
55+
Application::$app->session->setFlash('error','Cart is empty');
56+
Application::$app->response->redirect('/market-place-home');
57+
}
58+
59+
$this->setLayout('auth');
60+
return $this->render('service-centre/view-cart', ['cartItems'=> $cartItems]);
61+
62+
}
63+
64+
//function to remove cart items
65+
public function removeItemsFromCart(Request $request, Response $response) {
66+
if (!Application::$app->session->get('customer')) {
67+
Application::$app->session->setFlash('error','Please login first');
68+
Application::$app->response->redirect('/customer-login');
69+
}
70+
$user_id = Application::$app->session->get('customer');
71+
$product_id = $request->getBody()['product_id'];
72+
73+
if (!$product_id) {
74+
Application::$app->session->setFlash('error','Select item to remove');
75+
}
76+
$cart = new Cart();
77+
if ($cart->removeCartItem($user_id, $product_id)) {
78+
Application::$app->response->redirect('/view-cart');
79+
Application::$app->session->setFlash('success','Product remove successfully');
80+
} else {
81+
Application::$app->session->setFlash('error','Product remove failed, try again');
82+
}
83+
}
84+
85+
}

controllers/ProductController.php

+20-1
Original file line numberDiff line numberDiff line change
@@ -135,8 +135,27 @@ public function delete(Request $request){
135135
}
136136

137137

138-
}
138+
public function filterProductByCategory(Request $request, Response $response) {
139+
//get category by the params
140+
141+
$category = $_GET['category'] ?? 'all';
142+
143+
if ($category === 'all') {
144+
$products = (new Product())->getAllProducts();
145+
} else {
146+
$products = (new Product())->getProductsByCategory($category);
147+
}
139148

149+
140150

151+
if ($products) {
152+
$this->setLayout('auth');
153+
return $this->render('service-centre/market-place-home', [
154+
'products'=> $products,
155+
]);
156+
}
157+
}
158+
159+
}
141160

142161
?>

controllers/ServiceCentreController.php

+6
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,12 @@ public function serviceCenterMessages()
124124
return $this->render('/service-centre/service-center-messages');
125125
}
126126

127+
public function cart()
128+
{
129+
$this->setLayout('auth');
130+
return $this->render('/service-centre/view-cart');
131+
}
132+
127133
public function viewServiceCenterProfile($id)
128134
{
129135

core/DbModel.php

+7
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,13 @@ abstract public function attributes(): array;
1111
abstract public function primaryKey(): string;
1212

1313

14+
// protected Database $db;
15+
16+
// public function __construct(){
17+
// $this->db = Application::$app->db;
18+
// }
19+
20+
1421
public function save()
1522
{
1623

models/Product.php

+25-2
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ class Product extends DbModel
1111
public string $description = '';
1212
public float $price;
1313
public string $media = '';
14+
public string $category;
1415
public ?string $created_at = null;
1516
public ?string $updated_at = null;
1617

@@ -21,7 +22,7 @@ public function tableName(): string
2122

2223
public function attributes(): array
2324
{
24-
return ['ser_cen_id', 'description', 'price', 'media'];
25+
return ['ser_cen_id', 'description', 'price', 'media', 'category'];
2526
}
2627
public function primaryKey(): string
2728
{
@@ -101,7 +102,7 @@ public function editProduct(): bool
101102
{
102103
$sql = "
103104
UPDATE product
104-
SET description = :description, price = :price, media = CASE WHEN :media = '' THEN media ELSE :media END, updated_at = NOW()
105+
SET description = :description, price = :price, category = :category, media = CASE WHEN :media = '' THEN media ELSE :media END, updated_at = NOW()
105106
WHERE product_id = :product_id AND ser_cen_id = :ser_cen_id
106107
";
107108

@@ -111,6 +112,7 @@ public function editProduct(): bool
111112
$stmt->bindValue(':media', $this->media);
112113
$stmt->bindValue(':product_id', $this->product_id);
113114
$stmt->bindValue(':ser_cen_id', $this->ser_cen_id);
115+
$stmt->bindValue(':category', $this->category);
114116

115117
// Debugging: Check SQL and parameters
116118
// var_dump([
@@ -163,6 +165,27 @@ public function updateRules(): array
163165

164166
];
165167
}
168+
169+
public function getProductsByCategory(string $category) {
170+
try {
171+
172+
$sql = 'SELECT p.*, s.name AS seller_name
173+
FROM product p
174+
JOIN service_center s ON p.ser_cen_id = s.ser_cen_id
175+
WHERE p.category = :category
176+
ORDER BY p.created_at DESC';
177+
178+
$stmt = self::prepare($sql);
179+
$stmt->bindValue(':category', $category);
180+
$stmt->execute();
181+
182+
return $stmt->fetchAll(\PDO::FETCH_ASSOC);
183+
184+
} catch (\Exception $e) {
185+
error_log($e->getMessage());
186+
return [];
187+
}
188+
}
166189
}
167190

168191
?>

models/cart.php

+121
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,121 @@
1+
<?php
2+
3+
namespace app\models;
4+
use app\core\DbModel;
5+
use app\core\Application;
6+
7+
class Cart extends DbModel
8+
{
9+
public int $id;
10+
public int $cart_id;
11+
public int $user_id;
12+
public int $product_id;
13+
public int $quantity;
14+
public ?string $created_at = null;
15+
public ?string $updated_at = null;
16+
17+
public function tableName(): string {
18+
return 'cart_items';
19+
}
20+
21+
public function attributes(): array {
22+
return ['cart_id','product_id','quantity'];
23+
}
24+
25+
public function primaryKey(): string {
26+
return 'id';
27+
}
28+
29+
// add a product to cart
30+
public function addToCart(int $user_id, int $product_id, int $quantity) {
31+
//get the cart_id for the user
32+
$sql = 'SELECT cart_id FROM customer WHERE cus_id = :cus_id';
33+
$stmt = self::prepare($sql);
34+
$stmt->bindValue(':cus_id', $user_id);
35+
$stmt->execute();
36+
$cart = $stmt->fetchColumn();
37+
38+
//if user does not have an cart, then create a one
39+
if (!$cart) {
40+
// $this->db->query("INSERT INTO cart (user_id) VALUES (?)", [$user_id]);
41+
// $cart = $this->db->lastInsertId();
42+
// $this->db->query("UPDATE customer SET cart_id = ? WHERE cus_id = ?", [$cart, $user_id]);
43+
$stmt = Application::$app->db->pdo->prepare("INSERT INTO cart (user_id) VALUES (?)");
44+
$stmt->execute([$user_id]);
45+
46+
// Get last inserted cart_id
47+
$cart = Application::$app->db->pdo->lastInsertId();
48+
49+
// Update customer table with the cart_id
50+
$stmt = Application::$app->db->pdo->prepare("UPDATE customer SET cart_id = ? WHERE cus_id = ?");
51+
$stmt->execute([$cart, $user_id]);
52+
}
53+
// $existing = $this->db->query("SELECT id FROM cart_items WHERE cart_id = ? AND product_id = ?", [$cart, $product_id])->fetchColumn();
54+
// Check if the product is already in the cart
55+
$stmt = Application::$app->db->pdo->prepare("SELECT id FROM cart_items WHERE cart_id = ? AND product_id = ?");
56+
$stmt->execute([$cart, $product_id]);
57+
$existing = $stmt->fetchColumn();
58+
59+
if ($existing) {
60+
// $this->db->query("UPDATE cart_items SET quantity = quantity + ? WHERE id = ?", [$quantity, $existing]);
61+
// Update quantity if product exists in cart
62+
$stmt = Application::$app->db->pdo->prepare("UPDATE cart_items SET quantity = quantity + ? WHERE id = ?");
63+
$stmt->execute([$quantity, $existing]);
64+
} else {
65+
// $this->db->query("INSERT INTO cart_items (cart_id, product_id, quantity) VALUES (?, ?, ?)", [$cart, $product_id, $quantity]);
66+
// Insert new product into cart_items
67+
$stmt = Application::$app->db->pdo->prepare("INSERT INTO cart_items (cart_id, product_id, quantity) VALUES (?, ?, ?)");
68+
$stmt->execute([$cart, $product_id, $quantity]);
69+
}
70+
71+
return true;
72+
73+
}
74+
75+
//get all items for a specific user
76+
public function getCartItems(int $user_id) {
77+
$db = Application::$app->db->pdo;
78+
$stmt = $db->prepare("
79+
SELECT ci.id, ci.product_id, ci.quantity, p.description, p.price, p.media
80+
FROM cart_items ci
81+
JOIN cart c ON ci.cart_id = c.id
82+
JOIN product p ON ci.product_id = p.product_id
83+
WHERE c.user_id = ?
84+
");
85+
$stmt->execute([$user_id]);
86+
return $stmt->fetchAll(\PDO::FETCH_ASSOC);
87+
}
88+
89+
//model to remove items from the cart
90+
public function removeCartItem(int $user_id, int $product_id) {
91+
$db = Application::$app->db->pdo;
92+
93+
$stmt = $db->prepare("SELECT id FROM cart WHERE user_id = ?");
94+
$stmt->execute([$user_id]);
95+
$cart = $stmt->fetch(\PDO::FETCH_ASSOC);
96+
97+
if (!$cart) {
98+
return false;
99+
}
100+
101+
$cart_id = $cart['id'];
102+
103+
$stmt = $db->prepare("DELETE FROM cart_items WHERE cart_id = ? AND product_id = ?");
104+
return $stmt->execute([$cart_id, $product_id]);
105+
}
106+
107+
public function rules(): array
108+
{
109+
return [
110+
111+
];
112+
}
113+
114+
public function updateRules(): array
115+
{
116+
return [
117+
118+
];
119+
}
120+
121+
}

public/css/service-center/add-products.css

+27
Original file line numberDiff line numberDiff line change
@@ -162,6 +162,33 @@ table tr:hover {
162162
background-color: #e9ecef;
163163
}
164164

165+
.category-select {
166+
width: 100%;
167+
max-width: 300px;
168+
padding: 10px;
169+
font-size: 14px;
170+
border: 2px solid #ccc;
171+
border-radius: 5px;
172+
background-color: #fff;
173+
color: #333;
174+
cursor: pointer;
175+
outline: none;
176+
transition: border-color 0.3s ease-in-out;
177+
}
178+
179+
.category-select:focus {
180+
border-color: #007bff;
181+
}
182+
183+
option {
184+
font-size: 14px;
185+
padding: 10px;
186+
}
187+
188+
.category-select:hover {
189+
border-color: #0056b3;
190+
}
191+
165192
/* Responsive Table */
166193
@media (max-width: 768px) {
167194
table {

0 commit comments

Comments
 (0)