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 ngothao (2) #69

Open
wants to merge 16 commits into
base: main
Choose a base branch
from
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 getBlog = 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 = 401;
throw err;
}
res.status(200).json({
blog,
});
} catch (err) {
next(err);
}
};

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

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

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

module.exports = {
getBlogs,
getBlog,
createBlog,
updateBlog,
deleteBlog,
};
6 changes: 6 additions & 0 deletions src/middleware/error.middleware.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
const errorMiddleware = (err, req, res, next) => {
res.status(err.status || 500).json({
message: err.message,
});
};
module.exports = errorMiddleware;
2 changes: 1 addition & 1 deletion src/models/blog.model.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
const mongoose = require("mongoose");
const Schema = mongoose.Schema;
const blogsSchema = new Schema({

Choose a reason for hiding this comment

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

blogSchema

name: {
title: {
type: String,
required: true,
},
Expand Down
16 changes: 16 additions & 0 deletions src/routes/blog.route.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
const express = require("express");
const {
getBlogs,
getBlog,
createBlog,
updateBlog,
deleteBlog,
} = require("../controllers/blog.controller");

const blogRouter = express.Router();

Choose a reason for hiding this comment

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

router


blogRouter.route("/").get(getBlogs).post(createBlog);

blogRouter.route("/:blogId").get(getBlog).put(updateBlog).delete(deleteBlog);

module.exports = blogRouter;
16 changes: 16 additions & 0 deletions src/routes/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
const express = require("express");
const router = express.Router();
const blogRouter = require("./blog.route");

const routes = [
{
path: "/blogs",
route: blogRouter,
},
];

routes.map((route) => {
router.use(route.path, route.route);
});

module.exports = router;
13 changes: 9 additions & 4 deletions src/sever.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,14 @@
const express = require("express");
const app = express();
const router = require("./routes");
const mongoose = require("mongoose");
const dotenv = require("dotenv");
const errorMiddleware = require("./middleware/error.middleware");

const app = express();

app.use(express.json());
app.use(router);

dotenv.config();

const port = process.env.PORT || 8080;
Expand All @@ -11,9 +18,7 @@ mongoose
.then(() => console.log("Connected!"))
.catch((err) => console.log(err));

app.get("/", function (req, res) {
res.send("Hello world");
});
app.use(errorMiddleware);

app.listen(port, () => {
console.log(`Example app listening on port ${port}`);
Expand Down