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
const pool = new Pool(dbConfig);
export async function GET() {
try {
// Log connection attempt with sanitized config
const sanitizedConfig = {
user: dbConfig.user,
host: dbConfig.host,
database: dbConfig.database,
port: dbConfig.port,
hasPassword: !!dbConfig.password,
ssl: !!dbConfig.ssl
};
console.log('Attempting database connection with config:', sanitizedConfig);
// Mock data for development
const MOCK_NEWS = [
{
id: 1,
title: "New Project Management Features Released",
url: "#",
date: "2024-03-20",
source: "Internal",
description: "New features added to improve project management workflow",
category: "Update",
sentiment: { score: null, label: null },
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();
console.log('Successfully connected to database');
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
console.log('Executing news query...');
const result = await client.query(`
SELECT
id,
@ -72,20 +86,15 @@ export async function GET() {
source,
description,
category,
sentiment_score as "sentimentScore",
sentiment_score,
sentiment,
symbols,
symbol
FROM news
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
const news = result.rows.map(row => ({
id: row.id,
@ -96,50 +105,24 @@ export async function GET() {
description: row.description,
category: row.category,
sentiment: {
score: row.sentimentScore,
score: row.sentiment_score,
label: row.sentiment
},
symbols: Array.isArray(row.symbols) ? row.symbols : null,
symbols: row.symbols,
symbol: row.symbol
}));
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 {
// Release the client back to the pool
client.release();
console.log('Database client released');
}
} catch (error: any) {
console.error('Database connection 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(
{ error: 'Database configuration is incomplete' },
{ status: 500 }
);
}
// Return detailed error message
} catch (error) {
console.error('Database error:', error);
return NextResponse.json(
{
error: 'Failed to connect to database',
details: error.message,
code: error.code
},
{ error: 'Failed to fetch news' },
{ status: 500 }
);
}
*/
}