Skip to content

Instantly share code, notes, and snippets.

@bogordesaincom
Created November 12, 2022 15:25
Show Gist options
  • Select an option

  • Save bogordesaincom/312d2340a5b98b61de8e17f17b520616 to your computer and use it in GitHub Desktop.

Select an option

Save bogordesaincom/312d2340a5b98b61de8e17f17b520616 to your computer and use it in GitHub Desktop.
Controller Sample
const { Op } = require('sequelize');
const { Category, sequelize } = require('@models');
const { getPagination, getPagingData } = require('@utils/pagination');
const {
getSuccessIndex,
getError,
getSuccess,
getNotFound,
getTenantID,
} = require('@utils/responder');
const index = async (req, res) => {
const page = req.query.page || 0;
const size = req.query.size || 10;
const search = req.query.search || '';
const sort_by = req.query.sort_by || '';
const start_date = req.query.start_date || '';
const end_date = req.query.end_date || '';
const order_by = req.query.order_by || '';
const { limit, offset } = getPagination(page, size);
try {
const tenantID = await getTenantID(req);
let condition = {};
condition.tenant_id = { [Op.eq]: tenantID };
if (search) {
condition = {
[Op.or]: [{ name: { [Op.iLike]: `%${search}%` } }],
};
}
if (start_date) {
condition.created_at = {
[Op.between]: [new Date(start_date), new Date(end_date)],
};
}
const sort = sort_by ? [[sort_by, order_by]] : [['id', 'desc']];
const models = await Category.findAndCountAll({
order: sort,
attributes: ['id', 'name', 'created_at'],
limit,
offset,
where: condition,
});
const response = getPagingData(models, page, limit);
return getSuccessIndex(res, response);
} catch (error) {
return getError(res, error);
}
};
const store = async (req, res) => {
const trx = await sequelize.transaction();
const tenant_id = await getTenantID(req);
try {
const response = await Category.create({ ...req.body, tenant_id });
await trx.commit();
return getSuccess(res, response);
} catch (error) {
await trx.rollback();
return getError(res, error);
}
};
const show = async (req, res) => {
try {
const models = await Category.findByPk(req.params.id);
if (models) {
return getSuccess(res, models);
} else {
return getNotFound(res);
}
} catch (error) {
return getError(res, error);
}
};
const update = async (req, res) => {
const trx = await sequelize.transaction();
try {
const response = await Category.update(req.body, {
where: {
id: req.params.id,
},
});
await trx.commit();
return getSuccess(res, response);
} catch (error) {
await trx.rollback();
return getError(res, error);
}
};
const destroy = async (req, res) => {
const trx = await sequelize.transaction();
try {
const tenantID = await getTenantID(req);
const categories = await Category.findByPk(req.params.id);
if (tenantID == categories.tenant_id) {
const response = await Category.destroy({
where: { id: req.params.id },
});
await trx.commit();
return getSuccess(res, response);
} else {
await trx.rollback();
return getError(res);
}
} catch (error) {
await trx.rollback();
return getError(res, error);
}
};
module.exports = { index, store, show, update, destroy };
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment