continuing to connect models and services
This commit is contained in:
@@ -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);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -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);
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user