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:
+134
-20
@@ -1,44 +1,158 @@
|
||||
var createError = require('http-errors');
|
||||
var express = require('express');
|
||||
var path = require('path');
|
||||
var cookieParser = require('cookie-parser');
|
||||
var logger = require('morgan');
|
||||
var createError = require("http-errors");
|
||||
var express = require("express");
|
||||
var path = require("path");
|
||||
var cookieParser = require("cookie-parser");
|
||||
var logger = require("morgan");
|
||||
var swaggerJsdoc = require("swagger-jsdoc");
|
||||
var swaggerUi = require("swagger-ui-express");
|
||||
|
||||
var indexRouter = require('./routes/index');
|
||||
var usersRouter = require('./routes/users');
|
||||
var indexRouter = require("./routes/index");
|
||||
var usersRouter = require("./routes/users");
|
||||
var shippingDockRouter = require("./routes/shipping_dock");
|
||||
var orderRouter = require("./routes/order");
|
||||
var transactionRouter = require("./routes/transaction");
|
||||
|
||||
const db = require("./models");
|
||||
var cors = require("cors");
|
||||
|
||||
// Swagger configuration
|
||||
const swaggerOptions = {
|
||||
definition: {
|
||||
openapi: "3.0.0",
|
||||
info: {
|
||||
title: "Day 1 API Documentation",
|
||||
version: "1.0.0",
|
||||
description:
|
||||
"API documentation for Shipping Dock, Order, and Transaction management",
|
||||
},
|
||||
servers: [
|
||||
{
|
||||
url: "http://localhost:3000",
|
||||
description: "Development server",
|
||||
},
|
||||
],
|
||||
components: {
|
||||
schemas: {
|
||||
ShippingDock: {
|
||||
type: "object",
|
||||
properties: {
|
||||
id: {
|
||||
type: "integer",
|
||||
description: "Auto-generated ID",
|
||||
},
|
||||
name: {
|
||||
type: "string",
|
||||
description: "Name of the shipping dock",
|
||||
},
|
||||
status: {
|
||||
type: "integer",
|
||||
description: "Status: 1 = active, 0 = inactive",
|
||||
enum: [0, 1],
|
||||
},
|
||||
created_at: { type: "string", format: "date-time" },
|
||||
updated_at: { type: "string", format: "date-time" },
|
||||
},
|
||||
},
|
||||
Order: {
|
||||
type: "object",
|
||||
properties: {
|
||||
id: {
|
||||
type: "integer",
|
||||
description: "Auto-generated ID",
|
||||
},
|
||||
user_id: { type: "integer", description: "User ID" },
|
||||
amount: {
|
||||
type: "number",
|
||||
format: "decimal",
|
||||
description: "Order amount",
|
||||
},
|
||||
tax: {
|
||||
type: "number",
|
||||
format: "decimal",
|
||||
description: "Tax amount",
|
||||
},
|
||||
notes: {
|
||||
type: "string",
|
||||
description: "Additional notes",
|
||||
},
|
||||
status: {
|
||||
type: "integer",
|
||||
description: "Status: 1 = paid, 0 = not paid",
|
||||
enum: [0, 1],
|
||||
},
|
||||
created_at: { type: "string", format: "date-time" },
|
||||
updated_at: { type: "string", format: "date-time" },
|
||||
},
|
||||
},
|
||||
Transaction: {
|
||||
type: "object",
|
||||
properties: {
|
||||
id: {
|
||||
type: "integer",
|
||||
description: "Auto-generated ID",
|
||||
},
|
||||
order_id: { type: "integer", description: "Order ID" },
|
||||
user_id: { type: "integer", description: "User ID" },
|
||||
shipping_dock_id: {
|
||||
type: "integer",
|
||||
description: "Shipping Dock ID",
|
||||
},
|
||||
amount: {
|
||||
type: "number",
|
||||
format: "decimal",
|
||||
description: "Transaction amount",
|
||||
},
|
||||
notes: {
|
||||
type: "string",
|
||||
description: "Additional notes",
|
||||
},
|
||||
created_at: { type: "string", format: "date-time" },
|
||||
updated_at: { type: "string", format: "date-time" },
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
apis: ["./routes/*.js"],
|
||||
};
|
||||
|
||||
const swaggerSpec = swaggerJsdoc(swaggerOptions);
|
||||
|
||||
var app = express();
|
||||
app.set("db", db);
|
||||
// view engine setup
|
||||
app.set('views', path.join(__dirname, 'views'));
|
||||
app.set('view engine', 'jade');
|
||||
app.set("views", path.join(__dirname, "views"));
|
||||
app.set("view engine", "jade");
|
||||
app.use(cors());
|
||||
app.use(logger('dev'));
|
||||
app.use(logger("dev"));
|
||||
app.use(express.json());
|
||||
app.use(express.urlencoded({ extended: false }));
|
||||
app.use(cookieParser());
|
||||
app.use(express.static(path.join(__dirname, 'public')));
|
||||
app.use(express.static(path.join(__dirname, "public")));
|
||||
|
||||
app.use('/', indexRouter);
|
||||
app.use('/users', usersRouter);
|
||||
// Swagger docs route
|
||||
app.use("/docs", swaggerUi.serve, swaggerUi.setup(swaggerSpec));
|
||||
|
||||
app.use("/", indexRouter);
|
||||
app.use("/users", usersRouter);
|
||||
app.use("/api/v1/shipping_dock", shippingDockRouter);
|
||||
app.use("/api/v1/order", orderRouter);
|
||||
app.use("/api/v1/transaction", transactionRouter);
|
||||
|
||||
// catch 404 and forward to error handler
|
||||
app.use(function (req, res, next) {
|
||||
next(createError(404));
|
||||
next(createError(404));
|
||||
});
|
||||
|
||||
// error handler
|
||||
app.use(function (err, req, res, next) {
|
||||
// set locals, only providing error in development
|
||||
res.locals.message = err.message;
|
||||
res.locals.error = req.app.get('env') === 'development' ? err : {};
|
||||
// set locals, only providing error in development
|
||||
res.locals.message = err.message;
|
||||
res.locals.error = req.app.get("env") === "development" ? err : {};
|
||||
|
||||
// render the error page
|
||||
res.status(err.status || 500);
|
||||
res.render('error');
|
||||
// render the error page
|
||||
res.status(err.status || 500);
|
||||
res.render("error");
|
||||
});
|
||||
|
||||
module.exports = app;
|
||||
|
||||
Binary file not shown.
+38
-44
@@ -1,4 +1,4 @@
|
||||
'use strict';
|
||||
"use strict";
|
||||
/*Powered By: Manaknightdigital Inc. https://manaknightdigital.com/ Year: 2020*/
|
||||
/**
|
||||
* Sequelize File
|
||||
@@ -8,59 +8,53 @@
|
||||
* @author Ryan Wong
|
||||
*
|
||||
*/
|
||||
const fs = require('fs');
|
||||
const path = require('path');
|
||||
let Sequelize = require('sequelize');
|
||||
const fs = require("fs");
|
||||
const path = require("path");
|
||||
let Sequelize = require("sequelize");
|
||||
const basename = path.basename(__filename);
|
||||
const { DataTypes } = require('sequelize');
|
||||
const config = {
|
||||
DB_DATABASE: 'mysql',
|
||||
DB_USERNAME: 'root',
|
||||
DB_PASSWORD: 'root',
|
||||
DB_ADAPTER: 'mysql',
|
||||
DB_NAME: 'day_1',
|
||||
DB_HOSTNAME: 'localhost',
|
||||
DB_PORT: 3306,
|
||||
};
|
||||
const { DataTypes } = require("sequelize");
|
||||
|
||||
let db = {};
|
||||
|
||||
let sequelize = new Sequelize(config.DB_DATABASE, config.DB_USERNAME, config.DB_PASSWORD, {
|
||||
dialect: config.DB_ADAPTER,
|
||||
username: config.DB_USERNAME,
|
||||
password: config.DB_PASSWORD,
|
||||
database: config.DB_NAME,
|
||||
host: config.DB_HOSTNAME,
|
||||
port: config.DB_PORT,
|
||||
logging: console.log,
|
||||
timezone: '-04:00',
|
||||
pool: {
|
||||
maxConnections: 1,
|
||||
minConnections: 0,
|
||||
maxIdleTime: 100,
|
||||
},
|
||||
define: {
|
||||
timestamps: false,
|
||||
underscoredAll: true,
|
||||
underscored: true,
|
||||
},
|
||||
// SQLite configuration - stores data in a file instead of MySQL server
|
||||
let sequelize = new Sequelize({
|
||||
dialect: "sqlite",
|
||||
storage: "./database.sqlite", // Database file location
|
||||
logging: console.log,
|
||||
define: {
|
||||
timestamps: false,
|
||||
underscoredAll: true,
|
||||
underscored: true,
|
||||
},
|
||||
});
|
||||
|
||||
// sequelize.sync({ force: true });
|
||||
// Sync database tables
|
||||
sequelize
|
||||
.sync({ alter: true })
|
||||
.then(() => {
|
||||
console.log("Database tables synchronized");
|
||||
})
|
||||
.catch((err) => {
|
||||
console.error("Error synchronizing database:", err);
|
||||
});
|
||||
|
||||
fs.readdirSync(__dirname)
|
||||
.filter((file) => {
|
||||
return file.indexOf('.') !== 0 && file !== basename && file.slice(-3) === '.js';
|
||||
})
|
||||
.forEach((file) => {
|
||||
var model = require(path.join(__dirname, file))(sequelize, DataTypes);
|
||||
db[model.name] = model;
|
||||
});
|
||||
.filter((file) => {
|
||||
return (
|
||||
file.indexOf(".") !== 0 &&
|
||||
file !== basename &&
|
||||
file.slice(-3) === ".js"
|
||||
);
|
||||
})
|
||||
.forEach((file) => {
|
||||
var model = require(path.join(__dirname, file))(sequelize, DataTypes);
|
||||
db[model.name] = model;
|
||||
});
|
||||
|
||||
Object.keys(db).forEach((modelName) => {
|
||||
if (db[modelName].associate) {
|
||||
db[modelName].associate(db);
|
||||
}
|
||||
if (db[modelName].associate) {
|
||||
db[modelName].associate(db);
|
||||
}
|
||||
});
|
||||
|
||||
db.sequelize = sequelize;
|
||||
|
||||
@@ -0,0 +1,45 @@
|
||||
module.exports = (sequelize, DataTypes) => {
|
||||
const order = sequelize.define(
|
||||
"order",
|
||||
{
|
||||
id: {
|
||||
type: DataTypes.INTEGER,
|
||||
primaryKey: true,
|
||||
autoIncrement: true,
|
||||
},
|
||||
user_id: {
|
||||
type: DataTypes.INTEGER,
|
||||
allowNull: false,
|
||||
},
|
||||
amount: {
|
||||
type: DataTypes.DECIMAL(10, 2),
|
||||
allowNull: false,
|
||||
},
|
||||
tax: {
|
||||
type: DataTypes.DECIMAL(10, 2),
|
||||
allowNull: false,
|
||||
},
|
||||
notes: {
|
||||
type: DataTypes.TEXT,
|
||||
allowNull: true,
|
||||
},
|
||||
status: {
|
||||
type: DataTypes.INTEGER,
|
||||
allowNull: false,
|
||||
defaultValue: 0,
|
||||
comment: "1: paid, 0: not paid",
|
||||
},
|
||||
created_at: DataTypes.DATE,
|
||||
updated_at: DataTypes.DATE,
|
||||
},
|
||||
{
|
||||
timestamps: true,
|
||||
freezeTableName: true,
|
||||
tableName: "order",
|
||||
createdAt: "created_at",
|
||||
updatedAt: "updated_at",
|
||||
}
|
||||
);
|
||||
|
||||
return order;
|
||||
};
|
||||
@@ -0,0 +1,33 @@
|
||||
module.exports = (sequelize, DataTypes) => {
|
||||
const shipping_dock = sequelize.define(
|
||||
"shipping_dock",
|
||||
{
|
||||
id: {
|
||||
type: DataTypes.INTEGER,
|
||||
primaryKey: true,
|
||||
autoIncrement: true,
|
||||
},
|
||||
name: {
|
||||
type: DataTypes.STRING,
|
||||
allowNull: false,
|
||||
},
|
||||
status: {
|
||||
type: DataTypes.INTEGER,
|
||||
allowNull: false,
|
||||
defaultValue: 1,
|
||||
comment: "1: active, 0: inactive",
|
||||
},
|
||||
created_at: DataTypes.DATE,
|
||||
updated_at: DataTypes.DATE,
|
||||
},
|
||||
{
|
||||
timestamps: true,
|
||||
freezeTableName: true,
|
||||
tableName: "shipping_dock",
|
||||
createdAt: "created_at",
|
||||
updatedAt: "updated_at",
|
||||
}
|
||||
);
|
||||
|
||||
return shipping_dock;
|
||||
};
|
||||
@@ -0,0 +1,43 @@
|
||||
module.exports = (sequelize, DataTypes) => {
|
||||
const transaction = sequelize.define(
|
||||
"transaction",
|
||||
{
|
||||
id: {
|
||||
type: DataTypes.INTEGER,
|
||||
primaryKey: true,
|
||||
autoIncrement: true,
|
||||
},
|
||||
order_id: {
|
||||
type: DataTypes.INTEGER,
|
||||
allowNull: false,
|
||||
},
|
||||
user_id: {
|
||||
type: DataTypes.INTEGER,
|
||||
allowNull: false,
|
||||
},
|
||||
shipping_dock_id: {
|
||||
type: DataTypes.INTEGER,
|
||||
allowNull: false,
|
||||
},
|
||||
amount: {
|
||||
type: DataTypes.DECIMAL(10, 2),
|
||||
allowNull: false,
|
||||
},
|
||||
notes: {
|
||||
type: DataTypes.TEXT,
|
||||
allowNull: true,
|
||||
},
|
||||
created_at: DataTypes.DATE,
|
||||
updated_at: DataTypes.DATE,
|
||||
},
|
||||
{
|
||||
timestamps: true,
|
||||
freezeTableName: true,
|
||||
tableName: "transaction",
|
||||
createdAt: "created_at",
|
||||
updatedAt: "updated_at",
|
||||
}
|
||||
);
|
||||
|
||||
return transaction;
|
||||
};
|
||||
+4
-1
@@ -14,6 +14,9 @@
|
||||
"jade": "~1.11.0",
|
||||
"morgan": "~1.9.1",
|
||||
"mysql2": "^2.3.3",
|
||||
"sequelize": "^6.15.1"
|
||||
"sequelize": "^6.15.1",
|
||||
"sqlite3": "^5.1.7",
|
||||
"swagger-jsdoc": "^6.2.8",
|
||||
"swagger-ui-express": "^5.0.1"
|
||||
}
|
||||
}
|
||||
|
||||
Generated
+2245
File diff suppressed because it is too large
Load Diff
@@ -0,0 +1,2 @@
|
||||
ignoredBuiltDependencies:
|
||||
- sqlite3
|
||||
@@ -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;
|
||||
@@ -0,0 +1,257 @@
|
||||
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;
|
||||
@@ -0,0 +1,262 @@
|
||||
var express = require("express");
|
||||
var router = express.Router();
|
||||
|
||||
/**
|
||||
* @swagger
|
||||
* /api/v1/transaction:
|
||||
* get:
|
||||
* summary: Get all transactions
|
||||
* tags: [Transaction]
|
||||
* responses:
|
||||
* 200:
|
||||
* description: List of all transactions
|
||||
* content:
|
||||
* application/json:
|
||||
* schema:
|
||||
* type: object
|
||||
* properties:
|
||||
* success:
|
||||
* type: boolean
|
||||
* data:
|
||||
* type: array
|
||||
* items:
|
||||
* $ref: '#/components/schemas/Transaction'
|
||||
*/
|
||||
// GET all transactions
|
||||
router.get("/", async function (req, res, next) {
|
||||
try {
|
||||
const db = req.app.get("db");
|
||||
const transactions = await db.transaction.findAll();
|
||||
res.json({ success: true, data: transactions });
|
||||
} catch (error) {
|
||||
console.error(error);
|
||||
res.status(500).json({
|
||||
success: false,
|
||||
message: "Error fetching transactions",
|
||||
error: error.message,
|
||||
});
|
||||
}
|
||||
});
|
||||
|
||||
/**
|
||||
* @swagger
|
||||
* /api/v1/transaction/{id}:
|
||||
* get:
|
||||
* summary: Get one transaction by ID
|
||||
* tags: [Transaction]
|
||||
* parameters:
|
||||
* - in: path
|
||||
* name: id
|
||||
* required: true
|
||||
* schema:
|
||||
* type: integer
|
||||
* responses:
|
||||
* 200:
|
||||
* description: Transaction details
|
||||
* 404:
|
||||
* description: Transaction not found
|
||||
*/
|
||||
// GET one transaction by id
|
||||
router.get("/:id", async function (req, res, next) {
|
||||
try {
|
||||
const db = req.app.get("db");
|
||||
const transaction = await db.transaction.findByPk(req.params.id);
|
||||
|
||||
if (!transaction) {
|
||||
return res
|
||||
.status(404)
|
||||
.json({ success: false, message: "Transaction not found" });
|
||||
}
|
||||
|
||||
res.json({ success: true, data: transaction });
|
||||
} catch (error) {
|
||||
console.error(error);
|
||||
res.status(500).json({
|
||||
success: false,
|
||||
message: "Error fetching transaction",
|
||||
error: error.message,
|
||||
});
|
||||
}
|
||||
});
|
||||
|
||||
/**
|
||||
* @swagger
|
||||
* /api/v1/transaction:
|
||||
* post:
|
||||
* summary: Create a new transaction
|
||||
* tags: [Transaction]
|
||||
* requestBody:
|
||||
* required: true
|
||||
* content:
|
||||
* application/json:
|
||||
* schema:
|
||||
* type: object
|
||||
* required:
|
||||
* - order_id
|
||||
* - user_id
|
||||
* - shipping_dock_id
|
||||
* - amount
|
||||
* properties:
|
||||
* order_id:
|
||||
* type: integer
|
||||
* user_id:
|
||||
* type: integer
|
||||
* shipping_dock_id:
|
||||
* type: integer
|
||||
* amount:
|
||||
* type: number
|
||||
* format: decimal
|
||||
* notes:
|
||||
* type: string
|
||||
* responses:
|
||||
* 201:
|
||||
* description: Transaction created successfully
|
||||
* 400:
|
||||
* description: Bad request
|
||||
*/
|
||||
// POST - Create a new transaction
|
||||
router.post("/", async function (req, res, next) {
|
||||
try {
|
||||
const db = req.app.get("db");
|
||||
const { order_id, user_id, shipping_dock_id, amount, notes } = req.body;
|
||||
|
||||
if (!order_id || !user_id || !shipping_dock_id || !amount) {
|
||||
return res.status(400).json({
|
||||
success: false,
|
||||
message:
|
||||
"order_id, user_id, shipping_dock_id, and amount are required",
|
||||
});
|
||||
}
|
||||
|
||||
const transaction = await db.transaction.create({
|
||||
order_id,
|
||||
user_id,
|
||||
shipping_dock_id,
|
||||
amount,
|
||||
notes: notes || null,
|
||||
});
|
||||
|
||||
res.status(201).json({ success: true, data: transaction });
|
||||
} catch (error) {
|
||||
console.error(error);
|
||||
res.status(500).json({
|
||||
success: false,
|
||||
message: "Error creating transaction",
|
||||
error: error.message,
|
||||
});
|
||||
}
|
||||
});
|
||||
|
||||
/**
|
||||
* @swagger
|
||||
* /api/v1/transaction/{id}:
|
||||
* put:
|
||||
* summary: Update a transaction by ID
|
||||
* tags: [Transaction]
|
||||
* parameters:
|
||||
* - in: path
|
||||
* name: id
|
||||
* required: true
|
||||
* schema:
|
||||
* type: integer
|
||||
* requestBody:
|
||||
* content:
|
||||
* application/json:
|
||||
* schema:
|
||||
* type: object
|
||||
* properties:
|
||||
* order_id:
|
||||
* type: integer
|
||||
* user_id:
|
||||
* type: integer
|
||||
* shipping_dock_id:
|
||||
* type: integer
|
||||
* amount:
|
||||
* type: number
|
||||
* notes:
|
||||
* type: string
|
||||
* responses:
|
||||
* 200:
|
||||
* description: Transaction updated successfully
|
||||
* 404:
|
||||
* description: Transaction not found
|
||||
* delete:
|
||||
* summary: Delete a transaction by ID
|
||||
* tags: [Transaction]
|
||||
* parameters:
|
||||
* - in: path
|
||||
* name: id
|
||||
* required: true
|
||||
* schema:
|
||||
* type: integer
|
||||
* responses:
|
||||
* 200:
|
||||
* description: Transaction deleted successfully
|
||||
* 404:
|
||||
* description: Transaction not found
|
||||
*/
|
||||
// PUT - Update a transaction by id
|
||||
router.put("/:id", async function (req, res, next) {
|
||||
try {
|
||||
const db = req.app.get("db");
|
||||
const { order_id, user_id, shipping_dock_id, amount, notes } = req.body;
|
||||
|
||||
const transaction = await db.transaction.findByPk(req.params.id);
|
||||
|
||||
if (!transaction) {
|
||||
return res
|
||||
.status(404)
|
||||
.json({ success: false, message: "Transaction not found" });
|
||||
}
|
||||
|
||||
await transaction.update({
|
||||
order_id: order_id !== undefined ? order_id : transaction.order_id,
|
||||
user_id: user_id !== undefined ? user_id : transaction.user_id,
|
||||
shipping_dock_id:
|
||||
shipping_dock_id !== undefined
|
||||
? shipping_dock_id
|
||||
: transaction.shipping_dock_id,
|
||||
amount: amount !== undefined ? amount : transaction.amount,
|
||||
notes: notes !== undefined ? notes : transaction.notes,
|
||||
});
|
||||
|
||||
res.json({ success: true, data: transaction });
|
||||
} catch (error) {
|
||||
console.error(error);
|
||||
res.status(500).json({
|
||||
success: false,
|
||||
message: "Error updating transaction",
|
||||
error: error.message,
|
||||
});
|
||||
}
|
||||
});
|
||||
|
||||
// DELETE - Delete a transaction by id
|
||||
router.delete("/:id", async function (req, res, next) {
|
||||
try {
|
||||
const db = req.app.get("db");
|
||||
const transaction = await db.transaction.findByPk(req.params.id);
|
||||
|
||||
if (!transaction) {
|
||||
return res
|
||||
.status(404)
|
||||
.json({ success: false, message: "Transaction not found" });
|
||||
}
|
||||
|
||||
await transaction.destroy();
|
||||
|
||||
res.json({
|
||||
success: true,
|
||||
message: "Transaction deleted successfully",
|
||||
});
|
||||
} catch (error) {
|
||||
console.error(error);
|
||||
res.status(500).json({
|
||||
success: false,
|
||||
message: "Error deleting transaction",
|
||||
error: error.message,
|
||||
});
|
||||
}
|
||||
});
|
||||
|
||||
module.exports = router;
|
||||
Reference in New Issue
Block a user