Improved user feedback system, improved Product detail view

This commit is contained in:
2024-09-20 12:57:19 +02:00
parent ed264ff026
commit 54d13686cf
24 changed files with 1113 additions and 602 deletions

View File

@@ -1,8 +1,9 @@
import { Table, Column, Model, BelongsTo, ForeignKey, HasMany } from 'sequelize-typescript';
import { Table, Column, Model, BelongsTo, ForeignKey, HasMany, Unique } from 'sequelize-typescript';
import { Product } from './product.model';
@Table
export class Category extends Model {
@Unique
@Column
name: string

View File

@@ -14,33 +14,22 @@ account.post("/login", (req: Request, res: Response) => {
if (account != null) {
if (account.password == req.body.password) {
// Status: 200 OK
res.status(200).json({
loginSuccessful: true,
account: account,
message: ""
}).send()
res.status(200).json(account).send()
} else {
// Status: 401 Unauthorized
res.status(401).json({
loginSuccessful: false,
account: null,
message: "Wrong password"
}).send()
res.status(401).send()
}
} else {
// Status: 401 Unauthorized
res.status(401).json({
loginSuccessful: false,
userId: -1,
message: "Username doesn't exists"
}).send()
// Status: 400 Bad request
res.status(400).send()
}
}
)
})
// Creating a new user
account.post("/register", (req: Request, res: Response) => {
account.post("/", (req: Request, res: Response) => {
// Check if username is valid
if (!validateString(req.body.username, 4))
{
// Status: 400 Bad request
@@ -48,25 +37,25 @@ account.post("/register", (req: Request, res: Response) => {
message: "Username too short!"
}).send()
}
else if (!validateString(req.body.password, 8))
// Check if password is valid
if (!validateString(req.body.password, 8))
{
// Status: 400 Bad request
res.status(400).json({
message: "Password too short!"
}).send()
}
else
{
Account.create(req.body)
.then(account => {
res.status(200).json(account).send()
}).catch(reason => {
// Status: 400 Bad request
res.status(400).json({
message: reason
}).send()
})
}
// Create account
Account.create(req.body)
.then(account => {
// Status: 201 Created
res.status(201).json(account).send()
}).catch(reason => {
// Status: 409 Conflict
res.status(409).send()
})
})
account.patch("/", (req: Request, res: Response) => {
@@ -75,9 +64,11 @@ account.patch("/", (req: Request, res: Response) => {
where: { id: req.body.id }
})
.then(account => {
res.status(200).send()
// Status: 200 OK
res.status(200).json(account).send()
})
.catch(error => {
// Status: 400 Bad request
res.status(400).json({
message: error
}).send()

View File

@@ -10,7 +10,7 @@ category.get("/", (req: Request, res: Response, next: NextFunction) => {
res.status(200).json(categories).send()
})
.catch(error => {
res.status(400)//.json({ message: error }).send()
res.status(400)
})
})
@@ -36,7 +36,7 @@ category.delete("/:id", (req: Request, res: Response, next: NextFunction) => {
res.status(200).send()
})
.catch(error => {
res.status(406).json({
res.status(400).json({
message: error
}).send()
})

View File

@@ -14,7 +14,7 @@ order.get("/:id", (req: Request, res: Response) => {
]
})
.then(orders => {
res.status(200).send(orders)
res.status(200).json(orders).send()
})
})

View File

@@ -18,7 +18,7 @@ product.get("/", (req: Request, res: Response) => {
product.get("/:productId", (req: Request, res: Response) => {
Product.findByPk(req.params.productId)
.then(product => {
res.status(200).json(product)
res.status(200).json(product).send()
})
})
@@ -26,7 +26,7 @@ product.get("/:productId", (req: Request, res: Response) => {
product.post("/", (req: Request, res: Response) => {
Product.create(req.body)
.then(product => {
res.status(200).send()
res.status(201).json(product).send()
})
.catch(error => {
res.status(400).json({
@@ -41,10 +41,10 @@ product.delete("/:id", (req: Request, res: Response) => {
where: { id: req.params.id }
})
.then(product => {
res.status(200).send()
res.status(200).json(product).send()
})
.catch(error => {
res.status(406).json({
res.status(400).json({
message: error
}).send()
})