Extend database with more tables, rewrite API doc, improve API endpoints
This commit is contained in:
@@ -1,27 +1,41 @@
|
||||
import { Router, Request, Response, NextFunction } from "express";
|
||||
import { Router, Request, Response } from "express";
|
||||
import { Account } from "../models/account.model";
|
||||
import { validateString } from "../scripts/validateHelper";
|
||||
import { Address } from "../models/address.model";
|
||||
import { Payment } from "../models/payment.model";
|
||||
import { AccountRole } from "../models/accountRole.model";
|
||||
|
||||
export const account = Router()
|
||||
|
||||
// Login user
|
||||
account.post("/login", (req: Request, res: Response) => {
|
||||
Account.findOne({
|
||||
raw: true,
|
||||
where: { username: req.body.username }
|
||||
where: { username: req.body.username },
|
||||
include: [ Address, Payment, AccountRole ],
|
||||
attributes: {
|
||||
exclude: [
|
||||
"accountRoleId"
|
||||
]
|
||||
}
|
||||
})
|
||||
.then(account => {
|
||||
if (account != null) {
|
||||
if (account.password == req.body.password) {
|
||||
if (account.dataValues.password == req.body.password) {
|
||||
// Status: 200 OK
|
||||
res.status(200).json(account).send()
|
||||
} else {
|
||||
// Status: 401 Unauthorized
|
||||
res.status(401).send()
|
||||
res.status(401).json({
|
||||
code: 401,
|
||||
message: "Unauthorized"
|
||||
}).send()
|
||||
}
|
||||
} else {
|
||||
// Status: 400 Bad request
|
||||
res.status(400).send()
|
||||
res.status(400).json({
|
||||
code: 400,
|
||||
message: "Bad Request"
|
||||
}).send()
|
||||
}
|
||||
}
|
||||
)
|
||||
@@ -34,6 +48,7 @@ account.post("/", (req: Request, res: Response) => {
|
||||
{
|
||||
// Status: 400 Bad request
|
||||
res.status(400).json({
|
||||
code: 400,
|
||||
message: "Username too short!"
|
||||
}).send()
|
||||
}
|
||||
@@ -43,6 +58,7 @@ account.post("/", (req: Request, res: Response) => {
|
||||
{
|
||||
// Status: 400 Bad request
|
||||
res.status(400).json({
|
||||
code: 400,
|
||||
message: "Password too short!"
|
||||
}).send()
|
||||
}
|
||||
@@ -54,7 +70,10 @@ account.post("/", (req: Request, res: Response) => {
|
||||
res.status(201).json(account).send()
|
||||
}).catch(reason => {
|
||||
// Status: 409 Conflict
|
||||
res.status(409).send()
|
||||
res.status(409).json({
|
||||
code: 409,
|
||||
message: "Username already in use"
|
||||
}).send()
|
||||
})
|
||||
})
|
||||
|
||||
@@ -70,7 +89,8 @@ account.patch("/", (req: Request, res: Response) => {
|
||||
.catch(error => {
|
||||
// Status: 400 Bad request
|
||||
res.status(400).json({
|
||||
message: error
|
||||
code: 400,
|
||||
message: error
|
||||
}).send()
|
||||
})
|
||||
})
|
||||
|
||||
@@ -9,19 +9,17 @@ category.get("/", (req: Request, res: Response, next: NextFunction) => {
|
||||
.then(categories => {
|
||||
res.status(200).json(categories).send()
|
||||
})
|
||||
.catch(error => {
|
||||
res.status(400)
|
||||
})
|
||||
})
|
||||
|
||||
// Add new category
|
||||
category.post("/", (req: Request, res: Response, next: NextFunction) => {
|
||||
Category.create(req.body)
|
||||
.then(category => {
|
||||
res.status(201).send()
|
||||
res.status(201).json(category).send()
|
||||
})
|
||||
.catch(error => {
|
||||
res.status(400).json({
|
||||
code: 400,
|
||||
message: error
|
||||
}).send()
|
||||
})
|
||||
@@ -33,10 +31,11 @@ category.delete("/:id", (req: Request, res: Response, next: NextFunction) => {
|
||||
where: { id: req.params.id }
|
||||
})
|
||||
.then(category => {
|
||||
res.status(200).send()
|
||||
res.status(200).json(category).send()
|
||||
})
|
||||
.catch(error => {
|
||||
res.status(400).json({
|
||||
code: 400,
|
||||
message: error
|
||||
}).send()
|
||||
})
|
||||
|
||||
@@ -23,31 +23,25 @@ order.post("/", (req: Request, res: Response) => {
|
||||
let totalPrice = 0
|
||||
|
||||
Order.create(req.body)
|
||||
.then(order => {
|
||||
|
||||
for (let orderItem of req.body.orderItem) {
|
||||
.then(async order => {
|
||||
for (let orderItem of req.body.orderItems) {
|
||||
OrderItem.create({
|
||||
"orderId": order.id,
|
||||
"quantity": orderItem.quantity,
|
||||
"orderPrice": orderItem.orderPrice,
|
||||
"productId": orderItem.productId
|
||||
})
|
||||
|
||||
Product.findOne({
|
||||
raw: true,
|
||||
where: { id: orderItem.productId }
|
||||
totalPrice += orderItem.quantity * orderItem.orderPrice
|
||||
|
||||
Order.update({
|
||||
totalPrice: totalPrice
|
||||
}, {
|
||||
where: { id: order.id }
|
||||
})
|
||||
.then(product => {
|
||||
totalPrice += product.price * orderItem.quantity
|
||||
|
||||
Order.update({
|
||||
totalPrice: totalPrice
|
||||
}, {
|
||||
where: { id: order.id },
|
||||
})
|
||||
})
|
||||
}
|
||||
})
|
||||
|
||||
// Created
|
||||
res.status(201).send()
|
||||
// Created
|
||||
res.status(201).json(order).send()
|
||||
})
|
||||
})
|
||||
@@ -1,13 +1,20 @@
|
||||
import { Router, Request, Response, NextFunction } from "express";
|
||||
import { Product } from "../models/product.model";
|
||||
import { Category } from "../models/category.model";
|
||||
import { Brand } from "../models/brand.model";
|
||||
|
||||
export const product = Router()
|
||||
|
||||
// Get all products
|
||||
product.get("/", (req: Request, res: Response) => {
|
||||
Product.findAll({
|
||||
include: [ Category ]
|
||||
include: [ Category, Brand ],
|
||||
attributes: {
|
||||
exclude: [
|
||||
"categoryId",
|
||||
"brandId"
|
||||
]
|
||||
}
|
||||
})
|
||||
.then(products => {
|
||||
res.status(200).json(products)
|
||||
@@ -16,7 +23,18 @@ product.get("/", (req: Request, res: Response) => {
|
||||
|
||||
// Get a product by id
|
||||
product.get("/:productId", (req: Request, res: Response) => {
|
||||
Product.findByPk(req.params.productId)
|
||||
Product.findByPk(
|
||||
req.params.productId,
|
||||
{
|
||||
include: [ Category, Brand ],
|
||||
attributes: {
|
||||
exclude: [
|
||||
"categoryId",
|
||||
"brandId"
|
||||
]
|
||||
}
|
||||
}
|
||||
)
|
||||
.then(product => {
|
||||
res.status(200).json(product).send()
|
||||
})
|
||||
@@ -30,6 +48,7 @@ product.post("/", (req: Request, res: Response) => {
|
||||
})
|
||||
.catch(error => {
|
||||
res.status(400).json({
|
||||
code: 400,
|
||||
message: error
|
||||
}).send()
|
||||
})
|
||||
@@ -45,6 +64,7 @@ product.delete("/:id", (req: Request, res: Response) => {
|
||||
})
|
||||
.catch(error => {
|
||||
res.status(400).json({
|
||||
code: 400,
|
||||
message: error
|
||||
}).send()
|
||||
})
|
||||
|
||||
Reference in New Issue
Block a user