1.Initialization
const handleDB = require('../handleDB/index') // Get all navigation exports.allNav =async (req, res) => { } //Update or add navigation exports.upNav =async (req, res) => { } //Delete based on id exports.delNav = async(req, res) => { }
2. Database design
aynsc turns the function into an asynchronous function and uses it with await, using the form of arrow function
Simplify(async funtion(){})
How to write an exception:
try { } catch (error) { console.error('Error in upNav:', error); res.send({ code: 500, message: 'Internal server error' }); }
3. Get all front-end navigation categories
// Get all navigation categories exports.allNav = async (req, res) => { let result = await handleDB(res, 'book_nav', "find", "Error querying database", { where: `status=0` }) if (result. length == 0) { res.send({ code: 0, message: "Navigation list is empty" }) } const total = result.length; res.send({ code: 200, message: 'Get navigation list successfully', total: total, data: result }) }
4. Update or add navigation
//Update or add navigation exports.upNav = async (req, res) => { try { // Deconstruct the request body const { navId, ...data } = req.body; // Check if navId exists if (navId) { // If navId exists, update navigation information const result = await handleDB(res, 'book_nav', 'update', 'Update failed', `navId=${navId}`, data); // Check if the update is successful if (result.affectedRows !== 1) { return res.send({ code: 0, message: 'Failed to update navigation category data!' }); } res.send({ code: 200, message: 'Updating navigation classification data successfully' }); } else { // If navId does not exist, add navigation information // 3. Query the database to see if this user name exists let result1 = await handleDB(res, "book_nav", "find", "Database query error", { where: `navName="${data.navName}"` }) // 4. If it already exists, return the user name has been registered return if (result1.length > 0) { res.send({ code: 0, message: "The navigation category name has been registered, please change other navigation category names!" }); return } const result = await handleDB(res, 'book_nav', 'insert', 'Add failed', data); // Check if the insertion is successful if (result.affectedRows !== 1) { return res.send({ code: 0, message: 'Failed to add navigation category data!' }); } res.send({ code: 200, message: 'Add navigation category data successfully' }); } } catch (error) { console.error('Error in upNav:', error); res.send({ code: 500, message: 'Internal server error' }); } };
5. Delete based on id
//Delete based on id exports.delNav = async (req, res) => { // console.log(11); const navId = req.params.navId; if (!navId) { res.send({ code: 0, message: 'The obtained parameter is empty' }) } // 3 Query the database according to the ID to see if the navigation category exists let result = await handleDB(res, "book_nav", "find", "book_nav query error", { where: `navId = ${navId} AND status = 0` }); // console.log(result); // 4 Check whether the navigation with the specified id exists if (result.length !== 1) return res.send({ code: 0, message: 'This category does not exist!' }) let results = await handleDB(res, "book_nav", "update", "Error in querying data!", `navId= ${navId}`, { status: 1 }); // 6 SQL statement is executed successfully, but the number of rows affected is not equal to 1 if (results.affectedRows !== 1) return res.send({ code: 0, message: 'Failed to delete navigation category!' }) res.send({ code: 200, message: 'Delete navigation category successfully', }); }
6. Get different book product lists based on id
// Get different book products based on different IDs exports.getProduct = async (req, res) => { const id = req.params.id; if (!id) { return res.send({ code: 0, message: 'The obtained parameter is empty' }) } // 3 Query the database according to the ID to see if the product exists let result = await handleDB(res, "book_product", "find", "book_product query error", { where: `product_id = ${id} AND status = 0` }); // console.log(result); // 4 Check whether the product with the specified id exists if (result.length == 0) return res.send({ code: 0, message: 'The product list is empty!' }) const total = result.length res.send({ code: 200, message: "Getting product list successfully", total: total, data: result }) }