Skip to content

Commit 0ae575e

Browse files
authored
Merge pull request #39 from Abhishke391/feature/service-centre
Feature/service centre
2 parents 9593c06 + 8cc823a commit 0ae575e

10 files changed

+136
-70
lines changed

.env

-3
Original file line numberDiff line numberDiff line change
@@ -1,3 +0,0 @@
1-
DB_DSN = "mysql:host=localhost;dbname=fixmedb"
2-
DB_USER = root
3-
DB_PASSWORD =

controllers/ProductController.php

+55-36
Original file line numberDiff line numberDiff line change
@@ -11,34 +11,34 @@ class ProductController extends Controller
1111
{
1212
public function create(Request $request)
1313
{
14-
$product = new Product();
14+
$productModel = new Product();
1515
$ser_cen_id = Application::$app->session->get('serviceCenter');
1616

1717
if (!$ser_cen_id) {
1818
Application::$app->session->setFlash('error', 'Please log in to create a product.');
1919
Application::$app->response->redirect('/service-centre-login');
2020
return;
2121
}
22-
$product->ser_cen_id = $ser_cen_id;
22+
$productModel->ser_cen_id = $ser_cen_id;
2323

2424
if ($request->isPost()) {
25-
$product->loadData($request->getBody());
25+
$productModel->loadData($request->getBody());
2626

2727
if (!empty($_FILES['media']['name'])) {
2828
$uploadDir = 'assets/uploads/';
29-
$product->media = $_FILES['media']['name'];
30-
$targetFile = $uploadDir . basename($product->media);
29+
$productModel->media = $_FILES['media']['name'];
30+
$targetFile = $uploadDir . basename($productModel->media);
3131

3232
if (!move_uploaded_file($_FILES['media']['tmp_name'], $targetFile)) {
3333
Application::$app->session->setFlash('error', 'Failed to upload file.');
3434
return $this->render('service-centre/create-product', [
35-
'model' => $product,
35+
'model' => $productModel,
3636
'products' => [] // Ensure products is passed even if empty
3737
]);
3838
}
3939
}
4040

41-
if ($product->validate() && $product->save()) {
41+
if ($productModel->validate() && $productModel->save()) {
4242
Application::$app->session->setFlash('success', 'Product created successfully.');
4343
Application::$app->response->redirect('/service-center-create-product');
4444
return;
@@ -48,71 +48,90 @@ public function create(Request $request)
4848
// Fetch products for the logged-in service center
4949
// Output the dump in the HTML
5050

51-
// $products = $product->getProductByServiceCenter($ser_cen_id);
51+
// $productModels = $productModel->getProductByServiceCenter($ser_cen_id);
5252

5353
return $this->render('/service-centre/create-product', [
5454

55-
'model' => $product,
56-
// 'products' => $products
55+
'model' => $productModel,
56+
// 'products' => $productModels
5757
// Pass the products to the view
5858
]);
5959
}
6060

6161
public function filterProductsById()
6262
{
6363
$ser_cen_id = Application::$app->session->get('serviceCenter');
64-
$products = (new Product)->getProductByServiceCenter($ser_cen_id);
64+
$productModels = (new Product)->getProductByServiceCenter($ser_cen_id);
6565
$this->setLayout('auth');
6666
return $this->render('service-centre/create-product', [
67-
'products' => $products
67+
'products' => $productModels
6868
]);
6969
}
7070

71+
7172
public function index()
7273
{
73-
$products = (new Product)->getAllProducts(); // Fetch all products from the database
74+
$productModels = (new Product)->getAllProducts(); // Fetch all products from the database
7475
$this->setLayout('auth'); // Set layout if needed
7576
return $this->render('/service-centre/market-place-home', [
76-
'products' => $products // Pass products to the view
77+
'products' => $productModels // Pass products to the view
7778
]);
7879
}
7980

8081
public function update(Request $request)
8182
{
82-
$product = new Product();
83+
$productModel = new Product();
8384
$ser_cen_id = Application::$app->session->get('serviceCenter');
84-
8585
if (!$ser_cen_id) {
86-
Application::$app->session->setFlash('error', 'Please log in to create a product.');
86+
Application::$app->session->setFlash('error', 'Please log in to update a product.');
8787
Application::$app->response->redirect('/service-centre-login');
8888
}
89+
if ($request->isGet()){
90+
$product_id = $request->getBody()['product_id'] ?? null;
91+
92+
if ($product_id) {
93+
$product = $productModel->getProductByIdAndServiceCenter($ser_cen_id, $product_id);
94+
if ($product) {
95+
$this->setLayout('auth');
96+
return $this->render('service-centre/update-product', [
97+
'product' => $product
98+
]);
99+
}
100+
}
101+
else {
102+
Application::$app->session->setFlash('error', 'invalid product id.');
103+
Application::$app->response->redirect('/service-centre-login');
104+
}
105+
106+
}
89107

90108
if ($request->isPost()) {
91-
$product->loadData($request->getBody());
92-
$product->ser_cen_id = $ser_cen_id;
93-
109+
$productModel->loadData($request->getBody());
110+
$productModel->ser_cen_id = $ser_cen_id;
94111
if (!empty($_FILES['media']['name'])) {
95-
$uploadDir = 'assets/uploads/';
96-
$fileName = uniqid() . '_' . basename($_FILES['media']['name']);
97-
$targetFile = $uploadDir . $fileName;
98-
99-
if (!move_uploaded_file($_FILES['media']['tmp_name'], $targetFile)) {
100-
Application::$app->session->setFlash('error', 'Failed to upload file.');
101-
Application::$app->response->redirect('/service-center-create-product');
102-
return;
103-
}
104-
105-
$product->media = $fileName;
112+
$productModel->media = $_FILES['media']['name'];
113+
move_uploaded_file($_FILES['media']['tmp_name'], 'assets/uploads/' . $productModel->media);
106114
}
107-
if ($product->editProduct()){
115+
if ($productModel->editProduct()){
108116
Application::$app->session->setFlash('success', 'Product updated successfully.');
109117
Application::$app->response->redirect('/service-center-create-product');
118+
return;
110119
}
111-
else {
112-
Application::$app->session->setFlash('error', 'Failed to update product.');
113-
}
114-
Application::$app->response->redirect('/service-center-create-product');
115120
}
121+
122+
}
123+
124+
public function delete(Request $request){
125+
$product_id = $request->getBody()['product_id'];
126+
$ser_cen_id = Application::$app->session->get('serviceCenter');
127+
128+
if ((new Product())->deleteProduct($product_id, $ser_cen_id)){
129+
Application::$app->session->setFlash('success', 'Product deleted successfully.');
130+
}
131+
else {
132+
Application::$app->session->setFlash('error', 'Failed to delete product.');
133+
}
134+
Application::$app->response->redirect('/service-center-create-product');
116135
}
117136

118137

models/Product.php

+45-14
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,10 @@
77
class Product extends DbModel
88
{
99
public int $ser_cen_id;
10+
public int $product_id;
1011
public string $description = '';
1112
public float $price;
12-
public string $media;
13+
public string $media = '';
1314
public ?string $created_at = null;
1415
public ?string $updated_at = null;
1516

@@ -85,29 +86,59 @@ public function getProductByServiceCenter(int $ser_cen_id): array
8586
}
8687
}
8788

89+
public function getProductByIdAndServiceCenter(int $ser_cen_id, int $product_id): ?array
90+
{
91+
$sql = 'SELECT * FROM product WHERE product_id = :product_id AND ser_cen_id = :ser_cen_id';
92+
$stmt = self::prepare($sql);
93+
$stmt->bindValue(':product_id', $product_id);
94+
$stmt->bindValue(':ser_cen_id', $ser_cen_id);
95+
$stmt->execute();
96+
97+
return $stmt->fetch(\PDO::FETCH_ASSOC) ?: null;
98+
}
99+
88100
public function editProduct(): bool
89101
{
90-
$sql = "UPDATE product SET description = :description, price = :price, media = :media, updated_at = NOW() WHERE product_id = :product_id AND ser_cen_id = :ser_cen_id";
102+
$sql = "
103+
UPDATE product
104+
SET description = :description, price = :price, media = CASE WHEN :media = '' THEN media ELSE :media END, updated_at = NOW()
105+
WHERE product_id = :product_id AND ser_cen_id = :ser_cen_id
106+
";
107+
91108
$stmt = self::prepare($sql);
92109
$stmt->bindValue(':description', $this->description);
93110
$stmt->bindValue(':price', $this->price);
94111
$stmt->bindValue(':media', $this->media);
95-
$stmt->bindValue("product_id", $this->product_id);
96-
$stmt->bindValue("ser_cen_id", $this->ser_cen_id);
97-
98-
return $stmt->execute();
112+
$stmt->bindValue(':product_id', $this->product_id);
113+
$stmt->bindValue(':ser_cen_id', $this->ser_cen_id);
114+
115+
// Debugging: Check SQL and parameters
116+
// var_dump([
117+
// 'SQL' => $sql,
118+
// 'description' => $this->description,
119+
// 'price' => $this->price,
120+
// 'media' => $this->media,
121+
// 'product_id' => $this->product_id,
122+
// 'ser_cen_id' => $this->ser_cen_id
123+
// ]);
124+
125+
$result = $stmt->execute();
126+
127+
// Debugging: Check execution result
128+
// var_dump($result);
129+
// die();
130+
131+
return $result;
99132
}
100133

134+
101135
public function deleteProduct(int $product_id, int $ser_cen_id): bool
102136
{
103-
$tableName = self::tableName();
104-
$statement = self::prepare("
105-
DELETE FROM $tableName
106-
WHERE product_id = :product_id AND ser_cen_id = :ser_cen_id
107-
");
108-
$statement->bindValue(':product_id', $product_id);
109-
$statement->bindValue(':seller_id', $ser_cen_id);
110-
return $statement->execute();
137+
$sql = 'DELETE FROM product WHERE product_id = :product_id AND ser_cen_id = :ser_cen_id';
138+
$stmt = self::prepare($sql);
139+
$stmt->bindValue(':product_id', $product_id);
140+
$stmt->bindValue(':ser_cen_id', $ser_cen_id);
141+
return $stmt->execute();
111142
}
112143
public function productRules(): array
113144
{
Loading
Loading
Loading
1.67 KB
Loading

public/index.php

+3
Original file line numberDiff line numberDiff line change
@@ -114,8 +114,11 @@
114114
$app->router->post('/service-center-create-product', [ProductController::class, 'create']);
115115
$app->router->get('/market-place-home', [ProductController::class, 'index']);
116116
$app->router->get('/service-center-create-product', [ProductController::class, 'filterProductsById']);
117+
$app->router->get('/service-center-update-product', [ProductController::class,'update']);
117118
$app->router->get('/service-center-update-product', [ServiceCentreController::class, 'update']);
118119
$app->router->post('/service-center-update-product', [ProductController::class, 'update']);
120+
$app->router->post('/service-center-delete-product', [ProductController::class, 'delete']);
121+
119122

120123

121124
/** Admin Routes */

views/service-centre/components/header.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121

2222
<h6 class="user-name">
2323
<?php
24-
$username = strtoupper(Application::$app->service_center->{'name'});
24+
$username = strtoupper(Application::$app->serviceCenter->{'name'});
2525
echo $username;
2626
?>
2727
</h6>

views/service-centre/update-product.php

+32-16
Original file line numberDiff line numberDiff line change
@@ -21,26 +21,42 @@
2121
include_once 'components/header.php';
2222
?>
2323

24-
<form action="/service-center-update-product" method="post" enctype="multipart/form-data">
25-
<input type="hidden" name="product_id" value="<?php echo htmlspecialchars($product['product_id']); ?>">
26-
27-
<div>
28-
<label>Description:</label>
29-
<input type="text" name="description" value="<?php echo htmlspecialchars($product['description']); ?>" required>
24+
<?php if (Application::$app->session->getFlash('success')): ?>
25+
<div class="alert alert-success">
26+
<?php echo Application::$app->session->getFlash('success') ?>
3027
</div>
28+
<?php endif; ?>
3129

32-
<div>
33-
<label>Price:</label>
34-
<input type="number" step="0.01" name="price" value="<?php echo htmlspecialchars($product['price']); ?>" required>
35-
</div>
30+
<form action="/service-center-update-product" name="" method="post" enctype="multipart/form-data">
3631

37-
<div>
38-
<label>Media:</label>
39-
<input type="file" name="media">
40-
<p>Current File: <?php echo htmlspecialchars($product['media']); ?></p>
41-
</div>
32+
<?php if (!empty($product)): ?>
33+
<!-- --><?php //foreach ($products as $product): ?>
34+
35+
<input type="hidden" name="product_id" value="<?php echo htmlspecialchars($product['product_id']); ?>">
36+
37+
<div>
38+
<label>Description:</label>
39+
<input type="text" name="description" value="<?php echo htmlspecialchars($product['description']); ?>">
40+
</div>
41+
42+
<div>
43+
<label>Price:</label>
44+
<input type="number" step="0.01" name="price" value="<?php echo htmlspecialchars($product['price']); ?>">
45+
46+
</div>
47+
<div>
48+
<label>Media:</label>
49+
<input type="file" name="media">
50+
<p>Current File: <?php echo htmlspecialchars($product['media']); ?></p>
51+
</div>
52+
<button type="submit">Update Product</button>
53+
54+
<!-- --><?php //endforeach; ?>
55+
56+
<?php else: ?>
57+
<p>No products have been added yet.</p>
58+
<?php endif; ?>
4259

43-
<button type="submit">Update Product</button>
4460
</form>
4561

4662

0 commit comments

Comments
 (0)