- Deleted unused database table interfaces and repository methods for book acts, chapters, characters, locations, worlds, and related entities. - Reduced code complexity by consolidating remaining logic under streamlined methods in `BookRepo`. - Improved maintainability by cleaning up outdated query interfaces and redundant synchronization code.
382 lines
20 KiB
TypeScript
382 lines
20 KiB
TypeScript
import {Database, QueryResult, RunResult, SQLiteValue} from "node-sqlite3-wasm";
|
|
import System from "@/electron/database/System";
|
|
|
|
export interface BookWorldTable extends Record<string, SQLiteValue> {
|
|
world_id: string;
|
|
name: string;
|
|
hashed_name: string;
|
|
author_id: string;
|
|
book_id: string;
|
|
history: string | null;
|
|
politics: string | null;
|
|
economy: string | null;
|
|
religion: string | null;
|
|
languages: string | null;
|
|
last_update: number;
|
|
}
|
|
|
|
export interface BookWorldElementsTable extends Record<string, SQLiteValue> {
|
|
element_id: string;
|
|
world_id: string;
|
|
user_id: string;
|
|
element_type: number;
|
|
name: string;
|
|
original_name: string;
|
|
description: string | null;
|
|
last_update: number;
|
|
}
|
|
|
|
export interface SyncedWorldResult extends Record<string, SQLiteValue> {
|
|
world_id: string;
|
|
book_id: string;
|
|
name: string;
|
|
last_update: number;
|
|
}
|
|
|
|
export interface SyncedWorldElementResult extends Record<string, SQLiteValue> {
|
|
element_id: string;
|
|
world_id: string;
|
|
name: string;
|
|
last_update: number;
|
|
}
|
|
|
|
export interface WorldQuery extends Record<string, SQLiteValue> {
|
|
world_id: string;
|
|
world_name: string;
|
|
history: string | null;
|
|
politics: string | null;
|
|
economy: string | null;
|
|
religion: string | null;
|
|
languages: string | null;
|
|
element_id: string | null;
|
|
element_name: string | null;
|
|
element_description: string | null;
|
|
element_type: number | null;
|
|
}
|
|
|
|
export interface WorldElementValue {
|
|
id: string;
|
|
name: string;
|
|
description: string;
|
|
type: number;
|
|
}
|
|
|
|
export default class WorldRepository {
|
|
public static checkWorldExist(userId:string,bookId:string,worldName:string, lang: 'fr' | 'en'):boolean{
|
|
try {
|
|
const db: Database = System.getDb();
|
|
const result = db.get('SELECT world_id FROM book_world WHERE author_id=? AND book_id=? AND hashed_name=?', [userId,bookId,worldName]);
|
|
return result !== null;
|
|
} catch (e: unknown) {
|
|
if (e instanceof Error) {
|
|
console.error(`DB Error: ${e.message}`);
|
|
throw new Error(lang === 'fr' ? `Impossible de vérifier l'existence du monde.` : `Unable to verify world existence.`);
|
|
} else {
|
|
console.error("An unknown error occurred.");
|
|
throw new Error(lang === 'fr' ? "Une erreur inconnue s'est produite." : "An unknown error occurred.");
|
|
}
|
|
}
|
|
}
|
|
|
|
public static insertNewWorld(worldId: string, userId: string, bookId: string, encryptedName: string, hashedName: string, lang: 'fr' | 'en'): string {
|
|
let result: RunResult;
|
|
try {
|
|
const db: Database = System.getDb();
|
|
result = db.run('INSERT INTO book_world (world_id,author_id, book_id, name, hashed_name, last_update) VALUES (?,?,?,?,?,?)', [worldId, userId, bookId, encryptedName, hashedName, System.timeStampInSeconds()]);
|
|
} catch (e: unknown) {
|
|
if (e instanceof Error) {
|
|
console.error(`DB Error: ${e.message}`);
|
|
throw new Error(lang === 'fr' ? `Impossible d'ajouter le monde.` : `Unable to add world.`);
|
|
} else {
|
|
console.error("An unknown error occurred.");
|
|
throw new Error(lang === 'fr' ? "Une erreur inconnue s'est produite." : "An unknown error occurred.");
|
|
}
|
|
}
|
|
if (!result || result.changes === 0) {
|
|
throw new Error(lang === 'fr' ? `Erreur lors de l'ajout du monde.` : `Error adding world.`);
|
|
}
|
|
return worldId;
|
|
}
|
|
public static fetchWorlds(userId: string, bookId: string, lang: 'fr' | 'en'):WorldQuery[] {
|
|
try {
|
|
const db: Database = System.getDb();
|
|
return db.all('SELECT world.world_id AS world_id, world.name AS world_name, world.history, world.politics, world.economy, world.religion, world.languages, element.element_id AS element_id, element.name AS element_name, element.description AS element_description, element.element_type FROM book_world AS world LEFT JOIN book_world_elements AS element ON world.world_id=element.world_id WHERE world.author_id=? AND world.book_id=?', [userId, bookId]) as WorldQuery[];
|
|
} catch (e: unknown) {
|
|
if (e instanceof Error) {
|
|
console.error(`DB Error: ${e.message}`);
|
|
throw new Error(lang === 'fr' ? `Impossible de récupérer les mondes.` : `Unable to retrieve worlds.`);
|
|
} else {
|
|
console.error("An unknown error occurred.");
|
|
throw new Error(lang === 'fr' ? "Une erreur inconnue s'est produite." : "An unknown error occurred.");
|
|
}
|
|
}
|
|
}
|
|
|
|
public static updateWorld(userId: string, worldId: string, encryptName: string, hashedName: string, encryptHistory: string, encryptPolitics: string, encryptEconomy: string, encryptReligion: string, encryptLanguages: string, lastUpdate: number, lang: 'fr' | 'en'): boolean {
|
|
try {
|
|
const db: Database = System.getDb();
|
|
const result: RunResult = db.run('UPDATE book_world SET name=?, hashed_name=?, history=?, politics=?, economy=?, religion=?, languages=?, last_update=? WHERE author_id=? AND world_id=?', [encryptName, hashedName, encryptHistory, encryptPolitics, encryptEconomy, encryptReligion, encryptLanguages, lastUpdate, userId, worldId]);
|
|
return result.changes > 0;
|
|
} catch (e: unknown) {
|
|
if (e instanceof Error) {
|
|
console.error(`DB Error: ${e.message}`);
|
|
throw new Error(lang === 'fr' ? `Impossible de mettre à jour le monde.` : `Unable to update world.`);
|
|
} else {
|
|
console.error("An unknown error occurred.");
|
|
throw new Error(lang === 'fr' ? "Une erreur inconnue s'est produite." : "An unknown error occurred.");
|
|
}
|
|
}
|
|
}
|
|
|
|
public static updateWorldElements(userId: string, elements: WorldElementValue[], lang: 'fr' | 'en'): boolean {
|
|
try {
|
|
const db: Database = System.getDb();
|
|
for (const element of elements) {
|
|
const result: RunResult = db.run('UPDATE book_world_elements SET name=?, description=?, element_type=?, last_update=? WHERE user_id=? AND element_id=?', [element.name, element.description, element.type, System.timeStampInSeconds(), userId, element.id]);
|
|
if (result.changes <= 0) {
|
|
return false;
|
|
}
|
|
}
|
|
return true;
|
|
} catch (e: unknown) {
|
|
if (e instanceof Error) {
|
|
console.error(`DB Error: ${e.message}`);
|
|
throw new Error(lang === 'fr' ? `Impossible de mettre à jour les éléments du monde.` : `Unable to update world elements.`);
|
|
} else {
|
|
console.error("An unknown error occurred.");
|
|
throw new Error(lang === 'fr' ? "Une erreur inconnue s'est produite." : "An unknown error occurred.");
|
|
}
|
|
}
|
|
}
|
|
|
|
public static checkElementExist(worldNumId: string, hashedName: string, lang: 'fr' | 'en'): boolean {
|
|
try {
|
|
const db: Database = System.getDb();
|
|
const result = db.get('SELECT element_id FROM book_world_elements WHERE world_id=? AND original_name=?', [worldNumId, hashedName]);
|
|
return result !== null;
|
|
} catch (e: unknown) {
|
|
if (e instanceof Error) {
|
|
console.error(`DB Error: ${e.message}`);
|
|
throw new Error(lang === 'fr' ? `Impossible de vérifier l'existence de l'élément.` : `Unable to verify element existence.`);
|
|
} else {
|
|
console.error("An unknown error occurred.");
|
|
throw new Error(lang === 'fr' ? "Une erreur inconnue s'est produite." : "An unknown error occurred.");
|
|
}
|
|
}
|
|
}
|
|
|
|
public static insertNewElement(userId: string, elementId: string, elementType: number, worldId: string, encryptedName: string, hashedName: string, lang: 'fr' | 'en'): string {
|
|
let result: RunResult;
|
|
try {
|
|
const db: Database = System.getDb();
|
|
result = db.run('INSERT INTO book_world_elements (element_id,world_id,user_id, name, original_name, element_type, last_update) VALUES (?,?,?,?,?,?,?)', [elementId, worldId, userId, encryptedName, hashedName, elementType, System.timeStampInSeconds()]);
|
|
} catch (e: unknown) {
|
|
if (e instanceof Error) {
|
|
console.error(`DB Error: ${e.message}`);
|
|
throw new Error(lang === 'fr' ? `Impossible d'ajouter l'élément.` : `Unable to add element.`);
|
|
} else {
|
|
console.error("An unknown error occurred.");
|
|
throw new Error(lang === 'fr' ? "Une erreur inconnue s'est produite." : "An unknown error occurred.");
|
|
}
|
|
}
|
|
if (!result || result.changes === 0) {
|
|
throw new Error(lang === 'fr' ? `Erreur lors de l'ajout de l'élément.` : `Error adding element.`);
|
|
}
|
|
return elementId;
|
|
}
|
|
|
|
public static deleteElement(userId: string, elementId: string, lang: 'fr' | 'en'): boolean {
|
|
try {
|
|
const db: Database = System.getDb();
|
|
const result: RunResult = db.run('DELETE FROM book_world_elements WHERE user_id=? AND element_id=?', [userId, elementId]);
|
|
return result.changes > 0;
|
|
} catch (e: unknown) {
|
|
if (e instanceof Error) {
|
|
console.error(`DB Error: ${e.message}`);
|
|
throw new Error(lang === 'fr' ? `Impossible de supprimer l'élément.` : `Unable to delete element.`);
|
|
} else {
|
|
console.error("An unknown error occurred.");
|
|
throw new Error(lang === 'fr' ? "Une erreur inconnue s'est produite." : "An unknown error occurred.");
|
|
}
|
|
}
|
|
}
|
|
|
|
static async fetchBookWorlds(userId: string, bookId: string, lang: 'fr' | 'en'): Promise<BookWorldTable[]> {
|
|
try {
|
|
const db: Database = System.getDb();
|
|
return db.all('SELECT world_id, name, hashed_name, author_id, book_id, history, politics, economy, religion, languages, last_update FROM book_world WHERE author_id=? AND book_id=?', [userId, bookId]) as BookWorldTable[];
|
|
} catch (e: unknown) {
|
|
if (e instanceof Error) {
|
|
console.error(`DB Error: ${e.message}`);
|
|
throw new Error(lang === 'fr' ? `Impossible de récupérer les mondes.` : `Unable to retrieve worlds.`);
|
|
} else {
|
|
throw new Error(lang === 'fr' ? "Une erreur inconnue s'est produite." : "An unknown error occurred.");
|
|
}
|
|
}
|
|
}
|
|
|
|
static async fetchBookWorldElements(userId: string,worldId:string, lang: 'fr' | 'en'): Promise<BookWorldElementsTable[]> {
|
|
try {
|
|
const db: Database = System.getDb();
|
|
return db.all('SELECT element_id, world_id, user_id, element_type, name, original_name, description, last_update FROM book_world_elements WHERE user_id=? AND world_id=?', [userId, worldId]) as BookWorldElementsTable[];
|
|
} catch (e: unknown) {
|
|
if (e instanceof Error) {
|
|
console.error(`DB Error: ${e.message}`);
|
|
throw new Error(lang === 'fr' ? `Impossible de récupérer les éléments du monde.` : `Unable to retrieve world elements.`);
|
|
} else {
|
|
throw new Error(lang === 'fr' ? "Une erreur inconnue s'est produite." : "An unknown error occurred.");
|
|
}
|
|
}
|
|
}
|
|
|
|
static fetchSyncedWorlds(userId: string, lang: 'fr' | 'en'): SyncedWorldResult[] {
|
|
try {
|
|
const db: Database = System.getDb();
|
|
return db.all('SELECT world_id, book_id, name, last_update FROM book_world WHERE author_id = ?', [userId]) as SyncedWorldResult[];
|
|
} catch (e: unknown) {
|
|
if (e instanceof Error) {
|
|
console.error(`DB Error: ${e.message}`);
|
|
throw new Error(lang === 'fr' ? `Impossible de récupérer les mondes synchronisés.` : `Unable to retrieve synced worlds.`);
|
|
} else {
|
|
console.error("An unknown error occurred.");
|
|
throw new Error(lang === 'fr' ? "Une erreur inconnue s'est produite." : "An unknown error occurred.");
|
|
}
|
|
}
|
|
}
|
|
|
|
static fetchSyncedWorldElements(userId: string, lang: 'fr' | 'en'): SyncedWorldElementResult[] {
|
|
try {
|
|
const db: Database = System.getDb();
|
|
return db.all('SELECT element_id, world_id, name, last_update FROM book_world_elements WHERE user_id = ?', [userId]) as SyncedWorldElementResult[];
|
|
} catch (e: unknown) {
|
|
if (e instanceof Error) {
|
|
console.error(`DB Error: ${e.message}`);
|
|
throw new Error(lang === 'fr' ? `Impossible de récupérer les éléments de monde synchronisés.` : `Unable to retrieve synced world elements.`);
|
|
} else {
|
|
console.error("An unknown error occurred.");
|
|
throw new Error(lang === 'fr' ? "Une erreur inconnue s'est produite." : "An unknown error occurred.");
|
|
}
|
|
}
|
|
}
|
|
|
|
static insertSyncWorld(worldId: string, name: string, hashedName: string, authorId: string, bookId: string, history: string | null, politics: string | null, economy: string | null, religion: string | null, languages: string | null, lastUpdate: number, lang: 'fr' | 'en'): boolean {
|
|
try {
|
|
const db: Database = System.getDb();
|
|
const result: RunResult = db.run(
|
|
`INSERT INTO book_world (world_id, name, hashed_name, author_id, book_id, history, politics, economy, religion, languages, last_update)
|
|
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)`,
|
|
[worldId, name, hashedName, authorId, bookId, history, politics, economy, religion, languages, lastUpdate]
|
|
);
|
|
return result.changes > 0;
|
|
} catch (e: unknown) {
|
|
if (e instanceof Error) {
|
|
console.error(`DB Error: ${e.message}`);
|
|
throw new Error(lang === 'fr' ? `Impossible d'insérer le monde.` : `Unable to insert world.`);
|
|
} else {
|
|
throw new Error(lang === 'fr' ? "Une erreur inconnue s'est produite." : "An unknown error occurred.");
|
|
}
|
|
}
|
|
}
|
|
|
|
static insertSyncWorldElement(elementId: string, worldId: string, userId: string, elementType: number, name: string, originalName: string, description: string | null, lastUpdate: number, lang: 'fr' | 'en'): boolean {
|
|
try {
|
|
const db: Database = System.getDb();
|
|
const result: RunResult = db.run(
|
|
`INSERT INTO book_world_elements (element_id, world_id, user_id, element_type, name, original_name, description, last_update)
|
|
VALUES (?, ?, ?, ?, ?, ?, ?, ?)`,
|
|
[elementId, worldId, userId, elementType, name, originalName, description, lastUpdate]
|
|
);
|
|
return result.changes > 0;
|
|
} catch (e: unknown) {
|
|
if (e instanceof Error) {
|
|
console.error(`DB Error: ${e.message}`);
|
|
throw new Error(lang === 'fr' ? `Impossible d'insérer l'élément du monde.` : `Unable to insert world element.`);
|
|
} else {
|
|
throw new Error(lang === 'fr' ? "Une erreur inconnue s'est produite." : "An unknown error occurred.");
|
|
}
|
|
}
|
|
}
|
|
|
|
static async fetchCompleteWorldById(id: string, lang: "fr" | "en"):Promise<BookWorldTable[]> {
|
|
try {
|
|
const db: Database = System.getDb();
|
|
return db.all(
|
|
`SELECT world_id, name, hashed_name, author_id, book_id, history, politics, economy, religion, languages, last_update
|
|
FROM book_world
|
|
WHERE world_id = ?`,
|
|
[id]
|
|
) as BookWorldTable[];
|
|
} catch (e:unknown){
|
|
if (e instanceof Error) {
|
|
throw new Error(lang === 'fr' ? `Impossible de récupérer le monde complet.` : `Unable to retrieve complete world.`);
|
|
} else {
|
|
throw new Error(lang === 'fr' ? "Une erreur inconnue s'est produite." : "An unknown error occurred.");
|
|
}
|
|
}
|
|
}
|
|
|
|
static async fetchCompleteWorldElementById(id: string, lang: "fr" | "en"):Promise<BookWorldElementsTable[]> {
|
|
try {
|
|
const db: Database = System.getDb();
|
|
return db.all(
|
|
`SELECT element_id, world_id, user_id, element_type, name, original_name, description, last_update
|
|
FROM book_world_elements
|
|
WHERE element_id = ?`,
|
|
[id]
|
|
) as BookWorldElementsTable[];
|
|
} catch (e:unknown){
|
|
if (e instanceof Error) {
|
|
throw new Error(lang === 'fr' ? `Impossible de récupérer l'élément de monde complet.` : `Unable to retrieve complete world element.`);
|
|
} else {
|
|
throw new Error(lang === 'fr' ? "Une erreur inconnue s'est produite." : "An unknown error occurred.");
|
|
}
|
|
}
|
|
}
|
|
static updateWorldElement(userId: string, elementId: string, name: string, description: string, lastUpdate: number,lang: "fr" | "en"):boolean {
|
|
try {
|
|
const db: Database = System.getDb();
|
|
const query:string = `UPDATE book_world_elements SET name = ?, description = ?, last_update = FROM_UNIXTIME(?) WHERE element_id = UUID_TO_BIN(?) AND user_id = UUID_TO_BIN(?)`;
|
|
const params:(string|number)[] = [name, description, lastUpdate, elementId, userId];
|
|
const result:RunResult = db.run(query, params);
|
|
return result.changes > 0;
|
|
} catch (e:unknown) {
|
|
if (e instanceof Error) {
|
|
console.error(`DB Error: ${e.message}`);
|
|
throw new Error(lang === 'fr' ? `Impossible de mettre à jour l'élément du monde.` : `Unable to update world element.`);
|
|
} else {
|
|
console.error("An unknown error occurred.");
|
|
throw new Error(lang === 'fr' ? "Une erreur inconnue s'est produite." : "An unknown error occurred.");
|
|
}
|
|
}
|
|
}
|
|
|
|
static worldExist(userId: string, bookId: string, world_id: string,lang: "fr" | "en"): boolean {
|
|
try {
|
|
const db: Database = System.getDb();
|
|
const result: QueryResult | null = db.get('SELECT 1 FROM `book_world` WHERE `world_id`=? AND `author_id`=? AND `book_id`=?', [world_id, userId, bookId]) || null;
|
|
return result !== null;
|
|
} catch (e: unknown) {
|
|
if (e instanceof Error) {
|
|
console.error(`DB Error: ${e.message}`);
|
|
throw new Error(lang === 'fr' ? `Impossible de vérifier l'existence du monde.` : `Unable to check world existence.`);
|
|
} else {
|
|
throw new Error(lang === 'fr' ? "Une erreur inconnue s'est produite." : "An unknown error occurred.");
|
|
}
|
|
}
|
|
}
|
|
|
|
static worldElementExist(userId: string, world_id: string, element_id: string, lang: "fr" | "en"): boolean {
|
|
try {
|
|
const db: Database = System.getDb();
|
|
const result: QueryResult | null = db.get('SELECT 1 FROM `book_world_elements` WHERE `element_id`=? AND `world_id`=? AND `user_id`=?', [element_id, world_id, userId]) || null;
|
|
return result !== null;
|
|
} catch (e: unknown) {
|
|
if (e instanceof Error) {
|
|
console.error(`DB Error: ${e.message}`);
|
|
throw new Error(lang === 'fr' ? `Impossible de vérifier l'existence de l'élément du monde.` : `Unable to check world element existence.`);
|
|
} else {
|
|
throw new Error(lang === 'fr' ? "Une erreur inconnue s'est produite." : "An unknown error occurred.");
|
|
}
|
|
}
|
|
}
|
|
} |