-
Notifications
You must be signed in to change notification settings - Fork 84
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #250 from ChibuikemMichaelIlonze/edit-blog-post
feat: API Endpoint for Editing a Blog Post by Id
- Loading branch information
Showing
3 changed files
with
85 additions
and
1 deletion.
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,36 @@ | ||
import { Request, Response } from "express"; | ||
import { updateBlogPost } from "../services/updateBlog.services"; | ||
|
||
export const updateBlogController = async (req: Request, res: Response) => { | ||
const { id } = req.params; | ||
const { title, content, published_at, image_url } = req.body; | ||
|
||
console.log("Controller - updateBlogController called with id:", id); | ||
console.log("Controller - Request body:", req.body); | ||
|
||
try { | ||
const updatedBlog = await updateBlogPost( | ||
id, | ||
title, | ||
content, | ||
published_at, | ||
image_url, | ||
); | ||
console.log("Controller - Blog post updated successfully:", updatedBlog); | ||
|
||
return res.status(200).json({ | ||
status: "success", | ||
status_code: 200, | ||
message: "Blog post updated successfully", | ||
data: updatedBlog, | ||
}); | ||
} catch (error) { | ||
console.error("Controller - Error updating blog post:", error); | ||
|
||
return res.status(500).json({ | ||
status: "unsuccessful", | ||
status_code: 500, | ||
message: "Failed to update the blog post. Please try again later.", | ||
}); | ||
} | ||
}; |
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,42 @@ | ||
import { Blog } from "../models/blog"; | ||
import AppDataSource from "../data-source"; | ||
|
||
export const updateBlogPost = async ( | ||
id: string, | ||
title: string, | ||
content: string, | ||
published_at?: Date, | ||
image_url?: string, | ||
) => { | ||
const blogRepository = AppDataSource.getRepository(Blog); | ||
|
||
let blog; | ||
try { | ||
blog = await blogRepository.findOne({ where: { id } }); | ||
} catch (error) { | ||
throw new Error("Error finding blog post."); | ||
} | ||
|
||
if (!blog) { | ||
throw new Error("Blog post not found."); | ||
} | ||
|
||
blog.title = title; | ||
blog.content = content; | ||
|
||
if (published_at) { | ||
blog.published_at = published_at; | ||
} | ||
|
||
if (image_url) { | ||
blog.image_url = image_url; | ||
} | ||
|
||
try { | ||
await blogRepository.save(blog); | ||
} catch (error) { | ||
console.error("Service - Error saving blog post:", error); | ||
} | ||
|
||
return blog; | ||
}; |