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
12 changes: 6 additions & 6 deletions src/controllers/Blog.controller.js.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
const Blog = require('../models/blog.model');


const getBlogs = async (req, res) => {
const getBlogs = async (req, res, next) => {
try {
const blogs = await Blog.find();
res.status(200).json({
Expand All @@ -13,10 +13,10 @@ const getBlogs = async (req, res) => {
};


const getBlog = async (req, res) => {
const getBlog = async (req, res, next) => {
const { blogId } = req.params;
try {
const blog = await Blog.findById(blogId);
const blog = await Blog.findById(blogId).populate["author"];
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

[] => ()

if (!blog) {
const err = new Error('Blog is not found!');
err.status = 400;
Expand All @@ -31,7 +31,7 @@ const getBlog = async (req, res) => {
};


const createBlog = async (req, res) => {
const createBlog = async (req, res, next) => {
const rawBlog = req.body;
const { title, content } = rawBlog;
try {
Expand All @@ -50,7 +50,7 @@ const createBlog = async (req, res) => {
};


const updateBlog = async (req, res) => {
const updateBlog = async (req, res, next) => {
const { blogId } = req.params;
const newBlog = req.body;
try {
Expand All @@ -69,7 +69,7 @@ const updateBlog = async (req, res) => {
};


const deleteBlog = async (req, res) => {
const deleteBlog = async (req, res, next) => {
const { blogId } = req.params;
try {
const deletedBlog = await Blog.findByIdAndDelete(blogId);
Expand Down
146 changes: 72 additions & 74 deletions src/controllers/user.controllers.js
Original file line number Diff line number Diff line change
@@ -1,89 +1,87 @@
const User = require('../models/user.model')
const User = require("../models/user.model");

const getUsers = async (req, res, next) => {
try{
const Users = await User.find();
res.status(200).json({
Users
})
}catch(err){
next(err)
}
}
try {
const Users = await User.find();
res.status(200).json({
Users,
});
} catch (err) {
next(err);
}
};

const getUserById = async (req, res, next) => {
const userId = req.params
try{
const user = await User.findById(userId)
if(!user) {
const err = new Error("User not found")
err.status = 404
throw err
}
res.status(200).json({user})
}catch(err) {
next(err)
const userId = req.params.userId;
// console.log(userId);
try {
const user = await User.findById(userId);
if (!user) {
const err = new Error("User not found");
err.status = 404;
throw err;
}
}

res.status(200).json({ user });
} catch (err) {
next(err);
}
};
const createUser = async (req, res, next) => {
const rawUser = req.body;
const {name, password } = rawUser;
try {
if (!name || !password) {
const err = new Error('Name or password is required!');
err.status = 400;
throw err;
}
const newUser = await User.create(rawUser);
res.status(201).json({
newUser
});
} catch (error) {
next(err)
};
const rawUser = req.body;
const { name, password } = rawUser;
try {
if (!name || !password) {
const err = new Error("Name or password is required!");
err.status = 400;
throw err;
}
const newUser = await User.create(rawUser);
res.status(201).json({
newUser,
});
} catch (error) {
next(err);
}
};


const updateUserById = async (req, res) => {
const { userId } = req.params;
const newUser = req.body;
try {
const updatedUser = await Blog.findByIdAndUpdate(userId, newUser);
if (!updatedUser) {
const err = new Error(' user not found!');
err.status = 404;
throw err;
}
res.status(200).json({
updatedUser
});
} catch (error) {
next(err)
};
const { userId } = req.params;
const newUser = req.body;
try {
const updatedUser = await Blog.findByIdAndUpdate(userId, newUser);
if (!updatedUser) {
const err = new Error(" user not found!");
err.status = 404;
throw err;
}
res.status(200).json({
updatedUser,
});
} catch (error) {
next(err);
}
};


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



module.exports = {
getUsers,
getUserById,
createUser,
updateUserById,
deleteUserById
}
getUsers,
getUserById,
createUser,
updateUserById,
deleteUserById,
};
4 changes: 3 additions & 1 deletion src/models/blog.model.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,9 @@ const blogSchema = new Schema(
required: true
},


author: {
type:{ type: Schema.Types.ObjectId, ref: 'User' },
},
comments: [{ type: String }]
},
{
Expand Down
6 changes: 4 additions & 2 deletions src/models/user.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 blogSchema = new Schema(
const userSchema = new Schema(
{
name: {
type: String,
Expand All @@ -19,4 +19,6 @@ const blogSchema = new Schema(
{
timestamps: true,
}
)
)
const User = mongoose.model("User", userSchema)
module.exports = User
5 changes: 5 additions & 0 deletions src/routers/index.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,16 @@
const express = require("express")

const blogRouter = require("./blog.route")
const userRouter = require("./user.route")
const router = express.Router()
const routes = [
{
path: '/blogs',
route: blogRouter,
},
{
path: '/users',
route: userRouter,
}
]

Expand Down
22 changes: 22 additions & 0 deletions src/routers/user.route.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
const express = require('express');

const {
getUsers,
getUserById,
createUser,
updateUserById,
deleteUserById,
} = require('../controllers/user.controllers');

const userRouter = express.Router();

userRouter.route('/')
.get(getUsers)
.post(createUser)

userRouter.route('/:userId')
.get(getUserById)
.put(updateUserById)
.delete(deleteUserById)

module.exports = userRouter;