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
8 changes: 6 additions & 2 deletions src/controllers/Blog.controller.js.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,9 @@ const getBlog = async (req, res) => {
try {
const blog = await Blog.findById(blogId);
if (!blog) {
throw Error('Blog not found!');
const err = new Error('Blog is not found!');
err.status = 400;
throw err;
}
res.status(200).json({
blog
Expand Down Expand Up @@ -72,7 +74,9 @@ const deleteBlog = async (req, res) => {
try {
const deletedBlog = await Blog.findByIdAndDelete(blogId);
if (!deletedBlog) {
throw Error('Blog not found!');
const err = new Error('Blog not found');
err.status = 400;
throw err;
}
res.status(204)
} catch (error) {
Expand Down
89 changes: 89 additions & 0 deletions src/controllers/user.controllers.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
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)
}
}

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 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 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 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)
};
};



module.exports = {
getUsers,
getUserById,
createUser,
updateUserById,
deleteUserById
}
6 changes: 1 addition & 5 deletions src/models/blog.model.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,8 @@ const blogSchema = new Schema(
type: String,
required: true
},
author: {
type: String,
default: 'Author'
},

images: [{ type: String }],

comments: [{ type: String }]
},
{
Expand Down
22 changes: 22 additions & 0 deletions src/models/user.model.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
const mongoose = require("mongoose")
const Schema = mongoose.Schema

const blogSchema = new Schema(
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

userSchema vaf chưa export

{
name: {
type: String,
required: true
},
password: {
type: String,
required: true,
},
role: {
type: String,

}
},
{
timestamps: true,
}
)