-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
191 lines (164 loc) · 5.33 KB
/
server.js
File metadata and controls
191 lines (164 loc) · 5.33 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
import express from 'express';
import cors from 'cors';
import sequelize from './config/database.js';
import { Product } from './models/index.js';
import process from 'process';
const app = express();
const PORT = process.env.PORT || 3000;
app.use(cors(
{
origin : 'http://localhost:8080'
}
));
app.use(express.json());
const initializeDatabase = async() => {
try {
await sequelize.authenticate();
console.log('Database connected successfully');
}
catch (error) {
console.error('Unable to connect to database:', error);
}
};
// Handsontable endpoints
app.get('/api/products', async(req, res) => {
try {
const products = await Product.findAll({
order: [['index', 'ASC']]
});
res.status(200).json({ products });
}
catch (error) {
console.error({ error });
res.status(500).json({
success : false,
message : 'There was an error loading the products data.'
});
}
});
app.patch('/api/products/save', async(req, res) => {
try {
const { changes } = req.body;
if (!changes || changes.length === 0) {
return res.status(200).json({ success: true, message: 'No changes to save' });
}
// Process each change
for (const change of changes) {
const [row, prop, oldValue, newValue] = change;
// Get the product ID for this row (row index + 1 assuming sequential IDs)
const products = await Product.findAll({ order: [['id', 'ASC']] });
const product = products[row];
if (product) {
// Map column names to database fields
// Column 0: ID (hidden), Column 1: index
const columnMap = {
1: 'index',
2: 'companyName',
3: 'country',
4: 'productName',
5: 'sellDate',
6: 'orderId',
7: 'inStock',
8: 'qty'
};
const fieldName = columnMap[prop];
if (fieldName) {
await product.update({ [fieldName]: newValue });
}
}
}
res.status(200).json({ success: true, message: 'Data saved successfully' });
}
catch (error) {
console.error({ error });
res.status(500).json({
success : false,
message : 'There was an error saving the data.'
});
}
});
app.patch('/api/products/bulk-update', async(req, res) => {
try {
const { updates } = req.body;
if (!updates || updates.length === 0) {
return res.status(400).json({ success: false, message: 'No updates provided' });
}
// Use a transaction for atomicity
await sequelize.transaction(async (t) => {
// Update each product by ID
for (const update of updates) {
const { id, ...fields } = update;
await Product.update(fields, {
where: { id },
transaction: t
});
}
});
res.status(200).json({
success: true,
message: `Updated ${updates.length} product${updates.length > 1 ? 's' : ''}`,
updatedCount: updates.length
});
}
catch (error) {
console.error({ error });
res.status(500).json({
success : false,
message : 'There was an error updating the products.'
});
}
});
app.post('/api/products/create', async(req, res) => {
try {
const { products } = req.body;
if (!products || products.length === 0) {
return res.status(400).json({ success: false, message: 'No products provided' });
}
// Create new products
const createdProducts = await Product.bulkCreate(products);
res.status(201).json({
success: true,
message: `Created ${createdProducts.length} product${createdProducts.length > 1 ? 's' : ''}`,
products: createdProducts
});
}
catch (error) {
console.error({ error });
res.status(500).json({
success : false,
message : 'There was an error creating the products.'
});
}
});
app.delete('/api/products/delete', async(req, res) => {
try {
const { ids } = req.query;
if (!ids) {
return res.status(400).json({ success: false, message: 'No product IDs provided' });
}
// Parse IDs from comma-separated string
const idArray = ids.split(',').map(id => parseInt(id, 10));
// Delete products by their IDs
const deletedCount = await Product.destroy({
where: {
id: idArray
}
});
res.status(200).json({
success: true,
message: `Deleted ${deletedCount} product${deletedCount > 1 ? 's' : ''}`,
deletedCount
});
}
catch (error) {
console.error({ error });
res.status(500).json({
success : false,
message : 'There was an error deleting the products.'
});
}
});
app.listen(PORT, async() => {
await initializeDatabase();
console.log(`Server running on http://localhost:${PORT}`);
});