feat: complete day 2
This commit is contained in:
@@ -0,0 +1,55 @@
|
||||
const express = require("express");
|
||||
const router = express.Router();
|
||||
const db = require("../models");
|
||||
const PaginationService = require("../services/PaginationService");
|
||||
const ReportService = require("../services/ReportService");
|
||||
|
||||
// GET /api/v1/order/odd - Return all odd order_id rows
|
||||
router.get("/odd", async (req, res) => {
|
||||
try {
|
||||
const oddOrders = await PaginationService.getOddOrders(db.order);
|
||||
res.json(oddOrders);
|
||||
} catch (error) {
|
||||
res.status(500).json({ error: error.message });
|
||||
}
|
||||
});
|
||||
|
||||
// GET /api/v1/order - Paginate orders with optional sorting
|
||||
router.get("/", async (req, res) => {
|
||||
try {
|
||||
const { page = 1, limit = 10, sort = "id", direction = "ASC" } = req.query;
|
||||
|
||||
const result = await PaginationService.paginateWithSort(db.order, {
|
||||
page,
|
||||
limit,
|
||||
sort,
|
||||
direction,
|
||||
});
|
||||
|
||||
res.json(result);
|
||||
} catch (error) {
|
||||
res.status(500).json({ error: error.message });
|
||||
}
|
||||
});
|
||||
|
||||
// GET /api/v1/order/cursor - Cursor-based pagination
|
||||
router.get("/cursor", async (req, res) => {
|
||||
try {
|
||||
const { id, limit = 10 } = req.query;
|
||||
|
||||
if (!id) {
|
||||
return res.status(400).json({ error: "id parameter is required" });
|
||||
}
|
||||
|
||||
const result = await PaginationService.paginateWithCursor(db.order, {
|
||||
id,
|
||||
limit,
|
||||
});
|
||||
|
||||
res.json(result);
|
||||
} catch (error) {
|
||||
res.status(500).json({ error: error.message });
|
||||
}
|
||||
});
|
||||
|
||||
module.exports = router;
|
||||
@@ -0,0 +1,93 @@
|
||||
const express = require("express");
|
||||
const router = express.Router();
|
||||
const db = require("../models");
|
||||
const ReportService = require("../services/ReportService");
|
||||
|
||||
// GET /api/v1/report/sale?month=1&year=2022
|
||||
router.get("/sale", async (req, res) => {
|
||||
try {
|
||||
const { month, year, from_date, to_date } = req.query;
|
||||
if (month && year) {
|
||||
const result = await ReportService.getMonthlySales(db.order, month, year);
|
||||
return res.json(result);
|
||||
} else if (from_date && to_date) {
|
||||
const result = await ReportService.getSalesByDateRange(
|
||||
db.order,
|
||||
from_date,
|
||||
to_date
|
||||
);
|
||||
return res.json(result);
|
||||
} else {
|
||||
return res.status(400).json({ error: "Missing required parameters" });
|
||||
}
|
||||
} catch (error) {
|
||||
res.status(500).json({ error: error.message });
|
||||
}
|
||||
});
|
||||
|
||||
// GET /api/v1/report/monthly?year=2022
|
||||
router.get("/monthly", async (req, res) => {
|
||||
try {
|
||||
const { year } = req.query;
|
||||
if (!year) return res.status(400).json({ error: "year is required" });
|
||||
const result = await ReportService.getMonthlySalesByYear(db.order, year);
|
||||
res.json(result);
|
||||
} catch (error) {
|
||||
res.status(500).json({ error: error.message });
|
||||
}
|
||||
});
|
||||
|
||||
// GET /api/v1/report/user?year=2022&user_id=1
|
||||
router.get("/user", async (req, res) => {
|
||||
try {
|
||||
const { year, user_id } = req.query;
|
||||
if (!year || !user_id)
|
||||
return res.status(400).json({ error: "year and user_id are required" });
|
||||
const result = await ReportService.getMonthlySalesByUser(
|
||||
db.order,
|
||||
year,
|
||||
user_id
|
||||
);
|
||||
res.json(result);
|
||||
} catch (error) {
|
||||
res.status(500).json({ error: error.message });
|
||||
}
|
||||
});
|
||||
|
||||
// GET /api/v1/report/shipping_dock?year=2022&shipping_dock_id=1
|
||||
router.get("/shipping_dock", async (req, res) => {
|
||||
try {
|
||||
const { year, shipping_dock_id } = req.query;
|
||||
if (!year || !shipping_dock_id)
|
||||
return res
|
||||
.status(400)
|
||||
.json({ error: "year and shipping_dock_id are required" });
|
||||
const result = await ReportService.getMonthlySalesByShippingDock(
|
||||
db.order,
|
||||
year,
|
||||
shipping_dock_id
|
||||
);
|
||||
res.json(result);
|
||||
} catch (error) {
|
||||
res.status(500).json({ error: error.message });
|
||||
}
|
||||
});
|
||||
|
||||
// GET /api/v1/report/user/count?year=2022&user_id=1
|
||||
router.get("/user/count", async (req, res) => {
|
||||
try {
|
||||
const { year, user_id } = req.query;
|
||||
if (!year || !user_id)
|
||||
return res.status(400).json({ error: "year and user_id are required" });
|
||||
const result = await ReportService.getOrderCountByUser(
|
||||
db.order,
|
||||
year,
|
||||
user_id
|
||||
);
|
||||
res.json(result);
|
||||
} catch (error) {
|
||||
res.status(500).json({ error: error.message });
|
||||
}
|
||||
});
|
||||
|
||||
module.exports = router;
|
||||
Reference in New Issue
Block a user