Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update tutorial.controller.js #7

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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/)
Expand Down
110 changes: 50 additions & 60 deletions app/controllers/tutorial.controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -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!` });
});
};
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
}
}