Fixing more API endpoints
parent
b3b1a3ae00
commit
d57fd17718
|
@ -140,6 +140,20 @@ $(document).ready(function (){
|
|||
});
|
||||
});
|
||||
|
||||
$('.btn-delete-product').on('click', function(){
|
||||
$.ajax({
|
||||
method: 'POST',
|
||||
url: '/admin/product/delete',
|
||||
data: { productId: $(this).attr('data-id') }
|
||||
})
|
||||
.done(function(msg){
|
||||
showNotification(msg.message, 'success', true);
|
||||
})
|
||||
.fail(function(msg){
|
||||
showNotification(msg.responseJSON.message, 'danger');
|
||||
});
|
||||
});
|
||||
|
||||
// Call to API to check if a permalink is available
|
||||
$(document).on('click', '#validate_permalink', function(e){
|
||||
if($('#productPermalink').val() !== ''){
|
||||
|
|
File diff suppressed because one or more lines are too long
|
@ -15,7 +15,8 @@ router.get('/admin/products', restrict, async (req, res, next) => {
|
|||
const topResults = await db.products.find({}).sort({ productAddedDate: -1 }).limit(10).toArray();
|
||||
res.render('products', {
|
||||
title: 'Cart',
|
||||
top_results: topResults,
|
||||
results: topResults,
|
||||
resultType: 'top',
|
||||
session: req.session,
|
||||
admin: true,
|
||||
config: req.app.config,
|
||||
|
@ -46,6 +47,7 @@ router.get('/admin/products/filter/:search', restrict, async (req, res, next) =>
|
|||
res.render('products', {
|
||||
title: 'Results',
|
||||
results: results,
|
||||
resultType: 'filtered',
|
||||
admin: true,
|
||||
config: req.app.config,
|
||||
session: req.session,
|
||||
|
@ -148,7 +150,7 @@ router.post('/admin/product/insert', restrict, checkAccess, async (req, res) =>
|
|||
|
||||
// If API request, return json
|
||||
if(req.apiAuthenticated){
|
||||
res.status(400).json({ error: 'Permalink already exists. Pick a new one.' });
|
||||
res.status(400).json({ message: 'Permalink already exists. Pick a new one.' });
|
||||
return;
|
||||
}
|
||||
|
||||
|
@ -195,7 +197,7 @@ router.post('/admin/product/insert', restrict, checkAccess, async (req, res) =>
|
|||
|
||||
// If API request, return json
|
||||
if(req.apiAuthenticated){
|
||||
res.status(400).json({ error: 'Error inserting document' });
|
||||
res.status(400).json({ message: 'Error inserting document' });
|
||||
return;
|
||||
}
|
||||
|
||||
|
@ -283,7 +285,7 @@ router.post('/admin/product/update', restrict, checkAccess, async (req, res) =>
|
|||
|
||||
// If API request, return json
|
||||
if(req.apiAuthenticated){
|
||||
res.status(400).json({ messge: 'Failed to update product' });
|
||||
res.status(400).json({ message: 'Failed to update product' });
|
||||
return;
|
||||
}
|
||||
|
||||
|
@ -294,7 +296,7 @@ router.post('/admin/product/update', restrict, checkAccess, async (req, res) =>
|
|||
if(count > 0 && req.body.productPermalink !== ''){
|
||||
// If API request, return json
|
||||
if(req.apiAuthenticated){
|
||||
res.status(400).json({ messge: 'Permalink already exists. Pick a new one' });
|
||||
res.status(400).json({ message: 'Permalink already exists. Pick a new one.' });
|
||||
return;
|
||||
}
|
||||
|
||||
|
@ -399,7 +401,7 @@ router.post('/admin/product/update', restrict, checkAccess, async (req, res) =>
|
|||
}catch(ex){
|
||||
// If API request, return json
|
||||
if(req.apiAuthenticated){
|
||||
res.status(400).json({ messge: 'Failed to save. Please try again' });
|
||||
res.status(400).json({ message: 'Failed to save. Please try again' });
|
||||
return;
|
||||
}
|
||||
|
||||
|
@ -411,25 +413,23 @@ router.post('/admin/product/update', restrict, checkAccess, async (req, res) =>
|
|||
});
|
||||
|
||||
// delete product
|
||||
router.get('/admin/product/delete/:id', restrict, checkAccess, async (req, res) => {
|
||||
router.post('/admin/product/delete', restrict, checkAccess, async (req, res) => {
|
||||
const db = req.app.db;
|
||||
|
||||
// remove the product
|
||||
await db.products.deleteOne({ _id: common.getId(req.params.id) }, {});
|
||||
await db.products.deleteOne({ _id: common.getId(req.body.productId) }, {});
|
||||
|
||||
// delete any images and folder
|
||||
rimraf('public/uploads/' + req.params.id, (err) => {
|
||||
rimraf('public/uploads/' + req.body.productId, (err) => {
|
||||
if(err){
|
||||
console.info(err.stack);
|
||||
res.status(400).json({ message: 'Failed to delete product' });
|
||||
}
|
||||
|
||||
// re-index products
|
||||
indexProducts(req.app)
|
||||
.then(() => {
|
||||
// redirect home
|
||||
req.session.message = 'Product successfully deleted';
|
||||
req.session.messageType = 'success';
|
||||
res.redirect('/admin/products');
|
||||
res.status(200).json({ message: 'Product successfully deleted' });
|
||||
});
|
||||
});
|
||||
});
|
||||
|
@ -440,10 +440,10 @@ router.post('/admin/product/published_state', restrict, checkAccess, async (req,
|
|||
|
||||
try{
|
||||
await db.products.updateOne({ _id: common.getId(req.body.id) }, { $set: { productPublished: common.convertBool(req.body.state) } }, { multi: false });
|
||||
res.status(200).json('Published state updated');
|
||||
res.status(200).json({ message: 'Published state updated' });
|
||||
}catch(ex){
|
||||
console.error(colors.red('Failed to update the published state: ' + ex));
|
||||
res.status(400).json('Published state not updated');
|
||||
res.status(400).json({ message: 'Published state not updated' });
|
||||
}
|
||||
});
|
||||
|
||||
|
|
|
@ -13,37 +13,23 @@
|
|||
</div>
|
||||
<p class="text-warning top-pad-10">{{ @root.__ "Products can be filtered by: product title or product description keywords" }}</p>
|
||||
</div>
|
||||
{{#if results}}
|
||||
<div class="col-lg-12">
|
||||
<ul class="list-group">
|
||||
<div class="col-lg-12">
|
||||
<ul class="list-group">
|
||||
<li class="list-group-item">
|
||||
<span class="pull-right"><strong>{{ @root.__ "Published" }}</strong></span>
|
||||
{{#ifCond resultType '==' 'filtered'}}
|
||||
<strong>{{ @root.__ "Products" }} - <span class="text-danger">{{ @root.__ "Filtered term" }}: {{searchTerm}} </span></strong>
|
||||
{{else}}
|
||||
<strong>{{ @root.__ "Recent products" }}</strong>
|
||||
{{/ifCond}}
|
||||
</li>
|
||||
{{#each results}}
|
||||
<li class="list-group-item">
|
||||
<span class="pull-right"><strong>{{ @root.__ "Published" }}</strong></span>
|
||||
<strong>{{ @root.__ "Products" }} - <span class="text-danger">{{ @root.__ "Filtered term" }}: {{searchTerm}} </span></strong>
|
||||
<button class="pull-right btn text-danger btn-delete-product" data-id="{{this._id}}"> <i class="fa fa-trash-o"></i></button>
|
||||
<h4 class="pull-right"><input id="{{this._id}}" class="published_state" type="checkbox" {{checkedState this.productPublished}}></h4>
|
||||
<h5><a href="/admin/product/edit/{{this._id}}">{{this.productTitle}}</a></h5>
|
||||
</li>
|
||||
{{#each results}}
|
||||
<li class="list-group-item">
|
||||
<h4 class="pull-right text-danger" style="padding-left: 10px;"><a class="text-danger" href="/admin/product/delete/{{this._id}}" onclick="return confirm('Are you sure you want to delete this product?');"> <i class="fa fa-trash-o"></i></a></h4>
|
||||
<h4 class="pull-right"><input id="{{this._id}}" class="published_state" type="checkbox" {{checkedState this.productPublished}}></h4>
|
||||
<h5><a href="/admin/product/edit/{{this._id}}">{{this.productTitle}}</a></h5>
|
||||
</li>
|
||||
{{/each}}
|
||||
</ul>
|
||||
</div>
|
||||
{{else}}
|
||||
<div class="col-lg-12">
|
||||
<ul class="list-group">
|
||||
<li class="list-group-item">
|
||||
<span class="pull-right"><strong>{{ @root.__ "Published" }}</strong></span>
|
||||
<strong>{{ @root.__ "Recent products" }}</strong>
|
||||
</li>
|
||||
{{#each top_results}}
|
||||
<li class="list-group-item">
|
||||
<h4 class="pull-right" style="padding-left: 10px;"><a class="text-danger" href="/admin/product/delete/{{this._id}}" onclick="return confirm('Are you sure you want to delete this product?');"> <i class="fa fa-trash-o"></i></a></h4>
|
||||
<h4 class="pull-right"><input id="{{this._id}}" class="published_state" type="checkbox" {{checkedState this.productPublished}}></h4>
|
||||
<h5><a href="/admin/product/edit/{{this._id}}">{{this.productTitle}}</a></h5>
|
||||
</li>
|
||||
{{/each}}
|
||||
</ul>
|
||||
</div>
|
||||
{{/if}}
|
||||
{{/each}}
|
||||
</ul>
|
||||
</div>
|
||||
</div>
|
Loading…
Reference in New Issue