-
Notifications
You must be signed in to change notification settings - Fork 0
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
NgoThao2003
wants to merge
16
commits into
main
Choose a base branch
from
Exam1-ngothaolai
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 1 commit
Commits
Show all changes
16 commits
Select commit
Hold shift + click to select a range
2f80265
API register & login + Add Middleware
abaf006
Buoi 9
ChristianGreyy ff83fb9
KiemTra
ChristianGreyy 2b75e7f
set up express + router
NgoThao2003 edd26ca
set up env
NgoThao2003 a870af4
set up MongoDB
NgoThao2003 3cc6a36
Rest API Blogs
NgoThao2003 fcf958d
Error Middleware
NgoThao2003 95dda33
CRUD user
NgoThao2003 d32c53a
API write API to get information
NgoThao2003 2f0eabf
upload file
NgoThao2003 7d3d66f
API register
NgoThao2003 0c1df05
API login
NgoThao2003 d8a1e79
middleware auth
NgoThao2003 b8be0ae
fixed middleware auth
NgoThao2003 3c9bb36
role.Middleware
NgoThao2003 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
blogSchema