more detail into models and services

This commit is contained in:
Mikayla Dobson
2022-09-23 12:51:51 -05:00
parent c9c1de3521
commit 0b09152de4
8 changed files with 112 additions and 6 deletions

View File

View File

@@ -1,13 +1,39 @@
const db = require('../db/Pool');
const pgp = require('pg-promise')({ capSQL: true });
module.exports = class CartModel {
async create(userid) {
try {
const statement = pgp.helpers.insert(userid, null, 'carts') + 'RETURNING *';
const result = await db.query(statement);
if (result.rows.length) return result.rows[0];
return null;
} catch(e) {
throw new Error(e);
}
}
async findOneByUserId(userid) {
try {
const statement = `SELECT * FROM carts WHERE userid = $1`;
const filter = [userid];
const result = await db.query(statement, filter);
if (result.rows.length) return result.rows[0];
return null;
} catch(e) {
throw new Error(e);
}
}
async findOneByCartId(cartid) {
try {
const statement = `SELECT * FROM carts WHERE id = $1`;
const filter = [cartid];
const result = await db.query(statement, filter);
if (result.rows.length) return result.rows[0];
return null;
} catch(e) {
throw new Error(e);
}
}
}

17
models/CartProduct.js Normal file
View File

@@ -0,0 +1,17 @@
module.exports = class CartProductModel {
async insert(data) {
}
async update(data) {
}
async find(cartid) {
}
async delete(productid) {
}
}

View File

11
models/OrderProduct.js Normal file
View File

@@ -0,0 +1,11 @@
module.exports = class OrderProductModel {
async create(data) {
}
async find(orderid) {
}
}

View File

@@ -1,9 +1,26 @@
module.exports = class ProductModel {
async find() {
const db = require('../db/Pool');
module.exports = class ProductModel {
async selectAll() {
try {
const q = "SELECT * FROM product;";
const result = await db.query(q);
if (result.rows.length) return result.rows;
return [];
} catch(e) {
throw new Error(e);
}
}
async findOne(productid) {
try {
const q = `SELECT * FROM product WHERE id = $1;`;
const filter = [productid];
const result = await db.query(q, filter);
if (result.rows.length) return result.rows[0];
return null;
} catch(e) {
throw new Error(e);
}
}
}