Add order, shipping dock, and transaction routes with CRUD operations and Swagger documentation
- Implemented GET, POST, PUT, and DELETE endpoints for orders, shipping docks, and transactions. - Added Swagger annotations for API documentation. - Included error handling for database operations. - Configured pnpm workspace to ignore built dependencies for sqlite3.
This commit is contained in:
@@ -0,0 +1,259 @@
|
||||
var express = require("express");
|
||||
var router = express.Router();
|
||||
|
||||
/**
|
||||
* @swagger
|
||||
* /api/v1/order:
|
||||
* get:
|
||||
* summary: Get all orders
|
||||
* tags: [Order]
|
||||
* responses:
|
||||
* 200:
|
||||
* description: List of all orders
|
||||
* content:
|
||||
* application/json:
|
||||
* schema:
|
||||
* type: object
|
||||
* properties:
|
||||
* success:
|
||||
* type: boolean
|
||||
* data:
|
||||
* type: array
|
||||
* items:
|
||||
* $ref: '#/components/schemas/Order'
|
||||
*/
|
||||
// GET all orders
|
||||
router.get("/", async function (req, res, next) {
|
||||
try {
|
||||
const db = req.app.get("db");
|
||||
const orders = await db.order.findAll();
|
||||
res.json({ success: true, data: orders });
|
||||
} catch (error) {
|
||||
console.error(error);
|
||||
res.status(500).json({
|
||||
success: false,
|
||||
message: "Error fetching orders",
|
||||
error: error.message,
|
||||
});
|
||||
}
|
||||
});
|
||||
|
||||
/**
|
||||
* @swagger
|
||||
* /api/v1/order/{id}:
|
||||
* get:
|
||||
* summary: Get one order by ID
|
||||
* tags: [Order]
|
||||
* parameters:
|
||||
* - in: path
|
||||
* name: id
|
||||
* required: true
|
||||
* schema:
|
||||
* type: integer
|
||||
* responses:
|
||||
* 200:
|
||||
* description: Order details
|
||||
* 404:
|
||||
* description: Order not found
|
||||
*/
|
||||
// GET one order by id
|
||||
router.get("/:id", async function (req, res, next) {
|
||||
try {
|
||||
const db = req.app.get("db");
|
||||
const order = await db.order.findByPk(req.params.id);
|
||||
|
||||
if (!order) {
|
||||
return res
|
||||
.status(404)
|
||||
.json({ success: false, message: "Order not found" });
|
||||
}
|
||||
|
||||
res.json({ success: true, data: order });
|
||||
} catch (error) {
|
||||
console.error(error);
|
||||
res.status(500).json({
|
||||
success: false,
|
||||
message: "Error fetching order",
|
||||
error: error.message,
|
||||
});
|
||||
}
|
||||
});
|
||||
|
||||
/**
|
||||
* @swagger
|
||||
* /api/v1/order:
|
||||
* post:
|
||||
* summary: Create a new order
|
||||
* tags: [Order]
|
||||
* requestBody:
|
||||
* required: true
|
||||
* content:
|
||||
* application/json:
|
||||
* schema:
|
||||
* type: object
|
||||
* required:
|
||||
* - user_id
|
||||
* - amount
|
||||
* - tax
|
||||
* properties:
|
||||
* user_id:
|
||||
* type: integer
|
||||
* amount:
|
||||
* type: number
|
||||
* format: decimal
|
||||
* tax:
|
||||
* type: number
|
||||
* format: decimal
|
||||
* notes:
|
||||
* type: string
|
||||
* status:
|
||||
* type: integer
|
||||
* enum: [0, 1]
|
||||
* default: 0
|
||||
* description: 0 = not paid, 1 = paid
|
||||
* responses:
|
||||
* 201:
|
||||
* description: Order created successfully
|
||||
* 400:
|
||||
* description: Bad request
|
||||
*/
|
||||
// POST - Create a new order
|
||||
router.post("/", async function (req, res, next) {
|
||||
try {
|
||||
const db = req.app.get("db");
|
||||
const { user_id, amount, tax, notes, status } = req.body;
|
||||
|
||||
if (!user_id || !amount || tax === undefined) {
|
||||
return res.status(400).json({
|
||||
success: false,
|
||||
message: "user_id, amount, and tax are required",
|
||||
});
|
||||
}
|
||||
|
||||
const order = await db.order.create({
|
||||
user_id,
|
||||
amount,
|
||||
tax,
|
||||
notes: notes || null,
|
||||
status: status !== undefined ? status : 0,
|
||||
});
|
||||
|
||||
res.status(201).json({ success: true, data: order });
|
||||
} catch (error) {
|
||||
console.error(error);
|
||||
res.status(500).json({
|
||||
success: false,
|
||||
message: "Error creating order",
|
||||
error: error.message,
|
||||
});
|
||||
}
|
||||
});
|
||||
|
||||
/**
|
||||
* @swagger
|
||||
* /api/v1/order/{id}:
|
||||
* put:
|
||||
* summary: Update an order by ID
|
||||
* tags: [Order]
|
||||
* parameters:
|
||||
* - in: path
|
||||
* name: id
|
||||
* required: true
|
||||
* schema:
|
||||
* type: integer
|
||||
* requestBody:
|
||||
* content:
|
||||
* application/json:
|
||||
* schema:
|
||||
* type: object
|
||||
* properties:
|
||||
* user_id:
|
||||
* type: integer
|
||||
* amount:
|
||||
* type: number
|
||||
* tax:
|
||||
* type: number
|
||||
* notes:
|
||||
* type: string
|
||||
* status:
|
||||
* type: integer
|
||||
* enum: [0, 1]
|
||||
* responses:
|
||||
* 200:
|
||||
* description: Order updated successfully
|
||||
* 404:
|
||||
* description: Order not found
|
||||
* delete:
|
||||
* summary: Delete an order by ID
|
||||
* tags: [Order]
|
||||
* parameters:
|
||||
* - in: path
|
||||
* name: id
|
||||
* required: true
|
||||
* schema:
|
||||
* type: integer
|
||||
* responses:
|
||||
* 200:
|
||||
* description: Order deleted successfully
|
||||
* 404:
|
||||
* description: Order not found
|
||||
*/
|
||||
// PUT - Update an order by id
|
||||
router.put("/:id", async function (req, res, next) {
|
||||
try {
|
||||
const db = req.app.get("db");
|
||||
const { user_id, amount, tax, notes, status } = req.body;
|
||||
|
||||
const order = await db.order.findByPk(req.params.id);
|
||||
|
||||
if (!order) {
|
||||
return res
|
||||
.status(404)
|
||||
.json({ success: false, message: "Order not found" });
|
||||
}
|
||||
|
||||
await order.update({
|
||||
user_id: user_id !== undefined ? user_id : order.user_id,
|
||||
amount: amount !== undefined ? amount : order.amount,
|
||||
tax: tax !== undefined ? tax : order.tax,
|
||||
notes: notes !== undefined ? notes : order.notes,
|
||||
status: status !== undefined ? status : order.status,
|
||||
});
|
||||
|
||||
res.json({ success: true, data: order });
|
||||
} catch (error) {
|
||||
console.error(error);
|
||||
res.status(500).json({
|
||||
success: false,
|
||||
message: "Error updating order",
|
||||
error: error.message,
|
||||
});
|
||||
}
|
||||
});
|
||||
|
||||
// DELETE - Delete an order by id
|
||||
router.delete("/:id", async function (req, res, next) {
|
||||
try {
|
||||
const db = req.app.get("db");
|
||||
const order = await db.order.findByPk(req.params.id);
|
||||
|
||||
if (!order) {
|
||||
return res
|
||||
.status(404)
|
||||
.json({ success: false, message: "Order not found" });
|
||||
}
|
||||
|
||||
await order.destroy();
|
||||
|
||||
res.json({ success: true, message: "Order deleted successfully" });
|
||||
} catch (error) {
|
||||
console.error(error);
|
||||
res.status(500).json({
|
||||
success: false,
|
||||
message: "Error deleting order",
|
||||
error: error.message,
|
||||
});
|
||||
}
|
||||
});
|
||||
|
||||
module.exports = router;
|
||||
Reference in New Issue
Block a user