-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
151 lines (128 loc) · 3.65 KB
/
Copy pathserver.js
File metadata and controls
151 lines (128 loc) · 3.65 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
const express = require("express");
const cors = require("cors");
const fs = require("fs").promises;
const path = require("path");
const app = express();
const port = process.env.PORT || 3000;
const DB_PATH = path.join(__dirname, "db.json");
app.use(cors());
app.use(express.json());
async function readDB() {
const data = await fs.readFile(DB_PATH, "utf8");
return JSON.parse(data);
}
async function writeDB(data) {
await fs.writeFile(DB_PATH, JSON.stringify(data, null, 2));
}
app.get("/", (req, res) => {
res.json({
status: "OK",
message: "SpicX API is running",
endpoints: {
data: "/data",
products: "/products",
categories: "/categories",
users: "/users",
carts: "/carts",
},
});
});
app.get("/data", async (req, res) => {
try {
const db = await readDB();
res.json(db);
} catch (error) {
res.status(500).json({ error: "Internal server error" });
}
});
app.get("/products", async (req, res) => {
try {
const db = await readDB();
res.json(db.products || []);
} catch (error) {
res.status(500).json({ error: "Internal server error" });
}
});
app.get("/products/:id", async (req, res) => {
try {
const db = await readDB();
const product = db.products.find((p) => p.id === parseInt(req.params.id));
product ? res.json(product) : res.status(404).json({ message: "Not found" });
} catch (error) {
res.status(500).json({ error: "Internal server error" });
}
});
app.get("/categories", async (req, res) => {
try {
const db = await readDB();
res.json(db.categories || []);
} catch (error) {
res.status(500).json({ error: "Internal server error" });
}
});
app.get("/users", async (req, res) => {
try {
const db = await readDB();
res.json(db.users || []);
} catch (error) {
res.status(500).json({ error: "Internal server error" });
}
});
app.post("/users", async (req, res) => {
try {
const db = await readDB();
const newUser = {
id: Date.now(),
...req.body,
role: "user",
isActive: true,
createdAt: new Date().toISOString(),
};
if (!db.users) db.users = [];
const existing = db.users.find(u => u.email === newUser.email);
if (existing) {
return res.status(400).json({ error: "Email already registered" });
}
db.users.push(newUser);
await writeDB(db);
const { password, ...userWithoutPassword } = newUser;
res.status(201).json(userWithoutPassword);
} catch (error) {
console.error("Error creating user:", error);
res.status(500).json({ error: "Internal server error" });
}
});
app.get("/carts", async (req, res) => {
try {
const { userId } = req.query;
if (!userId) return res.status(400).json({ error: "userId required" });
const db = await readDB();
if (!db.carts) db.carts = [];
const userCart = db.carts.find(c => c.userId == userId);
res.json(userCart ? userCart.items : []);
} catch (error) {
res.status(500).json({ error: "Internal server error" });
}
});
app.post("/carts", async (req, res) => {
try {
const { userId, items } = req.body;
if (!userId) return res.status(400).json({ error: "userId required" });
const db = await readDB();
if (!db.carts) db.carts = [];
const index = db.carts.findIndex(c => c.userId == userId);
if (index >= 0) {
db.carts[index].items = items;
} else {
db.carts.push({ userId, items });
}
await writeDB(db);
res.json({ userId, items });
} catch (error) {
console.error("Error saving cart:", error);
res.status(500).json({ error: "Internal server error" });
}
});
app.listen(port, () => {
console.log(`✅ API running on port ${port}`);
});