Implement ordering process
This commit is contained in:
@@ -4,6 +4,7 @@ import { Product } from "../models/product.model";
|
||||
import { OrderItem } from "../models/orderItem.model";
|
||||
import { Brand } from "../models/brand.model";
|
||||
import { Category } from "../models/category.model";
|
||||
import { Sequelize } from "sequelize-typescript";
|
||||
|
||||
export const order = Router()
|
||||
|
||||
@@ -36,25 +37,20 @@ order.get("/:id", (req: Request, res: Response) => {
|
||||
|
||||
// Place a new order
|
||||
order.post("/", (req: Request, res: Response) => {
|
||||
let totalPrice = 0
|
||||
|
||||
Order.create(req.body)
|
||||
.then(async order => {
|
||||
for (let orderItem of req.body.orderItems) {
|
||||
OrderItem.create({
|
||||
"orderId": order.id,
|
||||
"quantity": orderItem.quantity,
|
||||
"orderPrice": orderItem.orderPrice,
|
||||
"productId": orderItem.productId
|
||||
orderId: order.id,
|
||||
quantity: orderItem.quantity,
|
||||
orderPrice: orderItem.orderPrice,
|
||||
productId: orderItem.productId
|
||||
})
|
||||
|
||||
totalPrice += orderItem.quantity * orderItem.orderPrice
|
||||
|
||||
Order.update({
|
||||
totalPrice: totalPrice
|
||||
}, {
|
||||
where: { id: order.id }
|
||||
})
|
||||
Product.decrement(
|
||||
"inStock",
|
||||
{ where: { id: orderItem.productId } }
|
||||
)
|
||||
}
|
||||
|
||||
// Created
|
||||
|
||||
@@ -20,11 +20,17 @@ import brands from "./../data/brands.json"
|
||||
* Delete all datasets in every database table
|
||||
*/
|
||||
export function deleteAllTables() {
|
||||
Category.destroy({ truncate: true })
|
||||
Order.destroy({ truncate: true })
|
||||
OrderItem.destroy({truncate: true })
|
||||
Order.destroy({ truncate: true })
|
||||
|
||||
Product.destroy({ truncate: true })
|
||||
Brand.destroy({ truncate: true })
|
||||
Category.destroy({ truncate: true })
|
||||
|
||||
Address.destroy({ truncate: true })
|
||||
Payment.destroy({ truncate: true })
|
||||
Account.destroy({ truncate: true })
|
||||
AccountRole.destroy({ truncate: true})
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -20,6 +20,12 @@ app.use(bodyParser.json())
|
||||
// Create database and tables
|
||||
startDatabase()
|
||||
|
||||
// Add delay for more realistic response times
|
||||
app.use((req, res, next) => {
|
||||
console.log(123)
|
||||
setTimeout(next, Math.floor((Math.random () * 4000) + 100))
|
||||
})
|
||||
|
||||
// Routes
|
||||
app.use("/api", api)
|
||||
app.use("/categories", category)
|
||||
|
||||
Reference in New Issue
Block a user