Skip to content

Commit 1a10c9a

Browse files
committed
delete action added for elastic and mysql for product entity
1 parent dc0425a commit 1a10c9a

File tree

7 files changed

+64
-5
lines changed

7 files changed

+64
-5
lines changed

routes/products.js

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,30 @@ router.post('/new', function(req, res, next) {
9292
);
9393
});
9494

95+
router.get('/:id/delete', function(req, res, next) {
96+
var params = req.params;
97+
98+
waterfall(
99+
[
100+
function(waterfallCallback) {
101+
productService.delete(params.id, function(err, result) {
102+
//TODO: check error status
103+
waterfallCallback(false);
104+
});
105+
},
106+
function(waterfallCallback) {
107+
productSearchService.delete(params.id, function(err, result) {
108+
//TODO: check error status
109+
waterfallCallback(false);
110+
});
111+
}
112+
],
113+
function(err, result) {
114+
res.redirect('/product');
115+
}
116+
);
117+
});
118+
95119
router.get('/:id/edit', function(req, res, next) {
96120
var params = req.params;
97121

@@ -198,7 +222,6 @@ router.post('/:id/edit', function(req, res, next) {
198222
return;
199223
}
200224
);
201-
202225
});
203226

204227
module.exports = router;

routes/search.js

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,6 @@ router.get('/', function(req, res, next) {
3131

3232
}
3333
}, function(err, results) {
34-
console.log(results);
3534
res.render('search', results);
3635
});
3736

services/productSearchService.js

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,4 +47,16 @@ exports.insert = function(product, callback) {
4747
if (error) { callback(true, error); return; }
4848
callback(false, response);
4949
});
50+
};
51+
52+
53+
exports.delete = function(productId, callback) {
54+
db.delete({
55+
index: 'products',
56+
type: 'product',
57+
id: productId,
58+
}, function (error, response) {
59+
if (error) { callback(true, error); return; }
60+
callback(false, response);
61+
});
5062
};

services/productService.js

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -116,4 +116,24 @@ exports.update = function(sqlData, callback) {
116116
callback(false, results.changedRows);
117117
});
118118
});
119+
};
120+
121+
122+
123+
124+
exports.delete = function(productId, callback) {
125+
var sql = "DELETE FROM product_category WHERE product_id = ?;";
126+
var params = [productId];
127+
db.getConnection(function(err, connection) {
128+
if(err) { console.log(err); callback(true); return; }
129+
connection.query(sql, params, function(err, results, fields) {
130+
if(err) { console.log(err); callback(true); return; }
131+
var psql = "DELETE FROM products WHERE id = ?;";
132+
var pparams = [productId];
133+
connection.query(psql, pparams, function(err, results, fields) {
134+
callback(false, results.changedRows);
135+
});
136+
137+
});
138+
});
119139
};

views/index.twig

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
<div class="row">
55
<div class="col-12">
66
<form method="post">
7-
<input type="text" id="typeSearchInput" placeholder="Type for search ..."><span id="searchStatus">...</span>
7+
<input type="text" id="typeSearchInput" placeholder="Type for search ..." autofocus><span id="searchStatus">...</span>
88
</form>
99
<div id="typeSearchResults">
1010
</div>

views/product.twig

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,9 @@
22

33
{% block body %}
44
<div class="float-right">
5-
<small>(Quantity : {{product['quantity']}} | Price : {{product['price']}}) - [<a href="/product/{{product['id']}}/edit">Edit</a>]</small>
5+
<small>(Quantity : {{product['quantity']}} | Price : {{product['price']}}) -
6+
[<a href="/product/{{product['id']}}/edit">Edit</a> | <a href="/product/{{product['id']}}/delete">Delete</a>]
7+
</small>
68
</div>
79
<h1>{{title}}</h1>
810
<div>

views/products.twig

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,10 @@
66
<div>
77
<ul>
88
{% for product in products %}
9-
<li><a href="/product/id/{{product['id']}}">{{product['name']}}</a> <small>(<a href="/product/{{product['id']}}/edit">Edit</a>)</small></li>
9+
<li>
10+
<a href="/product/id/{{product['id']}}">{{product['name']}}</a>
11+
<small>(<a href="/product/{{product['id']}}/edit">Edit</a> | <a href="/product/{{product['id']}}/delete">Delete</a>)</small>
12+
</li>
1013
{% endfor %}
1114
</ul>
1215
</div>

0 commit comments

Comments
 (0)