This commit is contained in:
Philippe Torrel
2026-07-24 13:31:30 +02:00
parent c118892017
commit e5c2f47063
19 changed files with 1380 additions and 659 deletions

View File

@@ -38,9 +38,29 @@ Content-Type: application/json
# DELETE Product
DELETE http://127.0.0.1:8000/api/products/940c316b-518c-4a18-b1f6-328e010eef4d
DELETE http://127.0.0.1:8000/api/products/6cbe4783-cfb5-4ed7-8196-9d6b55217de0
Content-Type: application/json
###
# RESET Products
PATCH http://127.0.0.1:8000/api/products/reset
Content-Type: application/json
{
"reset": true
}
###
# SAVE Products
PATCH http://127.0.0.1:8000/api/products/save
Content-Type: application/json
{
"save": true
}

View File

@@ -0,0 +1,38 @@
[
{
"_id": "6cbe4783-cfb5-4ed7-8196-9d6b55217de0",
"position": 1,
"name": "3Doodler 3D Printing Pen",
"price": 29.99
},
{
"_id": "c38913d4-9524-461f-85d2-525241166e8e",
"position": 2,
"name": "Powerstation 5- E. Maximus Chargus",
"price": 44.95
},
{
"_id": "cdc887bd-fb0e-4485-8516-0a3720c3bb72",
"position": 3,
"name": "8-Bit Legendary Hero Heat-Change Mug",
"price": 6.99
},
{
"_id": "bded2710-8db8-41a5-87af-269195eab449",
"position": 4,
"name": "16-Bit Legendary Hero Heat-Change Mug",
"price": 12.99
},
{
"_id": "3b3352c1-789d-4b4f-8a93-f05f44ece5a5",
"position": 5,
"name": "32-Bit Legendary Hero Heat-Change Mug",
"price": 18.99
},
{
"_id": "5af39d33-cbcd-42c7-a160-50084faa91ba",
"position": 6,
"name": "64-Bit Legendary Hero Heat-Change Mug",
"price": 21.99
}
]

View File

@@ -35,4 +35,4 @@
"name": "64-Bit Legendary Hero Heat-Change Mug",
"price": 21.99
}
]
]

View File

@@ -14,6 +14,15 @@ const ProductModel = () => {
}
};
const reset = () => {
try {
products = JSON.parse(fs.readFileSync('./data/products.bakup.json', 'utf-8'));
} catch (error) {
console.log('Something went wrong: ', error);
products = [];
}
};
const save = () => {
try {
fs.writeFileSync('./data/products.json', JSON.stringify(products, null, 2), 'utf-8');
@@ -53,7 +62,6 @@ const ProductModel = () => {
};
const update = (product) => {
console.log(product);
const foundIdx = products.findIndex((obj) => {
return obj._id === product._id;
});
@@ -98,6 +106,7 @@ const ProductModel = () => {
get,
update,
delete: remove,
reset,
};
};

View File

@@ -4,6 +4,7 @@
"description": "",
"main": "server.js",
"scripts": {
"start": "npx nodemon server.js",
"server": "npx nodemon server.js"
},
"keywords": [],

View File

@@ -13,7 +13,7 @@ const app = express();
const products = ProductModel();
const corsOptions = {
origin: 'http://localhost:3000',
origin: ['http://localhost:3000', 'http://127.0.0.1:3000'],
optionsSuccessStatus: 200, // some legacy browsers (IE11, various SmartTVs) choke on 204
};
@@ -22,7 +22,12 @@ products.load();
// Middleware
app.use(cors(corsOptions)); // Fängt alle HTTP-Requests vor den Routenabfragen ab.
app.use(express.json()); // HTTP-Request Body wird als JSON Objekt erwartet und geparsed wird.
app.use(express.json()); // HTTP-Request Body wird als JSON Objekt erwartet und geparsed wird. // app.use(bodyParser.json()) - Modul body-parser war in express 4 notwendig
app.use((req, res, next) => {
console.log(color.yellow('HTTP-Method: '), color.magenta(req.method));
next();
});
// Routes
app.get('/', (req, res) => {
@@ -101,6 +106,34 @@ app.put('/api/products', (req, res) => {
return res.send({ msg: 'Product updated', status: 200, success: true, data: product });
});
// HTTP - Methode - PATCH
// reseten von Produkten
app.patch('/api/products/reset', (req, res) => {
const reset = req.body.reset;
if (!reset) {
return res.status(400).send({ msg: 'Could not reset products', status: 400, success: false });
}
products.reset();
return res.send({ msg: 'Products reseted', status: 200, success: true, data: reset });
});
// HTTP - Methode - PATCH
// speichern von Produkten
app.patch('/api/products/save', (req, res) => {
const save = req.body.save;
if (!save) {
return res.status(400).send({ msg: 'Could not save products', status: 400, success: false });
}
products.save();
return res.send({ msg: 'Products saved', status: 200, success: true, data: save });
});
// HTTP - Methode - DELETE
// CRU(D) - delete
// BREA(D) - delete