From 5ce252739baba5aa699eecf9635657665a42fbb5 Mon Sep 17 00:00:00 2001 From: tienbku Date: Thu, 26 May 2022 17:51:53 +0700 Subject: [PATCH 1/2] update package --- README.md | 2 ++ package.json | 4 ++-- 2 files changed, 4 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index d754176..5ab520c 100644 --- a/README.md +++ b/README.md @@ -48,6 +48,8 @@ Fullstack: > [Angular 12 + Node.js Express + MySQL example](https://www.bezkoder.com/angular-12-node-js-express-mysql/) +> [Angular 13 + Node.js Express + MySQL example](https://www.bezkoder.com/angular-13-node-js-express-mysql/) + > [React + Node.js + Express + MySQL example](https://www.bezkoder.com/react-node-express-mysql/) > [React + Redux + Node.js Express + MySQL](https://www.bezkoder.com/react-redux-mysql-crud/) diff --git a/package.json b/package.json index ef1deac..e0a3e02 100644 --- a/package.json +++ b/package.json @@ -16,7 +16,7 @@ "license": "ISC", "dependencies": { "cors": "^2.8.5", - "express": "^4.17.1", - "mysql": "^2.17.1" + "express": "^4.18.1", + "mysql": "^2.18.1" } } From 775dbe36d402b35890f7eb7d20ba32edf43fe8a5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Louren=C3=A7o=20Lima?= <51246899+LourencoDev@users.noreply.github.com> Date: Thu, 30 Jun 2022 21:45:46 -0300 Subject: [PATCH 2/2] Update tutorial.controller.js --- app/controllers/tutorial.controller.js | 110 +++++++++++-------------- 1 file changed, 50 insertions(+), 60 deletions(-) diff --git a/app/controllers/tutorial.controller.js b/app/controllers/tutorial.controller.js index 7da857a..ff5f42c 100644 --- a/app/controllers/tutorial.controller.js +++ b/app/controllers/tutorial.controller.js @@ -2,129 +2,119 @@ const Tutorial = require("../models/tutorial.model.js"); // Create and Save a new Tutorial exports.create = (req, res) => { + const { title, description, published } = req.body; // Validate request if (!req.body) { - res.status(400).send({ - message: "Content can not be empty!" - }); + return res.status(400).json({ message: "Content can not be empty!" }); } // Create a Tutorial const tutorial = new Tutorial({ - title: req.body.title, - description: req.body.description, - published: req.body.published || false + title, + description, + published: published || false, }); // Save Tutorial in the database Tutorial.create(tutorial, (err, data) => { - if (err) - res.status(500).send({ - message: - err.message || "Some error occurred while creating the Tutorial." - }); - else res.send(data); + if (err) { + return res.status(500).json({ message: err.message || "Some error occurred while creating the Tutorial." }); + } + + return res.status(200).json(data); }); }; // Retrieve all Tutorials from the database (with condition). exports.findAll = (req, res) => { - const title = req.query.title; + const { title } = req.query; Tutorial.getAll(title, (err, data) => { - if (err) - res.status(500).send({ - message: - err.message || "Some error occurred while retrieving tutorials." - }); - else res.send(data); + if (err) { + return res.status(500).json({ message: err.message || "Some error occurred while retrieving tutorials." }); + } + + return res.status(200).json(data); }); }; // Find a single Tutorial by Id exports.findOne = (req, res) => { - Tutorial.findById(req.params.id, (err, data) => { + const { id } = req.params; + + Tutorial.findById(id, (err, data) => { if (err) { if (err.kind === "not_found") { - res.status(404).send({ - message: `Not found Tutorial with id ${req.params.id}.` - }); + return res.status(404).json({ message: `Not found Tutorial with id ${req.params.id}.` }); } else { - res.status(500).send({ - message: "Error retrieving Tutorial with id " + req.params.id - }); + return res.status(500).json({ message: `Error retrieving Tutorial with id ${req.params.id}` }); } - } else res.send(data); + } + + return res.status(200).json(data); }); }; // find all published Tutorials exports.findAllPublished = (req, res) => { Tutorial.getAllPublished((err, data) => { - if (err) - res.status(500).send({ - message: - err.message || "Some error occurred while retrieving tutorials." - }); - else res.send(data); + if (err) { + return res.status(500).json({ message: err.message || "Some error occurred while retrieving tutorials." }); + } + + return res.status(200).json(data); }); }; // Update a Tutorial identified by the id in the request exports.update = (req, res) => { + const { id } = req.params; + // Validate Request if (!req.body) { - res.status(400).send({ - message: "Content can not be empty!" - }); + return res.status(400).json({ message: "Content can not be empty!" }); } - console.log(req.body); - Tutorial.updateById( - req.params.id, + id, new Tutorial(req.body), (err, data) => { if (err) { if (err.kind === "not_found") { - res.status(404).send({ - message: `Not found Tutorial with id ${req.params.id}.` - }); + return res.status(404).json({ message: `Not found Tutorial with id ${id}.` }); } else { - res.status(500).send({ - message: "Error updating Tutorial with id " + req.params.id - }); + return res.status(500).json({ message: `Error updating Tutorial with id ${id}` }); } - } else res.send(data); + } + + return res.status(200).json(data); } ); }; // Delete a Tutorial with the specified id in the request exports.delete = (req, res) => { - Tutorial.remove(req.params.id, (err, data) => { + const { id } = req.params; + Tutorial.remove(id, (err, data) => { if (err) { if (err.kind === "not_found") { - res.status(404).send({ - message: `Not found Tutorial with id ${req.params.id}.` - }); + return res.status(404).json({ message: `Not found Tutorial with id ${id}.` }); } else { - res.status(500).send({ - message: "Could not delete Tutorial with id " + req.params.id - }); + return res.status(500).json({ message: `Could not delete Tutorial with id ${id}` }); } - } else res.send({ message: `Tutorial was deleted successfully!` }); + } + + return res.status(200).json({ message: `Tutorial was deleted successfully!` }); }); }; // Delete all Tutorials from the database. exports.deleteAll = (req, res) => { Tutorial.removeAll((err, data) => { - if (err) - res.status(500).send({ - message: - err.message || "Some error occurred while removing all tutorials." - }); - else res.send({ message: `All Tutorials were deleted successfully!` }); + if (err) { + return res.status(500).json({ message: err.message || "Some error occurred while removing all tutorials." }); + } + + return res.status(200).json({ message: `All Tutorials were deleted successfully!` }); }); };