112 lines
3.6 KiB
JavaScript
112 lines
3.6 KiB
JavaScript
"use strict";
|
|
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
};
|
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
exports.deleteComment = exports.createComment = exports.getComments = void 0;
|
|
const supabase_1 = __importDefault(require("../utils/supabase"));
|
|
const getComments = async (c) => {
|
|
try {
|
|
const { post_id, limit = '10', offset = '0' } = c.req.query();
|
|
let query;
|
|
if (post_id) {
|
|
// 获取特定帖子的评论
|
|
query = supabase_1.default.rpc('get_comments_for_post', { post_id_param: post_id });
|
|
}
|
|
else {
|
|
// 获取所有评论
|
|
query = supabase_1.default.rpc('get_comments_with_posts');
|
|
}
|
|
// 应用分页
|
|
query = query.range(Number(offset), Number(offset) + Number(limit) - 1);
|
|
const { data: comments, error, count } = await query;
|
|
if (error) {
|
|
return c.json({ error: error.message }, 500);
|
|
}
|
|
return c.json({
|
|
comments,
|
|
count,
|
|
limit: Number(limit),
|
|
offset: Number(offset)
|
|
});
|
|
}
|
|
catch (error) {
|
|
return c.json({ error: 'Internal server error' }, 500);
|
|
}
|
|
};
|
|
exports.getComments = getComments;
|
|
const createComment = async (c) => {
|
|
try {
|
|
const { post_id, content } = await c.req.json();
|
|
const user_id = c.get('user')?.id;
|
|
if (!user_id) {
|
|
return c.json({ error: 'Unauthorized' }, 401);
|
|
}
|
|
const { data: comment, error } = await supabase_1.default
|
|
.from('comments')
|
|
.insert({
|
|
post_id,
|
|
content,
|
|
user_id
|
|
})
|
|
.select(`
|
|
comment_id,
|
|
content,
|
|
sentiment_score,
|
|
created_at,
|
|
updated_at,
|
|
post_id,
|
|
user_id
|
|
`)
|
|
.single();
|
|
if (error) {
|
|
return c.json({ error: error.message }, 500);
|
|
}
|
|
// 获取用户信息
|
|
const { data: userProfile, error: userError } = await supabase_1.default
|
|
.from('user_profiles')
|
|
.select('id, full_name, avatar_url')
|
|
.eq('id', user_id)
|
|
.single();
|
|
if (!userError && userProfile) {
|
|
comment.user_profile = userProfile;
|
|
}
|
|
return c.json(comment, 201);
|
|
}
|
|
catch (error) {
|
|
return c.json({ error: 'Internal server error' }, 500);
|
|
}
|
|
};
|
|
exports.createComment = createComment;
|
|
const deleteComment = async (c) => {
|
|
try {
|
|
const { comment_id } = c.req.param();
|
|
const user_id = c.get('user')?.id;
|
|
if (!user_id) {
|
|
return c.json({ error: 'Unauthorized' }, 401);
|
|
}
|
|
// Check if the comment belongs to the user
|
|
const { data: comment, error: fetchError } = await supabase_1.default
|
|
.from('comments')
|
|
.select()
|
|
.eq('comment_id', comment_id)
|
|
.eq('user_id', user_id)
|
|
.single();
|
|
if (fetchError || !comment) {
|
|
return c.json({ error: 'Comment not found or unauthorized' }, 404);
|
|
}
|
|
const { error: deleteError } = await supabase_1.default
|
|
.from('comments')
|
|
.delete()
|
|
.eq('comment_id', comment_id);
|
|
if (deleteError) {
|
|
return c.json({ error: deleteError.message }, 500);
|
|
}
|
|
return c.body(null, 204);
|
|
}
|
|
catch (error) {
|
|
return c.json({ error: 'Internal server error' }, 500);
|
|
}
|
|
};
|
|
exports.deleteComment = deleteComment;
|