Matching UI with improved API
This commit is contained in:
@@ -22,20 +22,20 @@ account.post("/login", (req: Request, res: Response) => {
|
||||
if (account != null) {
|
||||
if (account.dataValues.password == req.body.password) {
|
||||
// Status: 200 OK
|
||||
res.status(200).json(account).send()
|
||||
res.status(200).json(account)
|
||||
} else {
|
||||
// Status: 401 Unauthorized
|
||||
res.status(401).json({
|
||||
code: 401,
|
||||
message: "Unauthorized"
|
||||
}).send()
|
||||
})
|
||||
}
|
||||
} else {
|
||||
// Status: 400 Bad request
|
||||
res.status(400).json({
|
||||
code: 400,
|
||||
message: "Bad Request"
|
||||
}).send()
|
||||
})
|
||||
}
|
||||
}
|
||||
)
|
||||
@@ -50,7 +50,7 @@ account.post("/", (req: Request, res: Response) => {
|
||||
res.status(400).json({
|
||||
code: 400,
|
||||
message: "Username too short!"
|
||||
}).send()
|
||||
})
|
||||
}
|
||||
|
||||
// Check if password is valid
|
||||
@@ -60,20 +60,20 @@ account.post("/", (req: Request, res: Response) => {
|
||||
res.status(400).json({
|
||||
code: 400,
|
||||
message: "Password too short!"
|
||||
}).send()
|
||||
})
|
||||
}
|
||||
|
||||
// Create account
|
||||
Account.create(req.body)
|
||||
.then(account => {
|
||||
// Status: 201 Created
|
||||
res.status(201).json(account).send()
|
||||
res.status(201).json(account)
|
||||
}).catch(reason => {
|
||||
// Status: 409 Conflict
|
||||
res.status(409).json({
|
||||
code: 409,
|
||||
message: "Username already in use"
|
||||
}).send()
|
||||
})
|
||||
})
|
||||
})
|
||||
|
||||
@@ -84,13 +84,13 @@ account.patch("/", (req: Request, res: Response) => {
|
||||
})
|
||||
.then(account => {
|
||||
// Status: 200 OK
|
||||
res.status(200).json(account).send()
|
||||
res.status(200).json(account)
|
||||
})
|
||||
.catch(error => {
|
||||
// Status: 400 Bad request
|
||||
res.status(400).json({
|
||||
code: 400,
|
||||
message: error
|
||||
}).send()
|
||||
})
|
||||
})
|
||||
})
|
||||
|
||||
@@ -7,7 +7,7 @@ export const category = Router()
|
||||
category.get("/", (req: Request, res: Response, next: NextFunction) => {
|
||||
Category.findAll()
|
||||
.then(categories => {
|
||||
res.status(200).json(categories).send()
|
||||
res.status(200).json(categories)
|
||||
})
|
||||
})
|
||||
|
||||
@@ -15,13 +15,13 @@ category.get("/", (req: Request, res: Response, next: NextFunction) => {
|
||||
category.post("/", (req: Request, res: Response, next: NextFunction) => {
|
||||
Category.create(req.body)
|
||||
.then(category => {
|
||||
res.status(201).json(category).send()
|
||||
res.status(201).json(category)
|
||||
})
|
||||
.catch(error => {
|
||||
res.status(400).json({
|
||||
code: 400,
|
||||
message: error
|
||||
}).send()
|
||||
})
|
||||
})
|
||||
})
|
||||
|
||||
@@ -31,12 +31,12 @@ category.delete("/:id", (req: Request, res: Response, next: NextFunction) => {
|
||||
where: { id: req.params.id }
|
||||
})
|
||||
.then(category => {
|
||||
res.status(200).json(category).send()
|
||||
res.status(200).json(category)
|
||||
})
|
||||
.catch(error => {
|
||||
res.status(400).json({
|
||||
code: 400,
|
||||
message: error
|
||||
}).send()
|
||||
})
|
||||
})
|
||||
})
|
||||
@@ -2,6 +2,8 @@ import { Router, Request, Response } from "express";
|
||||
import { Order } from "../models/order.model";
|
||||
import { Product } from "../models/product.model";
|
||||
import { OrderItem } from "../models/orderItem.model";
|
||||
import { Brand } from "../models/brand.model";
|
||||
import { Category } from "../models/category.model";
|
||||
|
||||
export const order = Router()
|
||||
|
||||
@@ -10,7 +12,21 @@ order.get("/:id", (req: Request, res: Response) => {
|
||||
Order.findAll({
|
||||
where: { accountId: req.params.id },
|
||||
include: [
|
||||
{ model: OrderItem, include: [ Product ] }
|
||||
{
|
||||
model: OrderItem,
|
||||
include: [
|
||||
{
|
||||
model: Product,
|
||||
include: [ Brand, Category ],
|
||||
attributes: {
|
||||
exclude: [
|
||||
"categoryId",
|
||||
"brandId"
|
||||
]
|
||||
}
|
||||
},
|
||||
]
|
||||
}
|
||||
]
|
||||
})
|
||||
.then(orders => {
|
||||
@@ -42,6 +58,6 @@ order.post("/", (req: Request, res: Response) => {
|
||||
}
|
||||
|
||||
// Created
|
||||
res.status(201).json(order).send()
|
||||
res.status(201).json(order)
|
||||
})
|
||||
})
|
||||
@@ -36,7 +36,7 @@ product.get("/:productId", (req: Request, res: Response) => {
|
||||
}
|
||||
)
|
||||
.then(product => {
|
||||
res.status(200).json(product).send()
|
||||
res.status(200).json(product)
|
||||
})
|
||||
})
|
||||
|
||||
@@ -44,13 +44,13 @@ product.get("/:productId", (req: Request, res: Response) => {
|
||||
product.post("/", (req: Request, res: Response) => {
|
||||
Product.create(req.body)
|
||||
.then(product => {
|
||||
res.status(201).json(product).send()
|
||||
res.status(201).json(product)
|
||||
})
|
||||
.catch(error => {
|
||||
res.status(400).json({
|
||||
code: 400,
|
||||
message: error
|
||||
}).send()
|
||||
})
|
||||
})
|
||||
})
|
||||
|
||||
@@ -60,12 +60,12 @@ product.delete("/:id", (req: Request, res: Response) => {
|
||||
where: { id: req.params.id }
|
||||
})
|
||||
.then(product => {
|
||||
res.status(200).json(product).send()
|
||||
res.status(200).json(product)
|
||||
})
|
||||
.catch(error => {
|
||||
res.status(400).json({
|
||||
code: 400,
|
||||
message: error
|
||||
}).send()
|
||||
})
|
||||
})
|
||||
})
|
||||
Reference in New Issue
Block a user