test
This commit is contained in:
parent
dae5a4603e
commit
d47807d6e0
49
server.js
49
server.js
@ -196,6 +196,55 @@ app.get("/api/item/:id", (req, res) => {
|
|||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
|
app.post("/api/shop/items/buy", (req, res) => {
|
||||||
|
const { shopId, address, items } = req.body;
|
||||||
|
|
||||||
|
// Basic validation
|
||||||
|
if (
|
||||||
|
typeof shopId !== "number" ||
|
||||||
|
typeof address !== "string" ||
|
||||||
|
!Array.isArray(items)
|
||||||
|
) {
|
||||||
|
return res.status(400).json({
|
||||||
|
ok: false,
|
||||||
|
error: "Invalid parameters",
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
for (const item of items) {
|
||||||
|
if (typeof item.id !== "string" || typeof item.quantity !== "number") {
|
||||||
|
return res.status(400).json({
|
||||||
|
ok: false,
|
||||||
|
error: "Invalid item format",
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Build request payload
|
||||||
|
const request = {
|
||||||
|
type: "request",
|
||||||
|
to: shopId,
|
||||||
|
address,
|
||||||
|
items: items.map((item) => ({
|
||||||
|
name: item.id,
|
||||||
|
_requestCount: item.quantity,
|
||||||
|
})),
|
||||||
|
};
|
||||||
|
|
||||||
|
// Send via WebSocket
|
||||||
|
wss.clients.forEach((client) => {
|
||||||
|
if (client.readyState === 1) {
|
||||||
|
client.send(JSON.stringify(request));
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
return res.status(200).json({
|
||||||
|
ok: true,
|
||||||
|
message: "Items sent successfully",
|
||||||
|
data: request,
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
// Serve the items UI page from public/items.html for the /items route.
|
// Serve the items UI page from public/items.html for the /items route.
|
||||||
// The actual HTML/JS for the page lives in public/items.html (moved from server-side rendering).
|
// The actual HTML/JS for the page lives in public/items.html (moved from server-side rendering).
|
||||||
app.get("/", (req, res) => {
|
app.get("/", (req, res) => {
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user