Spaces:
Sleeping
Sleeping
File size: 3,725 Bytes
55ffa7e 83f4356 55ffa7e 83f4356 55ffa7e 83f4356 | 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 | const { supabaseAdmin, supabaseServiceRole } = require('../config/supabase');
const { success, created, error, notFound, forbidden, serverError } = require('../utils/response');
const { parsePagination } = require('../utils/pagination');
/**
* POST /api/posts/:postId/comments — add a comment to a post
*/
async function addComment(req, res) {
try {
const { postId } = req.params;
const { content } = req.body;
if (!content || content.trim().length === 0) {
return error(res, 'Comment content is required', 400);
}
if (content.length > 1000) {
return error(res, 'Comment must be under 1000 characters', 400);
}
// Verify post exists
const { data: post } = await supabaseAdmin
.from('posts')
.select('id')
.eq('id', postId)
.single();
if (!post) return notFound(res, 'Post not found');
const { data, error: dbError } = await supabaseAdmin
.from('comments')
.insert({
post_id: postId,
user_id: req.user.id,
content: content.trim(),
})
.select('*, profiles!inner(username, display_name, avatar_url)')
.single();
if (dbError) return error(res, dbError.message, 400);
// Increment comment count (service role: the counter functions are not
// callable by end users)
const { error: countError } = await supabaseServiceRole.rpc('increment_comments', { p_post_id: postId });
if (countError) console.error('increment_comments failed:', countError);
return created(res, data);
} catch (err) {
console.error('addComment error:', err);
return serverError(res);
}
}
/**
* GET /api/posts/:postId/comments — list comments for a post (paginated)
*/
async function listComments(req, res) {
try {
const { postId } = req.params;
const { page, limit, offset } = parsePagination(req.query);
const { data, error: dbError, count } = await supabaseAdmin
.from('comments')
.select('*, profiles!inner(username, display_name, avatar_url)', { count: 'exact' })
.eq('post_id', postId)
.order('created_at', { ascending: true })
.range(offset, offset + limit - 1);
if (dbError) return error(res, dbError.message, 400);
return success(res, data, { page, limit, total: count });
} catch (err) {
console.error('listComments error:', err);
return serverError(res);
}
}
/**
* DELETE /api/posts/:postId/comments/:commentId — delete own comment
*/
async function deleteComment(req, res) {
try {
const { postId, commentId } = req.params;
const { data: comment } = await supabaseAdmin
.from('comments')
.select('user_id')
.eq('id', commentId)
.eq('post_id', postId)
.single();
if (!comment) return notFound(res, 'Comment not found');
if (comment.user_id !== req.user.id) return forbidden(res, 'You can only delete your own comments');
const { data: deleted, error: deleteError } = await supabaseAdmin
.from('comments')
.delete()
.eq('id', commentId)
.select('id');
if (deleteError) {
console.error('deleteComment error:', deleteError);
return serverError(res, 'Could not delete the comment');
}
// Count down only if a row was really removed
if (deleted && deleted.length > 0) {
const { error: countError } = await supabaseServiceRole.rpc('decrement_comments', { p_post_id: postId });
if (countError) console.error('decrement_comments failed:', countError);
}
return success(res, { message: 'Comment deleted' });
} catch (err) {
console.error('deleteComment error:', err);
return serverError(res);
}
}
module.exports = { addComment, listComments, deleteComment };
|