1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204
| const { connectDB } = require('./utils/database');
module.exports = async (ctx) => { const db = await connectDB(ctx); const { method, action } = ctx.args;
try { switch (method) { case 'GET': return await handleGet(ctx, db, action); case 'POST': return await handlePost(ctx, db, action); case 'PUT': return await handlePut(ctx, db, action); case 'DELETE': return await handleDelete(ctx, db, action); default: return { error: 'Unsupported method', code: 405 }; } } catch (error) { console.error('API Error:', error); return { error: error.message, code: 500 }; } };
async function handleGet(ctx, db, action) { switch (action) { case 'getPosts': const { page = 1, limit = 10 } = ctx.args; const skip = (page - 1) * limit;
const posts = await db.collection('posts') .find({}) .sort({ createdAt: -1 }) .skip(skip) .limit(parseInt(limit)) .toArray();
return { success: true, data: posts, pagination: { page: parseInt(page), limit: parseInt(limit) } };
case 'getPostById': const { postId } = ctx.args; const post = await db.collection('posts').findOne({ _id: postId });
if (!post) { return { error: 'Post not found', code: 404 }; }
return { success: true, data: post };
case 'getUserPosts': const { userId, page: userPage = 1, limit: userLimit = 10 } = ctx.args; const userSkip = (userPage - 1) * userLimit;
const userPosts = await db.collection('posts') .find({ userId }) .sort({ createdAt: -1 }) .skip(userSkip) .limit(parseInt(userLimit)) .toArray();
return { success: true, data: userPosts, pagination: { page: parseInt(userPage), limit: parseInt(userLimit) } };
default: return { error: 'Invalid action', code: 400 }; } }
async function handlePost(ctx, db, action) { switch (action) { case 'createPost': const { userId, username, content, images = [] } = ctx.args;
if (!userId || !content) { return { error: 'Missing required fields', code: 400 }; }
const newPost = { userId, username, content, images, likes: [], comments: [], createdAt: new Date(), updatedAt: new Date() };
const result = await db.collection('posts').insertOne(newPost);
return { success: true, data: { ...newPost, _id: result.insertedId }, message: 'Post created successfully' };
case 'likePost': const { postId: likePostId, likerId, likerName } = ctx.args;
const post = await db.collection('posts').findOne({ _id: likePostId }); if (!post) { return { error: 'Post not found', code: 404 }; }
const alreadyLiked = post.likes.includes(likerId);
if (alreadyLiked) { await db.collection('posts').updateOne( { _id: likePostId }, { $pull: { likes: likerId }, $set: { updatedAt: new Date() } } ); return { success: true, message: 'Like removed' }; } else { await db.collection('posts').updateOne( { _id: likePostId }, { $push: { likes: likerId }, $set: { updatedAt: new Date() } } ); return { success: true, message: 'Post liked' }; }
case 'addComment': const { postId: commentPostId, commenterId, commenterName, commentContent } = ctx.args;
const comment = { userId: commenterId, username: commenterName, content: commentContent, createdAt: new Date() };
await db.collection('posts').updateOne( { _id: commentPostId }, { $push: { comments: comment }, $set: { updatedAt: new Date() } } );
return { success: true, data: comment, message: 'Comment added' };
default: return { error: 'Invalid action', code: 400 }; } }
async function handlePut(ctx, db, action) { switch (action) { case 'updatePost': const { postId: updatePostId, content: updateContent, images: updateImages } = ctx.args;
const updateData = { content: updateContent, images: updateImages, updatedAt: new Date() };
const updateResult = await db.collection('posts').updateOne( { _id: updatePostId }, { $set: updateData } );
if (updateResult.matchedCount === 0) { return { error: 'Post not found', code: 404 }; }
return { success: true, message: 'Post updated successfully' };
default: return { error: 'Invalid action', code: 400 }; } }
async function handleDelete(ctx, db, action) { switch (action) { case 'deletePost': const { postId: deletePostId } = ctx.args;
const deleteResult = await db.collection('posts').deleteOne({ _id: deletePostId });
if (deleteResult.deletedCount === 0) { return { error: 'Post not found', code: 404 }; }
return { success: true, message: 'Post deleted successfully' };
default: return { error: 'Invalid action', code: 400 }; } }
|