Include political statements feature files
This commit is contained in:
99
lib/politicalStatements.ts
Normal file
99
lib/politicalStatements.ts
Normal file
@@ -0,0 +1,99 @@
|
||||
import { promises as fs } from 'fs';
|
||||
import path from 'path';
|
||||
|
||||
export type PoliticalStatement = {
|
||||
id: number;
|
||||
text: string;
|
||||
active?: boolean;
|
||||
source?: string;
|
||||
};
|
||||
|
||||
function getFilePath(locale: string): string {
|
||||
const safeLocale = ['de', 'en'].includes(locale) ? locale : 'en';
|
||||
return path.join(process.cwd(), 'data', `political-statements.${safeLocale}.json`);
|
||||
}
|
||||
|
||||
async function readStatementsFile(locale: string): Promise<PoliticalStatement[]> {
|
||||
const filePath = getFilePath(locale);
|
||||
try {
|
||||
const raw = await fs.readFile(filePath, 'utf-8');
|
||||
const data = JSON.parse(raw);
|
||||
if (Array.isArray(data)) {
|
||||
return data;
|
||||
}
|
||||
return [];
|
||||
} catch (err: any) {
|
||||
if (err.code === 'ENOENT') {
|
||||
// File does not exist yet
|
||||
return [];
|
||||
}
|
||||
console.error('[politicalStatements] Failed to read file', filePath, err);
|
||||
return [];
|
||||
}
|
||||
}
|
||||
|
||||
async function writeStatementsFile(locale: string, statements: PoliticalStatement[]): Promise<void> {
|
||||
const filePath = getFilePath(locale);
|
||||
const dir = path.dirname(filePath);
|
||||
try {
|
||||
await fs.mkdir(dir, { recursive: true });
|
||||
await fs.writeFile(filePath, JSON.stringify(statements, null, 2), 'utf-8');
|
||||
} catch (err) {
|
||||
console.error('[politicalStatements] Failed to write file', filePath, err);
|
||||
throw err;
|
||||
}
|
||||
}
|
||||
|
||||
export async function getRandomActiveStatement(locale: string): Promise<PoliticalStatement | null> {
|
||||
const statements = await readStatementsFile(locale);
|
||||
const active = statements.filter((s) => s.active !== false);
|
||||
if (active.length === 0) {
|
||||
return null;
|
||||
}
|
||||
const index = Math.floor(Math.random() * active.length);
|
||||
return active[index] ?? null;
|
||||
}
|
||||
|
||||
export async function getAllStatements(locale: string): Promise<PoliticalStatement[]> {
|
||||
return readStatementsFile(locale);
|
||||
}
|
||||
|
||||
export async function createStatement(locale: string, input: Omit<PoliticalStatement, 'id'>): Promise<PoliticalStatement> {
|
||||
const statements = await readStatementsFile(locale);
|
||||
const nextId = statements.length > 0 ? Math.max(...statements.map((s) => s.id)) + 1 : 1;
|
||||
const newStatement: PoliticalStatement = {
|
||||
id: nextId,
|
||||
active: true,
|
||||
...input,
|
||||
};
|
||||
statements.push(newStatement);
|
||||
await writeStatementsFile(locale, statements);
|
||||
return newStatement;
|
||||
}
|
||||
|
||||
export async function updateStatement(locale: string, id: number, input: Partial<Omit<PoliticalStatement, 'id'>>): Promise<PoliticalStatement | null> {
|
||||
const statements = await readStatementsFile(locale);
|
||||
const index = statements.findIndex((s) => s.id === id);
|
||||
if (index === -1) return null;
|
||||
|
||||
const updated: PoliticalStatement = {
|
||||
...statements[index],
|
||||
...input,
|
||||
id,
|
||||
};
|
||||
statements[index] = updated;
|
||||
await writeStatementsFile(locale, statements);
|
||||
return updated;
|
||||
}
|
||||
|
||||
export async function deleteStatement(locale: string, id: number): Promise<boolean> {
|
||||
const statements = await readStatementsFile(locale);
|
||||
const filtered = statements.filter((s) => s.id !== id);
|
||||
if (filtered.length === statements.length) {
|
||||
return false;
|
||||
}
|
||||
await writeStatementsFile(locale, filtered);
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user