Unterschiede
Hier werden die Unterschiede zwischen zwei Versionen angezeigt.
Beide Seiten der vorigen Revision Vorhergehende Überarbeitung Nächste Überarbeitung | Vorhergehende Überarbeitung | ||
modul:m290:learningunits:lu09:theorie:03 [2024/10/17 16:21] – vdemir | modul:m290:learningunits:lu09:theorie:03 [2024/11/05 14:51] (aktuell) – vdemir | ||
---|---|---|---|
Zeile 1: | Zeile 1: | ||
- | ====== | + | ====== |
===== Learning Objectives ===== | ===== Learning Objectives ===== | ||
- | ... | + | - How to connect the server to the db |
+ | - How to fetch data from the db and display it in POSTMAN | ||
+ | - How to perform the CRUD operations on the server | ||
+ | - How to perform the CRUD operations by using PoSTMAN | ||
+ | - | ||
+ | ===== Sources ===== | ||
+ | * {{ : | ||
| | ||
- | ===== Introcuction | + | ===== Introduction |
- | ... | + | At last chapter of our database journey we want to perform all CRUD operations within the server AND the client (POSTMAN). For reasons of efficiency we are going to start with the R = READ of CRUD. |
- | . | + | |
- | ==== Vocabulary | + | ==== R - READ: Fetching list of data ==== |
- | ^English ^ Deutsch ^ | + | First of all, we want to display the list of users, which are store in the table users. The following code show how to get that list from the database. |
- | | ...| ...| | + | |
+ | // Method to get data from the user table | ||
+ | app.get('/ | ||
+ | const query = ' | ||
+ | db.query(query, | ||
+ | if (err) { | ||
+ | console.error(' | ||
+ | res.status(500).send(' | ||
+ | return; | ||
+ | } | ||
+ | res.json(results); | ||
+ | }); | ||
+ | }); | ||
+ | | ||
+ | {{: | ||
+ | |||
+ | ==== R - READ: Getting one spefific row of data ==== | ||
+ | If we want one speficific individual to be displayed, we need to make some changes, such as filtering a user_id, as shown in the following code below. | ||
+ | |||
+ | app.get('/ | ||
+ | const userId = req.params.id; | ||
+ | // SQL query to fetch user by id | ||
+ | const query = ' | ||
+ | db.query(query, | ||
+ | if (err) { | ||
+ | console.error(' | ||
+ | res.status(500).send(' | ||
+ | return; | ||
+ | } | ||
+ | if (results.length === 0) { | ||
+ | res.status(404).send(' | ||
+ | } else { | ||
+ | res.status(200).json(results[0]); | ||
+ | } | ||
+ | }); | ||
+ | }); | ||
+ | |||
+ | {{: | ||
+ | |||
+ | ==== D - Deletion of one specific record ==== | ||
+ | If we want to delete one specific row of data, we need another method to perform that. | ||
+ | |||
+ | app.delete('/ | ||
+ | const userId = req.params.id; | ||
+ | const deleteQuery = ' | ||
+ | db.query(deleteQuery, | ||
+ | if (err) { | ||
+ | res.status(500).send(' | ||
+ | } else if (result.affectedRows === 0) { | ||
+ | res.status(404).send(' | ||
+ | } else { | ||
+ | res.send(`User with ID ${userId} deleted successfully`); | ||
+ | } | ||
+ | }); | ||
+ | }); | ||
+ | | ||
+ | |||
+ | {{: | ||
+ | |||
+ | ==== U - Update of one specfic row ==== | ||
+ | Well, if we want to update one row of data, we need the http method PUT. | ||
+ | |||
+ | app.put('/ | ||
+ | const userId = req.params.id; | ||
+ | const { username, email } = req.query; // Get username and email from query parameters | ||
+ | // Ensure that at least one field is provided | ||
+ | if (!username && !email) { | ||
+ | return res.status(400).send(' | ||
+ | } | ||
+ | // Construct the update query | ||
+ | const updateQuery = ' | ||
+ | const values = [username || null, email || null, userId]; // Use null for any field not provided | ||
+ | db.query(updateQuery, | ||
+ | if (err) { | ||
+ | res.status(500).send(' | ||
+ | } else if (result.affectedRows === 0) { | ||
+ | res.status(404).send(' | ||
+ | } else { | ||
+ | res.send(`User with ID ${userId} updated successfully`); | ||
+ | } | ||
+ | }); | ||
+ | }); | ||
+ | | ||
+ | {{: | ||
+ | | ||
+ | ==== C - CREATE: Insert a new row into the db ==== | ||
+ | Finally, with the INSERT operation, we complete our CRUD requirements. | ||
+ | |||
+ | // insert a row | ||
+ | app.post('/ | ||
+ | const { username, email } = req.query; // Get username and email from query parameters | ||
+ | // Ensure that both fields are provided | ||
+ | if (!username || !email) { | ||
+ | return res.status(400).send(' | ||
+ | } | ||
+ | // Construct the insert query | ||
+ | const insertQuery = ' | ||
+ | const values = [username, email]; // Use provided fields for insertion | ||
+ | db.query(insertQuery, | ||
+ | if (err) { | ||
+ | console.error(' | ||
+ | return res.status(500).send(' | ||
+ | } | ||
+ | // Respond with the ID of the newly created user | ||
+ | res.status(201).send(`User added with ID: ${result.insertId}`); | ||
+ | }); | ||
+ | }); | ||
+ | |||
+ | {{: | ||
---- | ---- | ||
[[https:// | [[https:// | ||