more detail into models and services
This commit is contained in:
@@ -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
17
models/CartProduct.js
Normal file
@@ -0,0 +1,17 @@
|
||||
module.exports = class CartProductModel {
|
||||
async insert(data) {
|
||||
|
||||
}
|
||||
|
||||
async update(data) {
|
||||
|
||||
}
|
||||
|
||||
async find(cartid) {
|
||||
|
||||
}
|
||||
|
||||
async delete(productid) {
|
||||
|
||||
}
|
||||
}
|
||||
11
models/OrderProduct.js
Normal file
11
models/OrderProduct.js
Normal file
@@ -0,0 +1,11 @@
|
||||
module.exports = class OrderProductModel {
|
||||
async create(data) {
|
||||
|
||||
}
|
||||
|
||||
async find(orderid) {
|
||||
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
@@ -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);
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user