Documenting, restructure and expand API

This commit is contained in:
2024-09-13 12:07:33 +02:00
parent 39ce77ea63
commit 2847bd940f
13 changed files with 877 additions and 74 deletions

View File

@@ -1,18 +1,53 @@
import { Router, Request, Response, NextFunction } from "express";
import { Router, Request, Response } from "express";
import { Order } from "../models/order.model";
import { Product } from "../models/product.model";
import { OrderItem } from "../models/orderItem.model";
export const order = Router()
order.get("/", (req: Request, res: Response, next: NextFunction) => {
// Get all orders of one account by it's user id
order.get("/:id", (req: Request, res: Response) => {
Order.findAll({
where: { accountId: req.query.accountId },
where: { accountId: req.params.id },
include: [
{ model: OrderItem, include: [ Product ] }
]
})
.then(orders => {
res.send(orders)
res.status(200).send(orders)
})
})
// Place a new order
order.post("/", (req: Request, res: Response) => {
let totalPrice = 0
Order.create(req.body)
.then(order => {
for (let orderItem of req.body.orderItem) {
OrderItem.create({
"orderId": order.id,
"quantity": orderItem.quantity,
"productId": orderItem.productId
})
Product.findOne({
raw: true,
where: { id: orderItem.productId }
})
.then(product => {
totalPrice += product.price * orderItem.quantity
Order.update({
totalPrice: totalPrice
}, {
where: { id: order.id },
})
})
}
})
// Created
res.status(201).send()
})