Documenting, restructure and expand API

This commit is contained in:
2024-09-13 12:07:33 +02:00
parent 5134a9df31
commit a254f99404
13 changed files with 877 additions and 74 deletions

View File

@@ -11,19 +11,19 @@ export class Account extends Model {
password: string
@Column
firstName: string
firstName: string = ""
@Column
lastName: string
lastName: string = ""
@Column
street: string
street: string = ""
@Column
houseNumber: number
houseNumber: number = 0
@Column
postalCode: number
postalCode: number = 0
@Column
city: string

View File

@@ -4,57 +4,82 @@ import { validateString } from "../scripts/validateHelper";
export const account = Router()
// Request all user from the database
account.get("/", (req: Request, res: Response, next: NextFunction) => {
Account.findAll()
.then(accounts => {
res.json(accounts)
})
.catch(next)
// Login user
account.get("/", (req: Request, res: Response) => {
Account.findOne({
raw: true,
where: { username: req.body.username }
})
.then(account => {
if (account != null) {
if (account.password == req.body.password) {
// Status: 200 Created
res.status(201).json({
loginSuccessful: true,
userId: account.id,
message: ""
}).send()
} else {
// Status: 401 Unauthorized
res.status(401).json({
loginSuccessful: false,
userId: -1,
message: "Wrong password"
}).send()
}
} else {
// Status: 401 Unauthorized
res.status(401).json({
loginSuccessful: false,
userId: -1,
message: "Username doesn't exists"
}).send()
}
}
)
})
// Creating a new user
account.post("/register", (req: Request, res: Response, next: NextFunction) => {
account.post("/", (req: Request, res: Response) => {
if (!validateString(req.body.username, 4))
{
// Status: 400 Bad request
res.status(400).send({ error: "Username too short!" })
res.status(400).json({
message: "Username too short!"
}).send()
}
else if (!validateString(req.body.password, 8))
{
// Status: 400 Bad request
res.status(400).send({ error: "Password too short!" })
res.status(400).json({
message: "Password too short!"
}).send()
}
else
{
Account.create(req.body)
.then(account => {
res.json(account)
// Status: 200 OK
res.status(200).send()
res.status(200).json(account).send()
}).catch(reason => {
// Status: 400 Bad request
res.status(400).send({ error: reason })
res.status(400).json({
message: reason
}).send()
})
}
})
account.post("/login", (req: Request, res: Response, next: NextFunction) => {
Account.findOne({ raw: true, where: { username: req.body.username }})
account.patch("/", (req: Request, res: Response) => {
Account.update(req.body,
{
where: { id: req.body.id }
})
.then(account => {
if (account != null) {
if (account.password == req.body.password) {
// Status: 200 OK
res.status(200).send({ userAccountId: account.id })
} else {
// Status: 401 Unauthorized
res.status(401).send()
}
} else {
// Status: 401 Unauthorized
res.status(401).send()
}
}
)
})
res.status(200).send()
})
.catch(error => {
res.status(400).json({
message: error
}).send()
})
})

View File

@@ -4,7 +4,7 @@ import { deleteAllTables, prepopulateDatabase } from '../scripts/databaseHelper'
export const api = Router()
api.get("/", (req: Request, res: Response, next: NextFunction) => {
res.send("Hello World!")
res.status(200).send()
})
api.get("/resetdatabase", (req: Request, res: Response, next: NextFunction) => {

View File

@@ -3,19 +3,41 @@ import { Category } from "../models/category.model";
export const category = Router()
// Get all categories
category.get("/", (req: Request, res: Response, next: NextFunction) => {
Category.findAll()
.then(categories => {
res.json(categories)
res.status(200).json(categories).send()
})
.catch(error => {
res.status(400).json({ message: error }).send()
})
.catch(next)
})
// Add new category
category.post("/", (req: Request, res: Response, next: NextFunction) => {
try {
const category = Category.create(req.body)
res.status(201).json(category)
} catch (e) {
next(e)
}
Category.create(req.body)
.then(category => {
res.status(201).send()
})
.catch(error => {
res.status(400).json({
message: error
}).send()
})
})
// Delete category
category.delete("/:id", (req: Request, res: Response, next: NextFunction) => {
Category.destroy({
where: { id: req.params.id }
})
.then(category => {
res.status(200).send()
})
.catch(error => {
res.status(406).json({
message: error
}).send()
})
})

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()
})

View File

@@ -1,6 +0,0 @@
import { Product } from "../models/product.model";
import { OrderItem } from "../models/orderItem.model";
import { Router, Request, Response, NextFunction } from "express";
export const orderItem = Router()

View File

@@ -1,19 +1,51 @@
import { Router, Request, Response, NextFunction } from "express";
import { Product } from "../models/product.model";
import { Category } from "../models/category.model";
export const product = Router()
product.get("/", (req: Request, res: Response, next: NextFunction) => {
Product.findAll()
// Get all products
product.get("/", (req: Request, res: Response) => {
Product.findAll({
include: [ Category ]
})
.then(products => {
res.json(products)
res.status(200).json(products)
})
.catch(next)
})
product.get("/:productId", (req: Request, res: Response, next: NextFunction) => {
// Get a product by id
product.get("/:productId", (req: Request, res: Response) => {
Product.findByPk(req.params.productId)
.then(product => {
res.json(product)
res.status(200).json(product)
})
})
// Add a new product
product.post("/", (req: Request, res: Response) => {
Product.create(req.body)
.then(product => {
res.status(200).send()
})
.catch(error => {
res.status(400).json({
message: error
}).send()
})
})
// Remove a product
product.delete("/:id", (req: Request, res: Response) => {
Product.destroy({
where: { id: req.params.id }
})
.then(product => {
res.status(200).send()
})
.catch(error => {
res.status(406).json({
message: error
}).send()
})
})

View File

@@ -7,7 +7,6 @@ import { category } from './routes/category.routes'
import { product } from './routes/product.routes'
import { order } from './routes/order.routes'
import { account } from './routes/account.routes'
import { orderItem } from './routes/orderItem.routes'
const app = express()
const port = 3000
@@ -27,7 +26,6 @@ app.use("/categories", category)
app.use("/products", product)
app.use("/orders", order)
app.use("/accounts", account)
app.use("/orderItems", orderItem)
// Static files
const path = require('path')