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

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