implements a category route

This commit is contained in:
Mikayla Dobson
2022-09-27 17:08:59 -05:00
parent 388f3a5ba9
commit d99cf5bb8a
4 changed files with 130 additions and 0 deletions

50
models/CategoryModel.js Normal file
View File

@@ -0,0 +1,50 @@
const db = require('../db/Pool');
const pgp = require('pg-promise')({ capSQL: true });
module.exports = class CategoryModel {
async getAll() {
try {
const statement = "SELECT * FROM category";
const result = await db.query(statement);
if (result.rows.length) return result.rows;
return null;
} catch(e) {
throw new Error(e);
}
}
async getById(id) {
try {
const statement = "SELECT * FROM category WHERE id = $1";
const values = [id];
const result = await db.query(statement, values);
if (result.rows.length) return result.rows;
return null;
} catch(e) {
throw new Error(e);
}
}
async getByName(name) {
try {
const statement = "SELECT * FROM category WHERE name = $1";
const values = [name];
const result = await db.query(statement, values);
if (result.rows.length) return result.rows;
return null;
} catch(e) {
throw new Error(e);
}
}
async insertOne(data) {
try {
const statement = pgp.helpers.insert(data, null, 'category') + "RETURNING *";
const result = await db.query(statement);
if (result.rows.length) return result.rows;
return null;
} catch(e) {
throw new Error(e);
}
}
}