258 lines
6.8 KiB
JavaScript
258 lines
6.8 KiB
JavaScript
|
|
var express = require("express");
|
||
|
|
var router = express.Router();
|
||
|
|
|
||
|
|
/**
|
||
|
|
* @swagger
|
||
|
|
* /api/v1/shipping_dock:
|
||
|
|
* get:
|
||
|
|
* summary: Get all shipping docks
|
||
|
|
* tags: [Shipping Dock]
|
||
|
|
* responses:
|
||
|
|
* 200:
|
||
|
|
* description: List of all shipping docks
|
||
|
|
* content:
|
||
|
|
* application/json:
|
||
|
|
* schema:
|
||
|
|
* type: object
|
||
|
|
* properties:
|
||
|
|
* success:
|
||
|
|
* type: boolean
|
||
|
|
* data:
|
||
|
|
* type: array
|
||
|
|
* items:
|
||
|
|
* $ref: '#/components/schemas/ShippingDock'
|
||
|
|
*/
|
||
|
|
// GET all shipping docks
|
||
|
|
router.get("/", async function (req, res, next) {
|
||
|
|
try {
|
||
|
|
const db = req.app.get("db");
|
||
|
|
const shippingDocks = await db.shipping_dock.findAll();
|
||
|
|
res.json({ success: true, data: shippingDocks });
|
||
|
|
} catch (error) {
|
||
|
|
console.error(error);
|
||
|
|
res.status(500).json({
|
||
|
|
success: false,
|
||
|
|
message: "Error fetching shipping docks",
|
||
|
|
error: error.message,
|
||
|
|
});
|
||
|
|
}
|
||
|
|
});
|
||
|
|
|
||
|
|
/**
|
||
|
|
* @swagger
|
||
|
|
* /api/v1/shipping_dock/{id}:
|
||
|
|
* get:
|
||
|
|
* summary: Get one shipping dock by ID
|
||
|
|
* tags: [Shipping Dock]
|
||
|
|
* parameters:
|
||
|
|
* - in: path
|
||
|
|
* name: id
|
||
|
|
* required: true
|
||
|
|
* schema:
|
||
|
|
* type: integer
|
||
|
|
* description: Shipping dock ID
|
||
|
|
* responses:
|
||
|
|
* 200:
|
||
|
|
* description: Shipping dock details
|
||
|
|
* content:
|
||
|
|
* application/json:
|
||
|
|
* schema:
|
||
|
|
* type: object
|
||
|
|
* properties:
|
||
|
|
* success:
|
||
|
|
* type: boolean
|
||
|
|
* data:
|
||
|
|
* $ref: '#/components/schemas/ShippingDock'
|
||
|
|
* 404:
|
||
|
|
* description: Shipping dock not found
|
||
|
|
*/
|
||
|
|
// GET one shipping dock by id
|
||
|
|
router.get("/:id", async function (req, res, next) {
|
||
|
|
try {
|
||
|
|
const db = req.app.get("db");
|
||
|
|
const shippingDock = await db.shipping_dock.findByPk(req.params.id);
|
||
|
|
|
||
|
|
if (!shippingDock) {
|
||
|
|
return res
|
||
|
|
.status(404)
|
||
|
|
.json({ success: false, message: "Shipping dock not found" });
|
||
|
|
}
|
||
|
|
|
||
|
|
res.json({ success: true, data: shippingDock });
|
||
|
|
} catch (error) {
|
||
|
|
console.error(error);
|
||
|
|
res.status(500).json({
|
||
|
|
success: false,
|
||
|
|
message: "Error fetching shipping dock",
|
||
|
|
error: error.message,
|
||
|
|
});
|
||
|
|
}
|
||
|
|
});
|
||
|
|
|
||
|
|
/**
|
||
|
|
* @swagger
|
||
|
|
* /api/v1/shipping_dock:
|
||
|
|
* post:
|
||
|
|
* summary: Create a new shipping dock
|
||
|
|
* tags: [Shipping Dock]
|
||
|
|
* requestBody:
|
||
|
|
* required: true
|
||
|
|
* content:
|
||
|
|
* application/json:
|
||
|
|
* schema:
|
||
|
|
* type: object
|
||
|
|
* required:
|
||
|
|
* - name
|
||
|
|
* properties:
|
||
|
|
* name:
|
||
|
|
* type: string
|
||
|
|
* description: Name of the shipping dock
|
||
|
|
* status:
|
||
|
|
* type: integer
|
||
|
|
* description: Status (1 = active, 0 = inactive)
|
||
|
|
* enum: [0, 1]
|
||
|
|
* default: 1
|
||
|
|
* responses:
|
||
|
|
* 201:
|
||
|
|
* description: Shipping dock created successfully
|
||
|
|
* 400:
|
||
|
|
* description: Bad request - missing required fields
|
||
|
|
*/
|
||
|
|
// POST - Create a new shipping dock
|
||
|
|
router.post("/", async function (req, res, next) {
|
||
|
|
try {
|
||
|
|
const db = req.app.get("db");
|
||
|
|
const { name, status } = req.body;
|
||
|
|
|
||
|
|
if (!name) {
|
||
|
|
return res
|
||
|
|
.status(400)
|
||
|
|
.json({ success: false, message: "Name is required" });
|
||
|
|
}
|
||
|
|
|
||
|
|
const shippingDock = await db.shipping_dock.create({
|
||
|
|
name,
|
||
|
|
status: status !== undefined ? status : 1,
|
||
|
|
});
|
||
|
|
|
||
|
|
res.status(201).json({ success: true, data: shippingDock });
|
||
|
|
} catch (error) {
|
||
|
|
console.error(error);
|
||
|
|
res.status(500).json({
|
||
|
|
success: false,
|
||
|
|
message: "Error creating shipping dock",
|
||
|
|
error: error.message,
|
||
|
|
});
|
||
|
|
}
|
||
|
|
});
|
||
|
|
|
||
|
|
/**
|
||
|
|
* @swagger
|
||
|
|
* /api/v1/shipping_dock/{id}:
|
||
|
|
* put:
|
||
|
|
* summary: Update a shipping dock by ID
|
||
|
|
* tags: [Shipping Dock]
|
||
|
|
* parameters:
|
||
|
|
* - in: path
|
||
|
|
* name: id
|
||
|
|
* required: true
|
||
|
|
* schema:
|
||
|
|
* type: integer
|
||
|
|
* description: Shipping dock ID
|
||
|
|
* requestBody:
|
||
|
|
* required: true
|
||
|
|
* content:
|
||
|
|
* application/json:
|
||
|
|
* schema:
|
||
|
|
* type: object
|
||
|
|
* properties:
|
||
|
|
* name:
|
||
|
|
* type: string
|
||
|
|
* status:
|
||
|
|
* type: integer
|
||
|
|
* enum: [0, 1]
|
||
|
|
* responses:
|
||
|
|
* 200:
|
||
|
|
* description: Shipping dock updated successfully
|
||
|
|
* 404:
|
||
|
|
* description: Shipping dock not found
|
||
|
|
*/
|
||
|
|
// PUT - Update a shipping dock by id
|
||
|
|
router.put("/:id", async function (req, res, next) {
|
||
|
|
try {
|
||
|
|
const db = req.app.get("db");
|
||
|
|
const { name, status } = req.body;
|
||
|
|
|
||
|
|
const shippingDock = await db.shipping_dock.findByPk(req.params.id);
|
||
|
|
|
||
|
|
if (!shippingDock) {
|
||
|
|
return res
|
||
|
|
.status(404)
|
||
|
|
.json({ success: false, message: "Shipping dock not found" });
|
||
|
|
}
|
||
|
|
|
||
|
|
await shippingDock.update({
|
||
|
|
name: name !== undefined ? name : shippingDock.name,
|
||
|
|
status: status !== undefined ? status : shippingDock.status,
|
||
|
|
});
|
||
|
|
|
||
|
|
res.json({ success: true, data: shippingDock });
|
||
|
|
} catch (error) {
|
||
|
|
console.error(error);
|
||
|
|
res.status(500).json({
|
||
|
|
success: false,
|
||
|
|
message: "Error updating shipping dock",
|
||
|
|
error: error.message,
|
||
|
|
});
|
||
|
|
}
|
||
|
|
});
|
||
|
|
|
||
|
|
/**
|
||
|
|
* @swagger
|
||
|
|
* /api/v1/shipping_dock/{id}:
|
||
|
|
* delete:
|
||
|
|
* summary: Delete a shipping dock by ID
|
||
|
|
* tags: [Shipping Dock]
|
||
|
|
* parameters:
|
||
|
|
* - in: path
|
||
|
|
* name: id
|
||
|
|
* required: true
|
||
|
|
* schema:
|
||
|
|
* type: integer
|
||
|
|
* description: Shipping dock ID
|
||
|
|
* responses:
|
||
|
|
* 200:
|
||
|
|
* description: Shipping dock deleted successfully
|
||
|
|
* 404:
|
||
|
|
* description: Shipping dock not found
|
||
|
|
*/
|
||
|
|
// DELETE - Delete a shipping dock by id
|
||
|
|
router.delete("/:id", async function (req, res, next) {
|
||
|
|
try {
|
||
|
|
const db = req.app.get("db");
|
||
|
|
const shippingDock = await db.shipping_dock.findByPk(req.params.id);
|
||
|
|
|
||
|
|
if (!shippingDock) {
|
||
|
|
return res
|
||
|
|
.status(404)
|
||
|
|
.json({ success: false, message: "Shipping dock not found" });
|
||
|
|
}
|
||
|
|
|
||
|
|
await shippingDock.destroy();
|
||
|
|
|
||
|
|
res.json({
|
||
|
|
success: true,
|
||
|
|
message: "Shipping dock deleted successfully",
|
||
|
|
});
|
||
|
|
} catch (error) {
|
||
|
|
console.error(error);
|
||
|
|
res.status(500).json({
|
||
|
|
success: false,
|
||
|
|
message: "Error deleting shipping dock",
|
||
|
|
error: error.message,
|
||
|
|
});
|
||
|
|
}
|
||
|
|
});
|
||
|
|
|
||
|
|
module.exports = router;
|