-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
212 lines (195 loc) · 6.77 KB
/
server.js
File metadata and controls
212 lines (195 loc) · 6.77 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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
const express = require('express');
const cors = require('cors');
const fs = require('fs');
const path = require('path');
const { exec } = require('child_process');
const app = express();
const PORT = 3000;
const DATA_FILE = path.join(__dirname, 'salonClients.json');
// Middleware
app.use(cors());
app.use(express.json());
app.use(express.static(__dirname));
// Funkcja do wczytania danych z pliku
function loadClients() {
try {
if (fs.existsSync(DATA_FILE)) {
const data = fs.readFileSync(DATA_FILE, 'utf-8');
return JSON.parse(data);
}
return [];
} catch (error) {
console.error('Błąd przy wczytywaniu danych:', error);
return [];
}
}
// Funkcja do zapisania danych do pliku
function saveClients(clients) {
try {
fs.writeFileSync(DATA_FILE, JSON.stringify(clients, null, 2), 'utf-8');
console.log('Dane zapisane do pliku');
} catch (error) {
console.error('Błąd przy zapisywaniu danych:', error);
}
}
// GET - Pobierz wszystkich klientów
app.get('/api/clients', (req, res) => {
const clients = loadClients();
res.json(clients);
});
// POST - Dodaj nowego klienta
app.post('/api/clients', (req, res) => {
const clients = loadClients();
const newClient = {
id: Date.now(),
name: req.body.name,
phone: req.body.phone,
email: req.body.email,
notes: req.body.notes || '',
services: [],
payments: [],
createdAt: new Date().toISOString()
};
clients.push(newClient);
saveClients(clients);
res.json(newClient);
});
// GET - Pobierz konkretnego klienta
app.get('/api/clients/:id', (req, res) => {
const clients = loadClients();
const client = clients.find(c => c.id === parseInt(req.params.id));
if (client) {
res.json(client);
} else {
res.status(404).json({ error: 'Klient nie znaleziony' });
}
});
// PUT - Aktualizuj klienta
app.put('/api/clients/:id', (req, res) => {
const clients = loadClients();
const client = clients.find(c => c.id === parseInt(req.params.id));
if (client) {
client.name = req.body.name || client.name;
client.phone = req.body.phone || client.phone;
client.email = req.body.email || client.email;
client.notes = req.body.notes !== undefined ? req.body.notes : client.notes;
saveClients(clients);
res.json(client);
} else {
res.status(404).json({ error: 'Klient nie znaleziony' });
}
});
// DELETE - Usuń klienta
app.delete('/api/clients/:id', (req, res) => {
let clients = loadClients();
const index = clients.findIndex(c => c.id === parseInt(req.params.id));
if (index !== -1) {
const deletedClient = clients[index];
clients.splice(index, 1);
saveClients(clients);
res.json({ message: 'Klient usunięty', client: deletedClient });
} else {
res.status(404).json({ error: 'Klient nie znaleziony' });
}
});
// POST - Dodaj usługę do klienta
app.post('/api/clients/:id/services', (req, res) => {
const clients = loadClients();
const client = clients.find(c => c.id === parseInt(req.params.id));
if (client) {
client.services.push({
type: req.body.type,
date: req.body.date,
price: req.body.price,
notes: req.body.notes || ''
});
saveClients(clients);
res.json(client);
} else {
res.status(404).json({ error: 'Klient nie znaleziony' });
}
});
// PUT - Aktualizuj usługę klienta
app.put('/api/clients/:id/services/:serviceIndex', (req, res) => {
const clients = loadClients();
const client = clients.find(c => c.id === parseInt(req.params.id));
if (client && client.services[req.params.serviceIndex]) {
client.services[req.params.serviceIndex] = {
type: req.body.type,
date: req.body.date,
price: req.body.price,
notes: req.body.notes || ''
};
saveClients(clients);
res.json(client);
} else {
res.status(404).json({ error: 'Usługa nie znaleziona' });
}
});
// DELETE - Usuń usługę klienta
app.delete('/api/clients/:id/services/:serviceIndex', (req, res) => {
const clients = loadClients();
const client = clients.find(c => c.id === parseInt(req.params.id));
if (client && client.services[req.params.serviceIndex]) {
client.services.splice(req.params.serviceIndex, 1);
saveClients(clients);
res.json(client);
} else {
res.status(404).json({ error: 'Usługa nie znaleziona' });
}
});
// POST - Dodaj płatność do klienta
app.post('/api/clients/:id/payments', (req, res) => {
const clients = loadClients();
const client = clients.find(c => c.id === parseInt(req.params.id));
if (client) {
if (!client.payments) client.payments = [];
client.payments.push({
amount: req.body.amount,
date: req.body.date,
method: req.body.method
});
saveClients(clients);
res.json(client);
} else {
res.status(404).json({ error: 'Klient nie znaleziony' });
}
});
// POST - Export wszystkich danych
app.get('/api/export', (req, res) => {
const clients = loadClients();
res.json(clients);
});
// POST - Import danych
app.post('/api/import', (req, res) => {
try {
saveClients(req.body);
res.json({ message: 'Dane zaimportowane pomyślnie' });
} catch (error) {
res.status(400).json({ error: 'Błąd przy imporcie danych' });
}
});
app.listen(PORT, () => {
console.log(`
╔════════════════════════════════════════════════════════╗
║ 🚀 StylistPRO - Server Running 🚀 ║
║ ║
║ 🇵🇱 Serwer Salon Fryzjerski uruchomiony ║
║ Dane przechowywane w: salonClients.json ║
║ ║
║ 🇬🇧 Hair Salon Management System active ║
║ Data stored in: salonClients.json ║
║ ║
║ 📱 Opening http://localhost:${PORT}... ║
╚════════════════════════════════════════════════════════╝
`);
// Otwórz przeglądarkę / Open browser
const url = `http://localhost:${PORT}`;
if (process.platform === 'win32') {
exec(`start ${url}`);
} else if (process.platform === 'darwin') {
exec(`open ${url}`);
} else {
exec(`xdg-open ${url}`);
}
});