Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 10 additions & 4 deletions controllers/authControllers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,13 +32,13 @@ const login = async (req: Request, res: Response, next: NextFunction) => {
const { email, password } = req.body;
user = await UserModel.UserSchema.findOne({ email: email });
if (!user) {
return res.status(httpStatus.BAD_REQUEST).json({
return res.status(httpStatus.NOT_FOUND).json({
message: "User with this email does not exist",
});
}
const match = await bcrypt.compare(password, user.password);
if (!match) {
return res.status(httpStatus.BAD_REQUEST).json({
return res.status(httpStatus.UNAUTHORIZED).json({
message: "Password is incorrect",
});
}
Expand Down Expand Up @@ -80,7 +80,10 @@ const resetPassword = async (req: Request, res: Response, next: NextFunction) =>
})
}
catch (err) {
return next(err);
return res.status(httpStatus.BAD_REQUEST).json({
message: "Invalid token",
error: next(err)
});
}
}

Expand All @@ -102,7 +105,10 @@ const forgotPassword = async (req: Request, res: Response, next: NextFunction) =
});
}
catch (err) {
return next(err);
return res.status(httpStatus.BAD_REQUEST).json({
message: "Invalid email",
error: next(err)
});
}
}

Expand Down
36 changes: 30 additions & 6 deletions controllers/blogControllers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,43 +21,64 @@ const createBlog = async (req: Request, res: Response, next: NextFunction) => {
const deleteBlog = async (req: Request, res: Response, next: NextFunction) => {
try {
const blogId = new Types.ObjectId(req.params.id);
await BlogModel.BlogSchema.deleteOne({ __id: blogId });
const noDeleted = await BlogModel.BlogSchema.deleteOne({ __id: blogId });
if(!noDeleted.deletedCount) {
return res.status(httpStatus.NOT_FOUND).json({
message : "Blog not found"
})};
return res.status(httpStatus.OK).json({
message: "Blog was successfully deleted"
})
}
catch (err) {
return next(err);
return res.status(httpStatus.BAD_REQUEST).json({
message : "Invalid blog id",
error: next(err)
});
}
}

const updateBlog = async (req: Request, res: Response, next: NextFunction) => {
try {;
const blogId = new Types.ObjectId(req.params.id);
const blog = await BlogModel.BlogSchema.findByIdAndUpdate(blogId, req.body)
if (!blog) {
return res.status(httpStatus.NOT_FOUND).json({
message: "Blog not found"
})
}
return res.status(httpStatus.OK).json({
blog: blog,
message: "Blog was updated successfully"
})
}
catch (err) {
return next(err);
return res.status(httpStatus.BAD_REQUEST).json({
message : "Invalid blog id",
error: next(err)
});
}

}

const getBlogById = async (req: Request, res: Response, next: NextFunction) => {
try {
const blogId = new Types.ObjectId(req.params.id);
const blog = await BlogModel.BlogSchema.findById(blogId);
if (!blog) {
return res.status(httpStatus.BAD_REQUEST)
return res.status(httpStatus.NOT_FOUND).json({
message: "Blog not found"
})
}
return res.status(httpStatus.OK).json({
blog: blog
})
}
catch (err) {
return next(err);
return res.status(httpStatus.BAD_REQUEST).json({
message : "Invalid blog id",
error: next(err)
});
}
}

Expand All @@ -73,7 +94,10 @@ const getAllBlogs = async ( req:Request, res: Response, next: NextFunction) => {
})
}
catch (err) {
return next(err);
return res.status(httpStatus.BAD_REQUEST).json({
message : "Invalid query parameters",
error: next(err)
});
}
}

Expand Down