-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
73 lines (63 loc) · 1.92 KB
/
server.js
File metadata and controls
73 lines (63 loc) · 1.92 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
const express = require('express');
const app = express();
const bodyParser = require('body-parser');
const mysql = require('mysql');
const cors = require('cors');
const PORT = process.env.PORT || 3000;
// Middleware
app.use(bodyParser.json());
app.use(cors());
// Database connection
const db = mysql.createConnection({
host: 'localhost',
user: 'root',
password: 'yourpassword', // change this to your MySQL password
database: 'scims_db'
});
db.connect((err) => {
if (err) {
console.error('Error connecting to the database:', err);
} else {
console.log('Connected to the database');
}
});
// Route to test connection
app.get('/', (req, res) => {
res.send('SCIMS API is working!');
});
// Inventory routes
app.get('/inventory', (req, res) => {
db.query('SELECT * FROM inventory', (err, results) => {
if (err) throw err;
res.json(results);
});
});
app.post('/inventory', (req, res) => {
const { name, quantity, price } = req.body;
const query = 'INSERT INTO inventory (name, quantity, price) VALUES (?, ?, ?)';
db.query(query, [name, quantity, price], (err, results) => {
if (err) throw err;
res.json({ message: 'Product added successfully', productId: results.insertId });
});
});
app.put('/inventory/:id', (req, res) => {
const { id } = req.params;
const { name, quantity, price } = req.body;
const query = 'UPDATE inventory SET name = ?, quantity = ?, price = ? WHERE id = ?';
db.query(query, [name, quantity, price, id], (err, results) => {
if (err) throw err;
res.json({ message: 'Product updated successfully' });
});
});
app.delete('/inventory/:id', (req, res) => {
const { id } = req.params;
const query = 'DELETE FROM inventory WHERE id = ?';
db.query(query, [id], (err, results) => {
if (err) throw err;
res.json({ message: 'Product deleted successfully' });
});
});
// Start server
app.listen(PORT, () => {
console.log(`Server is running on port ${PORT}`);
});