continuing to connect models and services

This commit is contained in:
Mikayla Dobson
2022-09-26 15:49:36 -05:00
parent cff08d91d6
commit f4a7ced887
11 changed files with 108 additions and 33 deletions

View File

@@ -3,18 +3,51 @@ const pgp = require('pg-promise')({ capSQL: true });
module.exports = class CartProductModel {
async create(data) {
try {
const statement = pgp.helpers.insert(data, null, 'products_orders') + 'RETURNING *';
const result = await db.query(statement);
if (result.rows.length) return result.rows[0];
return null;
} catch(e) {
throw new Error(e);
}
}
async find(cartid) {
try {
const statement = "SELECT * FROM products_orders WHERE cartid = $1";
const values = [cartid];
const result = await db.query(statement, values);
if (result.rows.length) return result.rows[0];
return [];
} catch(e) {
throw new Error(e);
}
}
async update(data) {
const { id } = data;
try {
const condition = pgp.as.format("WHERE id = $1", [id]);
const statement = pgp.helpers.update(data, null, 'products_orders') + condition;
const result = await db.query(statement);
if (result.rows.length) return result.rows[0];
return null;
} catch(e) {
throw new Error(e);
}
}
async delete(productid) {
try {
const statement = "DELETE FROM products_orders WHERE id = $1 RETURNING *";
const values = [productid];
const result = await db.query(statement, values);
if (result.rows.length) return result.rows[0];
return null;
} catch(e) {
throw new Error(e);
}
}
}

View File

@@ -1,17 +1,44 @@
module.exports = class OrderModel {
async create() {
const db = require('../db/Pool');
const pgp = require('pg-promise')({ capSQL: true });
/**
* TODO: conceptualize and implement order lifecycle, from not submitted,
* to submitted/not shipped, to shipped and pending, to delivered?
**/
module.exports = class OrderModel {
async create(userid) {
try {
const statement = pgp.helpers.insert(userid, null, 'orders') + 'RETURNING *';
const result = await db.query(statement);
if (result.rows.length) return result.rows[0];
return null;
} catch(e) {
throw new Error(e);
}
}
async update(data) {
try {
} catch(e) {
throw new Error(e);
}
}
async findByUser(userid) {
try {
} catch(e) {
throw new Error(e);
}
}
async findByOrderId(orderid) {
try {
} catch(e) {
throw new Error(e);
}
}
}