Skip to content

Commit

Permalink
delete action added for elastic and mysql for product entity
Browse files Browse the repository at this point in the history
  • Loading branch information
hkulekci committed Oct 12, 2017
1 parent dc0425a commit 1a10c9a
Show file tree
Hide file tree
Showing 7 changed files with 64 additions and 5 deletions.
25 changes: 24 additions & 1 deletion routes/products.js
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,30 @@ router.post('/new', function(req, res, next) {
);
});

router.get('/:id/delete', function(req, res, next) {
var params = req.params;

waterfall(
[
function(waterfallCallback) {
productService.delete(params.id, function(err, result) {
//TODO: check error status
waterfallCallback(false);
});
},
function(waterfallCallback) {
productSearchService.delete(params.id, function(err, result) {
//TODO: check error status
waterfallCallback(false);
});
}
],
function(err, result) {
res.redirect('/product');
}
);
});

router.get('/:id/edit', function(req, res, next) {
var params = req.params;

Expand Down Expand Up @@ -198,7 +222,6 @@ router.post('/:id/edit', function(req, res, next) {
return;
}
);

});

module.exports = router;
1 change: 0 additions & 1 deletion routes/search.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@ router.get('/', function(req, res, next) {

}
}, function(err, results) {
console.log(results);
res.render('search', results);
});

Expand Down
12 changes: 12 additions & 0 deletions services/productSearchService.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,4 +47,16 @@ exports.insert = function(product, callback) {
if (error) { callback(true, error); return; }
callback(false, response);
});
};


exports.delete = function(productId, callback) {
db.delete({
index: 'products',
type: 'product',
id: productId,
}, function (error, response) {
if (error) { callback(true, error); return; }
callback(false, response);
});
};
20 changes: 20 additions & 0 deletions services/productService.js
Original file line number Diff line number Diff line change
Expand Up @@ -116,4 +116,24 @@ exports.update = function(sqlData, callback) {
callback(false, results.changedRows);
});
});
};




exports.delete = function(productId, callback) {
var sql = "DELETE FROM product_category WHERE product_id = ?;";
var params = [productId];
db.getConnection(function(err, connection) {
if(err) { console.log(err); callback(true); return; }
connection.query(sql, params, function(err, results, fields) {
if(err) { console.log(err); callback(true); return; }
var psql = "DELETE FROM products WHERE id = ?;";
var pparams = [productId];
connection.query(psql, pparams, function(err, results, fields) {
callback(false, results.changedRows);
});

});
});
};
2 changes: 1 addition & 1 deletion views/index.twig
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
<div class="row">
<div class="col-12">
<form method="post">
<input type="text" id="typeSearchInput" placeholder="Type for search ..."><span id="searchStatus">...</span>
<input type="text" id="typeSearchInput" placeholder="Type for search ..." autofocus><span id="searchStatus">...</span>
</form>
<div id="typeSearchResults">
</div>
Expand Down
4 changes: 3 additions & 1 deletion views/product.twig
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@

{% block body %}
<div class="float-right">
<small>(Quantity : {{product['quantity']}} | Price : {{product['price']}}) - [<a href="/product/{{product['id']}}/edit">Edit</a>]</small>
<small>(Quantity : {{product['quantity']}} | Price : {{product['price']}}) -
[<a href="/product/{{product['id']}}/edit">Edit</a> | <a href="/product/{{product['id']}}/delete">Delete</a>]
</small>
</div>
<h1>{{title}}</h1>
<div>
Expand Down
5 changes: 4 additions & 1 deletion views/products.twig
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,10 @@
<div>
<ul>
{% for product in products %}
<li><a href="/product/id/{{product['id']}}">{{product['name']}}</a> <small>(<a href="/product/{{product['id']}}/edit">Edit</a>)</small></li>
<li>
<a href="/product/id/{{product['id']}}">{{product['name']}}</a>
<small>(<a href="/product/{{product['id']}}/edit">Edit</a> | <a href="/product/{{product['id']}}/delete">Delete</a>)</small>
</li>
{% endfor %}
</ul>
</div>
Expand Down

0 comments on commit 1a10c9a

Please sign in to comment.