Skip to content

Commit 936c094

Browse files
author
fazt
committed
first commit
1 parent 9c0a70a commit 936c094

File tree

3 files changed

+67
-0
lines changed

3 files changed

+67
-0
lines changed

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
"description": "",
55
"main": "index.js",
66
"scripts": {
7+
"start": "node src/server.js",
78
"test": "echo \"Error: no test specified\" && exit 1"
89
},
910
"keywords": [],

src/app/index.js

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,5 +59,41 @@ $('#create-form').on('submit', function() {
5959
$('#get-request').click();
6060
}
6161
});
62+
});
63+
64+
65+
$('table').on('click', '.update-button', function() {
66+
var rowElement = $(this).closest('tr');
67+
var id = rowElement.find('.id').text();
68+
var newName = rowElement.find('.name').val();
69+
70+
console.log(newName);
71+
72+
$.ajax({
73+
url: '/products/' + id,
74+
method: 'PUT',
75+
contenType: 'application/json',
76+
data: {newName: newName},
77+
success: function(response) {
78+
console.log(response);
79+
$('#get-request').click();
80+
},
81+
82+
});
83+
});
6284

85+
// DELETE BUTTON
86+
$('table').on('click', '.delete-button', function() {
87+
var rowElement = $(this).closest('tr');
88+
var id = rowElement.find('.id').text();
89+
90+
$.ajax({
91+
url: '/products/' + id,
92+
method: 'DELETE',
93+
contentType: 'application/json',
94+
success: function(response) {
95+
console.log(response);
96+
$('#get-request').click();
97+
}
98+
});
6399
});

src/server.js

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,36 @@ app.post('/products', (req, res) => {
3535
res.send('Successfully created product');
3636
});
3737

38+
app.put('/products/:id', (req, res) => {
39+
const { id } = req.params;
40+
const newName = req.body.newName;
41+
var found = false;
42+
43+
console.log(newName);
44+
45+
products.forEach((product, i) => {
46+
if(!found && product.id === Number(id)) {
47+
product.name = newName;
48+
}
49+
});
50+
51+
res.send('Successfully updated product');
52+
});
53+
54+
app.delete('/products/:id', (req, res) => {
55+
var id = req.params.id;
56+
57+
var found = false;
58+
59+
products.forEach(function(product, index) {
60+
if(!found && product.id === Number(id)) {
61+
products.splice(index, 1);
62+
}
63+
});
64+
65+
res.send('successfully deleted product');
66+
});
67+
3868
app.listen(app.get('port'), () => {
3969
console.log(`server on port ${app.get('port')}`);
4070
});

0 commit comments

Comments
 (0)