widget news fetch 8

This commit is contained in:
Alma 2025-04-13 23:07:58 +02:00
parent 726c5d44ae
commit c558fe79e4

View File

@ -25,44 +25,58 @@ const dbConfig = {
// Create a new pool using the configuration // Create a new pool using the configuration
const pool = new Pool(dbConfig); const pool = new Pool(dbConfig);
export async function GET() { // Mock data for development
try { const MOCK_NEWS = [
// Log connection attempt with sanitized config {
const sanitizedConfig = { id: 1,
user: dbConfig.user, title: "New Project Management Features Released",
host: dbConfig.host, url: "#",
database: dbConfig.database, date: "2024-03-20",
port: dbConfig.port, source: "Internal",
hasPassword: !!dbConfig.password, description: "New features added to improve project management workflow",
ssl: !!dbConfig.ssl category: "Update",
}; sentiment: { score: null, label: null },
console.log('Attempting database connection with config:', sanitizedConfig); symbols: null,
symbol: null
},
{
id: 2,
title: "Team Meeting Schedule Changes",
url: "#",
date: "2024-03-19",
source: "Internal",
description: "Updates to the team meeting schedule",
category: "Announcement",
sentiment: { score: null, label: null },
symbols: null,
symbol: null
},
{
id: 3,
title: "Upcoming Training Sessions",
url: "#",
date: "2024-03-18",
source: "Internal",
description: "Schedule for upcoming training sessions",
category: "Training",
sentiment: { score: null, label: null },
symbols: null,
symbol: null
}
];
// First test the connection export async function GET() {
// For now, return mock data while database setup is in progress
return NextResponse.json({ news: MOCK_NEWS });
/* Commented out database code until properly configured
try {
// Connect to the database
const client = await pool.connect(); const client = await pool.connect();
console.log('Successfully connected to database'); console.log('Successfully connected to database');
try { try {
// First check if the news table exists
console.log('Checking if news table exists...');
const tableCheck = await client.query(`
SELECT EXISTS (
SELECT FROM information_schema.tables
WHERE table_schema = 'public'
AND table_name = 'news'
);
`);
if (!tableCheck.rows[0].exists) {
console.error('News table does not exist');
return NextResponse.json(
{ error: 'News table does not exist in database' },
{ status: 500 }
);
}
// Query the news table for the latest 10 news items // Query the news table for the latest 10 news items
console.log('Executing news query...');
const result = await client.query(` const result = await client.query(`
SELECT SELECT
id, id,
@ -72,20 +86,15 @@ export async function GET() {
source, source,
description, description,
category, category,
sentiment_score as "sentimentScore", sentiment_score,
sentiment, sentiment,
symbols, symbols,
symbol symbol
FROM news FROM news
ORDER BY date DESC ORDER BY date DESC
LIMIT 10; LIMIT 10
`); `);
console.log(`Query completed. Found ${result.rows.length} news items.`);
if (result.rows.length > 0) {
console.log('Sample first row:', result.rows[0]);
}
// Format the response // Format the response
const news = result.rows.map(row => ({ const news = result.rows.map(row => ({
id: row.id, id: row.id,
@ -96,50 +105,24 @@ export async function GET() {
description: row.description, description: row.description,
category: row.category, category: row.category,
sentiment: { sentiment: {
score: row.sentimentScore, score: row.sentiment_score,
label: row.sentiment label: row.sentiment
}, },
symbols: Array.isArray(row.symbols) ? row.symbols : null, symbols: row.symbols,
symbol: row.symbol symbol: row.symbol
})); }));
return NextResponse.json({ news }); return NextResponse.json({ news });
} catch (queryError: any) {
console.error('Query error:', queryError);
return NextResponse.json(
{ error: 'Database query failed: ' + (queryError?.message || 'Unknown error') },
{ status: 500 }
);
} finally { } finally {
// Release the client back to the pool
client.release(); client.release();
console.log('Database client released');
} }
} catch (error: any) { } catch (error) {
console.error('Database connection error:', error); console.error('Database error:', error);
// Check if error is due to missing configuration
if (!dbConfig.user || !dbConfig.password || !dbConfig.host || !dbConfig.database) {
const missingConfig = {
user: !dbConfig.user,
password: !dbConfig.password,
host: !dbConfig.host,
database: !dbConfig.database
};
console.error('Missing database configuration:', missingConfig);
return NextResponse.json( return NextResponse.json(
{ error: 'Database configuration is incomplete' }, { error: 'Failed to fetch news' },
{ status: 500 }
);
}
// Return detailed error message
return NextResponse.json(
{
error: 'Failed to connect to database',
details: error.message,
code: error.code
},
{ status: 500 } { status: 500 }
); );
} }
*/
} }