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

Exam1 tu nhat luong #54

Open
wants to merge 17 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 7 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
9 changes: 2 additions & 7 deletions .env.template
Original file line number Diff line number Diff line change
@@ -1,7 +1,2 @@
# Sample .env.template file

# Database URL
DB_URL =

# Port number
PORT =
PORT =
DB_URL =
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
node_modules
.env
.env
116 changes: 60 additions & 56 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

26 changes: 9 additions & 17 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,26 +1,18 @@
{
"dependencies": {
"dotenv": "^16.0.3",
"express": "^4.18.2",
"mongoose": "^7.0.3",
"nodemon": "^2.0.22"
},
"name": "hit_nodejs_2023",
"description": "",
"name": "hit-kiemtra",
"version": "1.0.0",
"description": "",
"main": "server.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1",
"start": "nodemon ./src/server.js"
},
"repository": {
"type": "git",
"url": "git+https://github.com/hit-haui/HIT_NodeJS_2023.git"
"start": "nodemon --inspect ./src/server.js"
},
"author": "",
"license": "ISC",
"bugs": {
"url": "https://github.com/hit-haui/HIT_NodeJS_2023/issues"
},
"homepage": "https://github.com/hit-haui/HIT_NodeJS_2023#readme"
"dependencies": {
"dotenv": "^16.0.3",
"express": "^4.18.2",
"mongoose": "^7.2.1",
"nodemon": "^2.0.22"
}
}
89 changes: 89 additions & 0 deletions src/controllers/blog.controller.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
const Blog = require('../models/blog.model');

const getBlogs = async (req, res, next) => {
try {
const blogs = await Blog.find();
res.status(200).json({ blogs });
}
catch (err) {
next(err);
};
};

const getBlogById = async (req, res, next) => {
const { blogId } = req.params;
try {
const blog = await Blog.findById(blogId);
if (!blog) {
const err = new Error('blog not found!');
err.status = 404;
throw err;
}
res.status(200).json({ blog });
}
catch (err) {
next(err);
};
};

const createBlog = async (req, res, next) => {
const newBlog = req.body;
try {
if (!newBlog.author) {
const err = new Error('Author is require!');
err.status = 400;
throw err;
}
const blog = await Blog.create(newBlog);
res.status(201).json({ blog });
}
catch (err) {
next(err);
};
};

const updateBlogById = async (req, res, next) => {
const { blogId } = req.params;
const newBlog = req.body;
try {
if (!newBlog.author) {
const err = new Error('Author is require!');
err.status = 400;
throw err;
}
const blog = await Blog.findByIdAndUpdate(blogId, newBlog);
if (!blog) {
const err = new Error('Blog not found!');
err.status = 404;
throw err;
}
res.status(200).json({ blog });
}
catch (err) {
next(err);
};
};

const deleteBlogById = async (req, res, next) => {
const { blogId } = req.params;
try {
const blog = await Blog.findByIdAndDelete(blogId);
if (!blog) {
const err = new Error('Blog not found!');
err.status = 404;
throw err;
}
res.status(200).json({ blog });
}
catch (err) {
next(err);
};
};

module.exports = {
getBlogs,
getBlogById,
createBlog,
updateBlogById,
deleteBlogById
};
Loading