Restore Random_Increase helper
This commit is contained in:
@@ -238,9 +238,12 @@ export default class DBAdapter {
|
|||||||
'SELECT quantity FROM products WHERE id = ?', [productId]
|
'SELECT quantity FROM products WHERE id = ?', [productId]
|
||||||
);
|
);
|
||||||
|
|
||||||
// TODO
|
const productRow = product?.[0];
|
||||||
if (!product || product.quantity < quantity){
|
if (!productRow || productRow.quantity < quantity){
|
||||||
return Promise.reject();
|
return Promise.reject({
|
||||||
|
type: DB_USER_ERROR,
|
||||||
|
error: new Error("Insufficient product quantity")
|
||||||
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
const result = await this.#dbClient.query(
|
const result = await this.#dbClient.query(
|
||||||
@@ -249,7 +252,7 @@ export default class DBAdapter {
|
|||||||
);
|
);
|
||||||
|
|
||||||
await this.#dbClient.query(
|
await this.#dbClient.query(
|
||||||
'UPDATE products SET quantity = quantity - ? WHERE id = >',
|
'UPDATE products SET quantity = quantity - ? WHERE id = ?',
|
||||||
[quantity, productId]
|
[quantity, productId]
|
||||||
);
|
);
|
||||||
|
|
||||||
@@ -267,37 +270,44 @@ export default class DBAdapter {
|
|||||||
'SELECT product_id, quantity FROM order_items WHERE id = ?', [itemId]
|
'SELECT product_id, quantity FROM order_items WHERE id = ?', [itemId]
|
||||||
);
|
);
|
||||||
|
|
||||||
// TODO
|
const itemRow = item?.[0];
|
||||||
if (!item){
|
if (!itemRow){
|
||||||
return Promise.reject();
|
return Promise.reject({
|
||||||
|
type: DB_USER_ERROR,
|
||||||
|
error: new Error("Order item not found")
|
||||||
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
const diff = quantity - item.quantity;
|
const diff = quantity - itemRow.quantity;
|
||||||
|
|
||||||
if (diff > 0){
|
if (diff > 0){
|
||||||
const product = await this.#dbClient.query(
|
const product = await this.#dbClient.query(
|
||||||
'SELECT quantity FROM products WHERE id = ?', [item.productId]
|
'SELECT quantity FROM products WHERE id = ?', [itemRow.product_id]
|
||||||
);
|
);
|
||||||
|
|
||||||
if (product.quantity < diff){
|
const productRow = product?.[0];
|
||||||
return Promise.reject();
|
if (!productRow || productRow.quantity < diff){
|
||||||
|
return Promise.reject({
|
||||||
|
type: DB_USER_ERROR,
|
||||||
|
error: new Error("Insufficient product quantity")
|
||||||
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
await this.#dbClient.query(
|
await this.#dbClient.query(
|
||||||
'UPDATE products SET quantity = quantity - ? WHERE id = >',
|
'UPDATE products SET quantity = quantity - ? WHERE id = ?',
|
||||||
[diff, item.productId]
|
[diff, itemRow.product_id]
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (diff < 0) {
|
if (diff < 0) {
|
||||||
await this.#dbClient.query(
|
await this.#dbClient.query(
|
||||||
'UPDATE products SET quantity = quantity - ? WHERE id = >',
|
'UPDATE products SET quantity = quantity + ? WHERE id = ?',
|
||||||
[-diff, item.productId]
|
[-diff, itemRow.product_id]
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
await this.#dbClient.query(
|
await this.#dbClient.query(
|
||||||
'UPDATE order_items SET quantity = ? WHERE id = >',
|
'UPDATE order_items SET quantity = ? WHERE id = ?',
|
||||||
[quantity, itemId]
|
[quantity, itemId]
|
||||||
);
|
);
|
||||||
|
|
||||||
@@ -315,14 +325,17 @@ export default class DBAdapter {
|
|||||||
'SELECT product_id, quantity FROM order_items WHERE id = ?', [itemId]
|
'SELECT product_id, quantity FROM order_items WHERE id = ?', [itemId]
|
||||||
);
|
);
|
||||||
|
|
||||||
// TODO
|
const itemRow = item?.[0];
|
||||||
if (!item){
|
if (!itemRow){
|
||||||
return Promise.reject();
|
return Promise.reject({
|
||||||
|
type: DB_USER_ERROR,
|
||||||
|
error: new Error("Order item not found")
|
||||||
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
await this.#dbClient.query(
|
await this.#dbClient.query(
|
||||||
'UPDATE products SET quantity = quantity + ? WHERE id = >',
|
'UPDATE products SET quantity = quantity + ? WHERE id = ?',
|
||||||
[item.quantity, item.productId]
|
[itemRow.quantity, itemRow.product_id]
|
||||||
);
|
);
|
||||||
|
|
||||||
await this.#dbClient.query(
|
await this.#dbClient.query(
|
||||||
@@ -344,13 +357,16 @@ export default class DBAdapter {
|
|||||||
'SELECT id, order_id FROM order_items WHERE id = ?', [itemId]
|
'SELECT id, order_id FROM order_items WHERE id = ?', [itemId]
|
||||||
);
|
);
|
||||||
|
|
||||||
// TODO
|
const itemRow = item?.[0];
|
||||||
if (!item){
|
if (!itemRow){
|
||||||
return Promise.reject();
|
return Promise.reject({
|
||||||
|
type: DB_USER_ERROR,
|
||||||
|
error: new Error("Order item not found")
|
||||||
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
await this.#dbClient.query(
|
await this.#dbClient.query(
|
||||||
'UPDATE order_items SET order_id = ? WHERE id = >',
|
'UPDATE order_items SET order_id = ? WHERE id = ?',
|
||||||
[targetOrderId, itemId]
|
[targetOrderId, itemId]
|
||||||
);
|
);
|
||||||
|
|
||||||
|
|||||||
@@ -16,6 +16,8 @@ const {
|
|||||||
PASSWORD
|
PASSWORD
|
||||||
} = process.env;
|
} = process.env;
|
||||||
|
|
||||||
|
const appPort = Number(PORT) || 3000;
|
||||||
|
|
||||||
const app = express();
|
const app = express();
|
||||||
app.use(express.json());
|
app.use(express.json());
|
||||||
|
|
||||||
@@ -240,9 +242,7 @@ app.put('/api/orders/:orderId/items/:itemId', async (req, res) => {
|
|||||||
app.delete('/api/orders/:orderId/items/:itemId', async (req, res) => {
|
app.delete('/api/orders/:orderId/items/:itemId', async (req, res) => {
|
||||||
try {
|
try {
|
||||||
await adapter.deleteOrderItem(req.params.itemId);
|
await adapter.deleteOrderItem(req.params.itemId);
|
||||||
res.statusCode = 204;
|
res.status(204).end();
|
||||||
res.message = "OK";
|
|
||||||
res.json({message: "success"});
|
|
||||||
} catch (err){
|
} catch (err){
|
||||||
res.statusCode = 500;
|
res.statusCode = 500;
|
||||||
res.message = "WHOOPS";
|
res.message = "WHOOPS";
|
||||||
@@ -283,11 +283,11 @@ app.use((req, res) => {
|
|||||||
res.status(404).json({ error: 'Invalid route' });
|
res.status(404).json({ error: 'Invalid route' });
|
||||||
});
|
});
|
||||||
|
|
||||||
app.listen(3000, async() => {
|
const server = app.listen(appPort, async() => {
|
||||||
try {
|
try {
|
||||||
await adapter.connect();
|
await adapter.connect();
|
||||||
console.log(`✅ Server running on port 3000`);
|
console.log(`✅ Server running on port ${appPort}`);
|
||||||
console.log(`📡 Local: http://localhost:3000`);
|
console.log(`📡 Local: http://localhost:${appPort}`);
|
||||||
} catch(err){
|
} catch(err){
|
||||||
console.log("Shutting down application...");
|
console.log("Shutting down application...");
|
||||||
process.exit(100);
|
process.exit(100);
|
||||||
@@ -296,7 +296,7 @@ app.listen(3000, async() => {
|
|||||||
|
|
||||||
process.on('SIGTERM', () => {
|
process.on('SIGTERM', () => {
|
||||||
console.log("CLOSE APP");
|
console.log("CLOSE APP");
|
||||||
app.close( async() => {
|
server.close(async () => {
|
||||||
await adapter.disconnect();
|
await adapter.disconnect();
|
||||||
console.log("DB DISCONNECTED");
|
console.log("DB DISCONNECTED");
|
||||||
});
|
});
|
||||||
|
|||||||
Reference in New Issue
Block a user