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 dhlieu2 #67

Open
wants to merge 16 commits into
base: main
Choose a base branch
from
7 changes: 0 additions & 7 deletions .env.template

This file was deleted.

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
975 changes: 84 additions & 891 deletions package-lock.json

Large diffs are not rendered by default.

29 changes: 12 additions & 17 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,26 +1,21 @@
{
"dependencies": {
"dotenv": "^16.0.3",
"express": "^4.18.2",
"mongoose": "^7.0.3",
"nodemon": "^2.0.22"
},
"name": "hit_nodejs_2023",
"description": "",
"version": "1.0.0",
"main": "server.js",
"description": "Bài tập về nhà lớp NodeJS\r ## Hướng dẫn\r - Clone project về máy \\\r `git clone https://github.com/hit-haui/HIT_NodeJS_2023.git`\r - Tạo nhánh mới theo tên buổi kèm theo tên của mình \\\r `git checkout -b btvn-buoi5/dinh-tan-hung`\r - Install package và code thôi \\\r `npm install` hoặc `npm i`\r - Sau khi hoàn thành task push lên nhánh của mình **(Không push lên nhánh main)**\r ## Còn tiếp ...",
"main": "index.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 src/server.js",
"test": "echo \"Error: no test specified\" && exit 1"
},
"keywords": [],
"author": "",
"license": "ISC",
"bugs": {
"url": "https://github.com/hit-haui/HIT_NodeJS_2023/issues"
"dependencies": {
"dotenv": "^16.1.3",
"express": "^4.18.2",
"mongoose": "^7.2.2"
},
"homepage": "https://github.com/hit-haui/HIT_NodeJS_2023#readme"
"devDependencies": {
"nodemon": "^2.0.22"
}
}
88 changes: 88 additions & 0 deletions src/controllers/Blog.controller.js.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
const Blog = require("../models/blog.model");

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

const getBlog = async (req, res, next) => {
const { blogId } = req.params;
try {
const blog = await Blog.findById(blogId).populate["author"];
if (!blog) {
const err = new Error("Blog is not found!");
err.status = 400;
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

http status: 404

throw err;
}
res.status(200).json({
blog,
});
} catch (error) {
next(err);
}
};

const createBlog = async (req, res, next) => {
const rawBlog = req.body;
const { title, content } = rawBlog;
try {
if (!title || !content) {
const err = new Error("Title or content is required!");
err.status = 400;
throw err;
}
const newBlog = await Blog.create(rawBlog);
res.status(201).json({
newBlog,
});
} catch (error) {
next(err);
}
};

const updateBlog = async (req, res, next) => {
const { blogId } = req.params;
const newBlog = req.body;
try {
const updatedBlog = await Blog.findByIdAndUpdate(blogId, newBlog);
if (!updatedBlog) {
const err = new Error("Blog not found!");
err.status = 404;
throw err;
}
res.status(200).json({
updatedBlog,
});
} catch (error) {
next(err);
}
};

const deleteBlog = async (req, res, next) => {
const { blogId } = req.params;
try {
const deletedBlog = await Blog.findByIdAndDelete(blogId);
if (!deletedBlog) {
const err = new Error("Blog not found");
err.status = 400;
throw err;
}
res.status(204);
} catch (error) {
next(err);
}
};

module.exports = {
getBlogs,
getBlog,
createBlog,
updateBlog,
deleteBlog,
};
159 changes: 0 additions & 159 deletions src/controllers/classroom.controller.js

This file was deleted.

Loading