This commit is contained in:
Local Server
2025-12-19 20:44:46 -06:00
parent 701f799cde
commit e4b3de4a46
113 changed files with 16673 additions and 2174 deletions

View File

@@ -0,0 +1,45 @@
const { query } = require("../config/database");
const buildSelectQuery = (
table,
conditions = [],
orderBy = "createdat DESC"
) => {
const whereClause =
conditions.length > 0 ? `WHERE ${conditions.join(" AND ")}` : "";
return `SELECT * FROM ${table} ${whereClause} ORDER BY ${orderBy}`;
};
const getById = async (table, id) => {
const result = await query(`SELECT * FROM ${table} WHERE id = $1`, [id]);
return result.rows[0] || null;
};
const getAllActive = async (table, orderBy = "createdat DESC") => {
const result = await query(
`SELECT * FROM ${table} WHERE isactive = true ORDER BY ${orderBy}`
);
return result.rows;
};
const deleteById = async (table, id) => {
const result = await query(
`DELETE FROM ${table} WHERE id = $1 RETURNING id`,
[id]
);
return result.rowCount > 0;
};
const countRecords = async (table, condition = "") => {
const whereClause = condition ? `WHERE ${condition}` : "";
const result = await query(`SELECT COUNT(*) FROM ${table} ${whereClause}`);
return parseInt(result.rows[0].count);
};
module.exports = {
buildSelectQuery,
getById,
getAllActive,
deleteById,
countRecords,
};

View File

@@ -0,0 +1,48 @@
const { HTTP_STATUS } = require("../config/constants");
const sendSuccess = (res, data = {}, statusCode = HTTP_STATUS.OK) => {
res.status(statusCode).json({
success: true,
...data,
});
};
const sendError = (
res,
message = "Server error",
statusCode = HTTP_STATUS.INTERNAL_ERROR
) => {
res.status(statusCode).json({
success: false,
message,
});
};
const sendNotFound = (res, resource = "Resource") => {
res.status(HTTP_STATUS.NOT_FOUND).json({
success: false,
message: `${resource} not found`,
});
};
const sendUnauthorized = (res, message = "Authentication required") => {
res.status(HTTP_STATUS.UNAUTHORIZED).json({
success: false,
message,
});
};
const sendForbidden = (res, message = "Access denied") => {
res.status(HTTP_STATUS.FORBIDDEN).json({
success: false,
message,
});
};
module.exports = {
sendSuccess,
sendError,
sendNotFound,
sendUnauthorized,
sendForbidden,
};