init
This commit is contained in:
111
backend/dist/controllers/commentsController.js
vendored
Normal file
111
backend/dist/controllers/commentsController.js
vendored
Normal file
@@ -0,0 +1,111 @@
|
||||
"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;
|
||||
116
backend/dist/controllers/influencersController.js
vendored
Normal file
116
backend/dist/controllers/influencersController.js
vendored
Normal file
@@ -0,0 +1,116 @@
|
||||
"use strict";
|
||||
var __importDefault = (this && this.__importDefault) || function (mod) {
|
||||
return (mod && mod.__esModule) ? mod : { "default": mod };
|
||||
};
|
||||
Object.defineProperty(exports, "__esModule", { value: true });
|
||||
exports.getInfluencerStats = exports.getInfluencerById = exports.getInfluencers = void 0;
|
||||
const supabase_1 = __importDefault(require("../utils/supabase"));
|
||||
const getInfluencers = async (c) => {
|
||||
try {
|
||||
const { platform, limit = '10', offset = '0', min_followers, max_followers, sort_by = 'followers_count', sort_order = 'desc' } = c.req.query();
|
||||
let query = supabase_1.default
|
||||
.from('influencers')
|
||||
.select(`
|
||||
influencer_id,
|
||||
name,
|
||||
platform,
|
||||
profile_url,
|
||||
followers_count,
|
||||
video_count,
|
||||
platform_count,
|
||||
created_at,
|
||||
updated_at
|
||||
`);
|
||||
// Apply filters
|
||||
if (platform) {
|
||||
query = query.eq('platform', platform);
|
||||
}
|
||||
if (min_followers) {
|
||||
query = query.gte('followers_count', Number(min_followers));
|
||||
}
|
||||
if (max_followers) {
|
||||
query = query.lte('followers_count', Number(max_followers));
|
||||
}
|
||||
// Apply sorting
|
||||
if (sort_by && ['followers_count', 'video_count', 'created_at'].includes(sort_by)) {
|
||||
query = query.order(sort_by, { ascending: sort_order === 'asc' });
|
||||
}
|
||||
// Apply pagination
|
||||
query = query.range(Number(offset), Number(offset) + Number(limit) - 1);
|
||||
const { data: influencers, error, count } = await query;
|
||||
if (error) {
|
||||
return c.json({ error: error.message }, 500);
|
||||
}
|
||||
return c.json({
|
||||
influencers,
|
||||
count,
|
||||
limit: Number(limit),
|
||||
offset: Number(offset)
|
||||
});
|
||||
}
|
||||
catch (error) {
|
||||
return c.json({ error: 'Internal server error' }, 500);
|
||||
}
|
||||
};
|
||||
exports.getInfluencers = getInfluencers;
|
||||
const getInfluencerById = async (c) => {
|
||||
try {
|
||||
const { influencer_id } = c.req.param();
|
||||
const { data: influencer, error } = await supabase_1.default
|
||||
.from('influencers')
|
||||
.select(`
|
||||
influencer_id,
|
||||
name,
|
||||
platform,
|
||||
profile_url,
|
||||
followers_count,
|
||||
video_count,
|
||||
platform_count,
|
||||
created_at,
|
||||
updated_at,
|
||||
posts (
|
||||
post_id,
|
||||
title,
|
||||
description,
|
||||
published_at
|
||||
)
|
||||
`)
|
||||
.eq('influencer_id', influencer_id)
|
||||
.single();
|
||||
if (error) {
|
||||
return c.json({ error: 'Influencer not found' }, 404);
|
||||
}
|
||||
return c.json(influencer);
|
||||
}
|
||||
catch (error) {
|
||||
return c.json({ error: 'Internal server error' }, 500);
|
||||
}
|
||||
};
|
||||
exports.getInfluencerById = getInfluencerById;
|
||||
const getInfluencerStats = async (c) => {
|
||||
try {
|
||||
const { platform } = c.req.query();
|
||||
let query = supabase_1.default
|
||||
.from('influencers')
|
||||
.select('platform, followers_count, video_count');
|
||||
if (platform) {
|
||||
query = query.eq('platform', platform);
|
||||
}
|
||||
const { data: stats, error } = await query;
|
||||
if (error) {
|
||||
return c.json({ error: error.message }, 500);
|
||||
}
|
||||
const aggregatedStats = {
|
||||
total_influencers: stats.length,
|
||||
total_followers: stats.reduce((sum, item) => sum + (item.followers_count || 0), 0),
|
||||
total_videos: stats.reduce((sum, item) => sum + (item.video_count || 0), 0),
|
||||
average_followers: Math.round(stats.reduce((sum, item) => sum + (item.followers_count || 0), 0) / (stats.length || 1)),
|
||||
average_videos: Math.round(stats.reduce((sum, item) => sum + (item.video_count || 0), 0) / (stats.length || 1))
|
||||
};
|
||||
return c.json(aggregatedStats);
|
||||
}
|
||||
catch (error) {
|
||||
return c.json({ error: 'Internal server error' }, 500);
|
||||
}
|
||||
};
|
||||
exports.getInfluencerStats = getInfluencerStats;
|
||||
Reference in New Issue
Block a user