Config With Seacrh

This commit is contained in:
elforjani3
2020-04-11 15:00:22 +02:00
parent e8079dbde3
commit 940418c4e0
12 changed files with 284 additions and 149 deletions

View File

@@ -15,30 +15,40 @@ export default {
router.use(JWTAuth);
router.post('/:id',
router.post(
'/:id',
// permit('create', 'edit'),
this.editCategory.validation,
asyncMiddleware(this.editCategory.handler));
asyncMiddleware(this.editCategory.handler)
);
router.post('/',
router.post(
'/',
// permit('create'),
this.newCategory.validation,
asyncMiddleware(this.newCategory.handler));
asyncMiddleware(this.newCategory.handler)
);
router.delete('/:id',
router.delete(
'/:id',
// permit('create', 'edit', 'delete'),
this.deleteItem.validation,
asyncMiddleware(this.deleteItem.handler));
asyncMiddleware(this.deleteItem.handler)
);
router.get('/:id',
router.get(
'/:id',
// permit('view'),
this.getCategory.validation,
asyncMiddleware(this.getCategory.handler));
asyncMiddleware(this.getCategory.handler)
);
router.get('/',
router.get(
'/',
// permit('view'),
this.getList.validation,
asyncMiddleware(this.getList.handler));
asyncMiddleware(this.getList.handler)
);
return router;
},
@@ -48,16 +58,26 @@ export default {
*/
newCategory: {
validation: [
check('name').exists().trim().escape(),
check('parent_category_id').optional().isNumeric().toInt(),
check('description').optional().trim().escape(),
check('name')
.exists()
.trim()
.escape(),
check('parent_category_id')
.optional({ nullable: true, checkFalsy: true })
.isNumeric()
.toInt(),
check('description')
.optional()
.trim()
.escape()
],
async handler(req, res) {
const validationErrors = validationResult(req);
if (!validationErrors.isEmpty()) {
return res.boom.badData(null, {
code: 'validation_error', ...validationErrors,
code: 'validation_error',
...validationErrors
});
}
@@ -66,20 +86,21 @@ export default {
if (form.parent_category_id) {
const foundParentCategory = await ItemCategory.query()
.where('id', form.parent_category_id).first();
.where('id', form.parent_category_id)
.first();
if (!foundParentCategory) {
return res.boom.notFound('The parent category ID is not found.', {
errors: [{ type: 'PARENT_CATEGORY_NOT_FOUND', code: 100 }],
errors: [{ type: 'PARENT_CATEGORY_NOT_FOUND', code: 100 }]
});
}
}
const category = await ItemCategory.query().insert({
...form,
user_id: user.id,
user_id: user.id
});
return res.status(200).send({ category });
},
}
},
/**
@@ -88,9 +109,18 @@ export default {
editCategory: {
validation: [
param('id').toInt(),
check('name').exists().trim().escape(),
check('parent_category_id').optional().isNumeric().toInt(),
check('description').optional().trim().escape(),
check('name')
.exists()
.trim()
.escape(),
check('parent_category_id')
.optional({ nullable: true, checkFalsy: true })
.isNumeric()
.toInt(),
check('description')
.optional()
.trim()
.escape()
],
async handler(req, res) {
const { id } = req.params;
@@ -98,33 +128,41 @@ export default {
if (!validationErrors.isEmpty()) {
return res.boom.badData(null, {
code: 'validation_error', ...validationErrors,
code: 'validation_error',
...validationErrors
});
}
const form = { ...req.body };
const itemCategory = await ItemCategory.query().where('id', id).first()
const itemCategory = await ItemCategory.query()
.where('id', id)
.first();
if (!itemCategory) {
return res.boom.notFound({
errors: [{ type: 'ITEM_CATEGORY.NOT.FOUND', code: 100 }],
errors: [{ type: 'ITEM_CATEGORY.NOT.FOUND', code: 100 }]
});
}
if (form.parent_category_id
&& form.parent_category_id !== itemCategory.parent_category_id) {
if (
form.parent_category_id &&
form.parent_category_id !== itemCategory.parent_category_id
) {
const foundParentCategory = await ItemCategory.query()
.where('id', form.parent_category_id).first();
.where('id', form.parent_category_id)
.first();
if (!foundParentCategory) {
return res.boom.notFound('The parent category ID is not found.', {
errors: [{ type: 'PARENT_CATEGORY_NOT_FOUND', code: 100 }],
errors: [{ type: 'PARENT_CATEGORY_NOT_FOUND', code: 100 }]
});
}
}
const updateItemCategory = await ItemCategory.query().where('id', id).update({ ...form });
const updateItemCategory = await ItemCategory.query()
.where('id', id)
.update({ ...form });
return res.status(200).send({ id: updateItemCategory });
},
}
},
/**
@@ -132,20 +170,26 @@ export default {
*/
deleteItem: {
validation: [
param('id').exists().toInt(),
param('id')
.exists()
.toInt()
],
async handler(req, res) {
const { id } = req.params;
const itemCategory = await ItemCategory.query().where('id', id).first();
const itemCategory = await ItemCategory.query()
.where('id', id)
.first();
if (!itemCategory) {
return res.boom.notFound();
}
await ItemCategory.query().where('id', itemCategory.id).delete();
await ItemCategory.query()
.where('id', itemCategory.id)
.delete();
return res.status(200).send();
},
}
},
/**
@@ -157,27 +201,25 @@ export default {
const categories = await ItemCategory.query();
return res.status(200).send({ categories });
},
}
},
/**
* Retrieve details of the given category.
*/
getCategory: {
validation: [
param('category_id').toInt(),
],
validation: [param('category_id').toInt()],
async handler(req, res) {
const { category_id: categoryId } = req.params;
const item = await ItemCategory.where('id', categoryId).fetch();
if (!item) {
return res.boom.notFound(null, {
errors: [{ type: 'CATEGORY_NOT_FOUND', code: 100 }],
errors: [{ type: 'CATEGORY_NOT_FOUND', code: 100 }]
});
}
return res.status(200).send({ category: item.toJSON() });
},
},
}
}
};