@@ -31,3 +31,43 @@ export const createPost = async (req: Request & { userId?: string }, res: Respon
3131 return res . status ( 500 ) . json ( { message : "Internal server error" } ) ;
3232 }
3333} ;
34+
35+ export const updatePost = async (
36+ req : Request & { userId ?: string } ,
37+ res : Response
38+ ) => {
39+ try {
40+ if ( ! req . userId ) {
41+ return res . status ( 401 ) . json ( { message : "Unauthorized" } ) ;
42+ }
43+
44+ const { postId } = req . params ;
45+ const { title, description, tags = [ ] , media = [ ] } =
46+ req . body as CreatePostInput ;
47+
48+ const post = await Post . findById ( postId ) ;
49+ if ( ! post ) {
50+ return res . status ( 404 ) . json ( { message : "Post not found" } ) ;
51+ }
52+
53+ if ( post . userId . toString ( ) !== req . userId ) {
54+ return res . status ( 403 ) . json ( { message : "Forbidden" } ) ;
55+ }
56+
57+ const updateData : Partial < CreatePostInput > = { } ;
58+ if ( title !== undefined ) updateData . title = title ;
59+ if ( description !== undefined ) updateData . description = description ;
60+ if ( tags !== undefined ) updateData . tags = tags ;
61+ if ( media !== undefined ) updateData . media = media ;
62+
63+ const updated = await Post . findByIdAndUpdate ( postId , { $set : updateData } , { new : true } ) ;
64+
65+ return res . status ( 200 ) . json ( {
66+ success : true ,
67+ message : "Post updated successfully" ,
68+ data : updated ,
69+ } ) ;
70+ } catch ( error ) {
71+ return res . status ( 500 ) . json ( { message : "Internal server error" } ) ;
72+ }
73+ } ;
0 commit comments