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

Merge pull request #213 from hngprojects/chore/husky-setup #236

Closed
wants to merge 3 commits into from
Closed
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
3 changes: 1 addition & 2 deletions contributors.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@


Erasmus Tayviah (StarmannRassy)

Micheal Peter (MYCON)
33 changes: 33 additions & 0 deletions src/controllers/blogCommentController.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import { Request, Response } from "express";
import { createComment } from "../services/blogComment.services";

export class BlogCommentController {
async createComment(req: Request, res: Response) {
const blogId = req.params.blogId;
const { content } = req.body;

try {
const comment = await createComment(blogId, content);
res.status(201).json({
status: "success",
status_code: 201,
message: "Comment created successfully.",
data: comment,
});
} catch (error: any) {
if (error.message === "Blog not found") {
res.status(404).json({
status: "unsuccessful",
status_code: 404,
message: error.message,
});
} else {
res.status(500).json({
status: "unsuccessful",
status_code: 500,
message: "Failed to create comment. Please try again later.",
});
}
}
}
}
10 changes: 10 additions & 0 deletions src/routes/blog.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,20 @@ import { authMiddleware } from "../middleware";
import { createBlogController } from "../controllers/createBlogController";
import { BlogController } from "../controllers";

import { BlogCommentController } from "../controllers/blogCommentController";
const blogRouter = Router();
const blogController = new BlogController();

blogRouter.post("/create", authMiddleware, createBlogController);
blogRouter.get("/", blogController.listBlogs.bind(blogController));
const blogCommentController = new BlogCommentController();

// endpoint to create a comment on a blog post

blogRouter.post(
"/:postID/comment",
authMiddleware,
blogCommentController.createComment.bind(blogCommentController),
);

export { blogRouter };
2 changes: 1 addition & 1 deletion src/services/createBlog.services.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ export const createBlogPost = async (
content: string,
image_url?: string,
tags?: number[],
categories?: number[]
categories?: number[],
) => {
const newBlog = new Blog();
newBlog.title = title;
Expand Down