Refactor BookRepo methods and improve error handling
- Add JSDoc comments for better maintainability and code clarity in `BookRepo` methods. - Streamline query definitions using variables to improve readability. - Consolidate error handling logic across all methods. - Ensure multilingual support in error messages for consistent user feedback. - Remove redundant error branches and simplify unknown error processing.
This commit is contained in:
@@ -1,4 +1,4 @@
|
||||
import {Database, QueryResult, RunResult, SQLiteValue} from "node-sqlite3-wasm";
|
||||
import { Database, QueryResult, RunResult, SQLiteValue } from "node-sqlite3-wasm";
|
||||
import System from "@/electron/database/System";
|
||||
|
||||
export interface BookWorldTable extends Record<string, SQLiteValue> {
|
||||
@@ -61,15 +61,25 @@ export interface WorldElementValue {
|
||||
type: number;
|
||||
}
|
||||
|
||||
export default class WorldRepository {
|
||||
public static checkWorldExist(userId:string,bookId:string,worldName:string, lang: 'fr' | 'en'):boolean{
|
||||
export default class WorldRepository {
|
||||
/**
|
||||
* Checks if a world with the given name exists for a specific user and book.
|
||||
* @param userId - The unique identifier of the user
|
||||
* @param bookId - The unique identifier of the book
|
||||
* @param worldName - The hashed name of the world to check
|
||||
* @param lang - The language for error messages ('fr' or 'en')
|
||||
* @returns True if the world exists, false otherwise
|
||||
*/
|
||||
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}`);
|
||||
const query: string = 'SELECT world_id FROM book_world WHERE author_id=? AND book_id=? AND hashed_name=?';
|
||||
const params: SQLiteValue[] = [userId, bookId, worldName];
|
||||
const world: QueryResult | null = db.get(query, params);
|
||||
return world !== null;
|
||||
} catch (error: unknown) {
|
||||
if (error instanceof Error) {
|
||||
console.error(`DB Error: ${error.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.");
|
||||
@@ -77,33 +87,56 @@ export default class WorldRepository {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Inserts a new world into the database.
|
||||
* @param worldId - The unique identifier for the new world
|
||||
* @param userId - The unique identifier of the author
|
||||
* @param bookId - The unique identifier of the book
|
||||
* @param encryptedName - The encrypted name of the world
|
||||
* @param hashedName - The hashed name of the world for uniqueness checks
|
||||
* @param lang - The language for error messages ('fr' or 'en')
|
||||
* @returns The world ID if insertion was successful
|
||||
*/
|
||||
public static insertNewWorld(worldId: string, userId: string, bookId: string, encryptedName: string, hashedName: string, lang: 'fr' | 'en'): string {
|
||||
let result: RunResult;
|
||||
let insertResult: 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}`);
|
||||
const query: string = 'INSERT INTO book_world (world_id, author_id, book_id, name, hashed_name, last_update) VALUES (?, ?, ?, ?, ?, ?)';
|
||||
const params: SQLiteValue[] = [worldId, userId, bookId, encryptedName, hashedName, System.timeStampInSeconds()];
|
||||
insertResult = db.run(query, params);
|
||||
} catch (error: unknown) {
|
||||
if (error instanceof Error) {
|
||||
console.error(`DB Error: ${error.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) {
|
||||
if (!insertResult || insertResult.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[] {
|
||||
|
||||
/**
|
||||
* Fetches all worlds and their elements for a specific user and book.
|
||||
* @param userId - The unique identifier of the user
|
||||
* @param bookId - The unique identifier of the book
|
||||
* @param lang - The language for error messages ('fr' or 'en')
|
||||
* @returns An array of world query results with joined element data
|
||||
*/
|
||||
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}`);
|
||||
const query: string = `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=?`;
|
||||
const params: SQLiteValue[] = [userId, bookId];
|
||||
const worlds: WorldQuery[] = db.all(query, params) as WorldQuery[];
|
||||
return worlds;
|
||||
} catch (error: unknown) {
|
||||
if (error instanceof Error) {
|
||||
console.error(`DB Error: ${error.message}`);
|
||||
throw new Error(lang === 'fr' ? `Impossible de récupérer les mondes.` : `Unable to retrieve worlds.`);
|
||||
} else {
|
||||
console.error("An unknown error occurred.");
|
||||
@@ -111,15 +144,32 @@ export default class WorldRepository {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Updates a world's data in the database.
|
||||
* @param userId - The unique identifier of the author
|
||||
* @param worldId - The unique identifier of the world to update
|
||||
* @param encryptName - The new encrypted name
|
||||
* @param hashedName - The new hashed name
|
||||
* @param encryptHistory - The new encrypted history
|
||||
* @param encryptPolitics - The new encrypted politics
|
||||
* @param encryptEconomy - The new encrypted economy
|
||||
* @param encryptReligion - The new encrypted religion
|
||||
* @param encryptLanguages - The new encrypted languages
|
||||
* @param lastUpdate - The timestamp of the last update
|
||||
* @param lang - The language for error messages ('fr' or 'en')
|
||||
* @returns True if the update was successful, false otherwise
|
||||
*/
|
||||
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}`);
|
||||
const query: string = 'UPDATE book_world SET name=?, hashed_name=?, history=?, politics=?, economy=?, religion=?, languages=?, last_update=? WHERE author_id=? AND world_id=?';
|
||||
const params: SQLiteValue[] = [encryptName, hashedName, encryptHistory, encryptPolitics, encryptEconomy, encryptReligion, encryptLanguages, lastUpdate, userId, worldId];
|
||||
const updateResult: RunResult = db.run(query, params);
|
||||
return updateResult.changes > 0;
|
||||
} catch (error: unknown) {
|
||||
if (error instanceof Error) {
|
||||
console.error(`DB Error: ${error.message}`);
|
||||
throw new Error(lang === 'fr' ? `Impossible de mettre à jour le monde.` : `Unable to update world.`);
|
||||
} else {
|
||||
console.error("An unknown error occurred.");
|
||||
@@ -127,20 +177,29 @@ export default class WorldRepository {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Updates multiple world elements in the database.
|
||||
* @param userId - The unique identifier of the user
|
||||
* @param elements - An array of world element values to update
|
||||
* @param lang - The language for error messages ('fr' or 'en')
|
||||
* @returns True if all updates were successful, false otherwise
|
||||
*/
|
||||
public static updateWorldElements(userId: string, elements: WorldElementValue[], lang: 'fr' | 'en'): boolean {
|
||||
try {
|
||||
const db: Database = System.getDb();
|
||||
const query: string = 'UPDATE book_world_elements SET name=?, description=?, element_type=?, last_update=? WHERE user_id=? AND element_id=?';
|
||||
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) {
|
||||
const params: SQLiteValue[] = [element.name, element.description, element.type, System.timeStampInSeconds(), userId, element.id];
|
||||
const updateResult: RunResult = db.run(query, params);
|
||||
if (updateResult.changes <= 0) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
return true;
|
||||
} catch (e: unknown) {
|
||||
if (e instanceof Error) {
|
||||
console.error(`DB Error: ${e.message}`);
|
||||
} catch (error: unknown) {
|
||||
if (error instanceof Error) {
|
||||
console.error(`DB Error: ${error.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.");
|
||||
@@ -148,15 +207,24 @@ export default class WorldRepository {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Checks if a world element with the given hashed name exists.
|
||||
* @param worldNumId - The unique identifier of the world
|
||||
* @param hashedName - The hashed name of the element to check
|
||||
* @param lang - The language for error messages ('fr' or 'en')
|
||||
* @returns True if the element exists, false otherwise
|
||||
*/
|
||||
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}`);
|
||||
const query: string = 'SELECT element_id FROM book_world_elements WHERE world_id=? AND original_name=?';
|
||||
const params: SQLiteValue[] = [worldNumId, hashedName];
|
||||
const element: QueryResult | null = db.get(query, params);
|
||||
return element !== null;
|
||||
} catch (error: unknown) {
|
||||
if (error instanceof Error) {
|
||||
console.error(`DB Error: ${error.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.");
|
||||
@@ -164,35 +232,57 @@ export default class WorldRepository {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Inserts a new world element into the database.
|
||||
* @param userId - The unique identifier of the user
|
||||
* @param elementId - The unique identifier for the new element
|
||||
* @param elementType - The type of the element
|
||||
* @param worldId - The unique identifier of the parent world
|
||||
* @param encryptedName - The encrypted name of the element
|
||||
* @param hashedName - The hashed name of the element for uniqueness checks
|
||||
* @param lang - The language for error messages ('fr' or 'en')
|
||||
* @returns The element ID if insertion was successful
|
||||
*/
|
||||
public static insertNewElement(userId: string, elementId: string, elementType: number, worldId: string, encryptedName: string, hashedName: string, lang: 'fr' | 'en'): string {
|
||||
let result: RunResult;
|
||||
let insertResult: 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}`);
|
||||
const query: string = 'INSERT INTO book_world_elements (element_id, world_id, user_id, name, original_name, element_type, last_update) VALUES (?, ?, ?, ?, ?, ?, ?)';
|
||||
const params: SQLiteValue[] = [elementId, worldId, userId, encryptedName, hashedName, elementType, System.timeStampInSeconds()];
|
||||
insertResult = db.run(query, params);
|
||||
} catch (error: unknown) {
|
||||
if (error instanceof Error) {
|
||||
console.error(`DB Error: ${error.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) {
|
||||
if (!insertResult || insertResult.changes === 0) {
|
||||
throw new Error(lang === 'fr' ? `Erreur lors de l'ajout de l'élément.` : `Error adding element.`);
|
||||
}
|
||||
return elementId;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Deletes a world element from the database.
|
||||
* @param userId - The unique identifier of the user
|
||||
* @param elementId - The unique identifier of the element to delete
|
||||
* @param lang - The language for error messages ('fr' or 'en')
|
||||
* @returns True if the deletion was successful, false otherwise
|
||||
*/
|
||||
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}`);
|
||||
const query: string = 'DELETE FROM book_world_elements WHERE user_id=? AND element_id=?';
|
||||
const params: SQLiteValue[] = [userId, elementId];
|
||||
const deleteResult: RunResult = db.run(query, params);
|
||||
return deleteResult.changes > 0;
|
||||
} catch (error: unknown) {
|
||||
if (error instanceof Error) {
|
||||
console.error(`DB Error: ${error.message}`);
|
||||
throw new Error(lang === 'fr' ? `Impossible de supprimer l'élément.` : `Unable to delete element.`);
|
||||
} else {
|
||||
console.error("An unknown error occurred.");
|
||||
@@ -200,42 +290,71 @@ export default class WorldRepository {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Fetches all worlds for a specific user and book.
|
||||
* @param userId - The unique identifier of the user
|
||||
* @param bookId - The unique identifier of the book
|
||||
* @param lang - The language for error messages ('fr' or 'en')
|
||||
* @returns A promise resolving to an array of book world table records
|
||||
*/
|
||||
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}`);
|
||||
const query: string = '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=?';
|
||||
const params: SQLiteValue[] = [userId, bookId];
|
||||
const worlds: BookWorldTable[] = db.all(query, params) as BookWorldTable[];
|
||||
return worlds;
|
||||
} catch (error: unknown) {
|
||||
if (error instanceof Error) {
|
||||
console.error(`DB Error: ${error.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[]> {
|
||||
|
||||
/**
|
||||
* Fetches all elements for a specific world.
|
||||
* @param userId - The unique identifier of the user
|
||||
* @param worldId - The unique identifier of the world
|
||||
* @param lang - The language for error messages ('fr' or 'en')
|
||||
* @returns A promise resolving to an array of book world elements table records
|
||||
*/
|
||||
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}`);
|
||||
const query: string = '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=?';
|
||||
const params: SQLiteValue[] = [userId, worldId];
|
||||
const elements: BookWorldElementsTable[] = db.all(query, params) as BookWorldElementsTable[];
|
||||
return elements;
|
||||
} catch (error: unknown) {
|
||||
if (error instanceof Error) {
|
||||
console.error(`DB Error: ${error.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.");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Fetches all synced worlds for a specific user.
|
||||
* @param userId - The unique identifier of the user
|
||||
* @param lang - The language for error messages ('fr' or 'en')
|
||||
* @returns An array of synced world results
|
||||
*/
|
||||
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}`);
|
||||
const query: string = 'SELECT world_id, book_id, name, last_update FROM book_world WHERE author_id = ?';
|
||||
const params: SQLiteValue[] = [userId];
|
||||
const syncedWorlds: SyncedWorldResult[] = db.all(query, params) as SyncedWorldResult[];
|
||||
return syncedWorlds;
|
||||
} catch (error: unknown) {
|
||||
if (error instanceof Error) {
|
||||
console.error(`DB Error: ${error.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.");
|
||||
@@ -243,14 +362,23 @@ export default class WorldRepository {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Fetches all synced world elements for a specific user.
|
||||
* @param userId - The unique identifier of the user
|
||||
* @param lang - The language for error messages ('fr' or 'en')
|
||||
* @returns An array of synced world element results
|
||||
*/
|
||||
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}`);
|
||||
const query: string = 'SELECT element_id, world_id, name, last_update FROM book_world_elements WHERE user_id = ?';
|
||||
const params: SQLiteValue[] = [userId];
|
||||
const syncedElements: SyncedWorldElementResult[] = db.all(query, params) as SyncedWorldElementResult[];
|
||||
return syncedElements;
|
||||
} catch (error: unknown) {
|
||||
if (error instanceof Error) {
|
||||
console.error(`DB Error: ${error.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.");
|
||||
@@ -258,90 +386,134 @@ export default class WorldRepository {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Inserts a synced world into the database.
|
||||
* @param worldId - The unique identifier for the world
|
||||
* @param name - The encrypted name of the world
|
||||
* @param hashedName - The hashed name of the world
|
||||
* @param authorId - The unique identifier of the author
|
||||
* @param bookId - The unique identifier of the book
|
||||
* @param history - The encrypted history (optional)
|
||||
* @param politics - The encrypted politics (optional)
|
||||
* @param economy - The encrypted economy (optional)
|
||||
* @param religion - The encrypted religion (optional)
|
||||
* @param languages - The encrypted languages (optional)
|
||||
* @param lastUpdate - The timestamp of the last update
|
||||
* @param lang - The language for error messages ('fr' or 'en')
|
||||
* @returns True if the insertion was successful, false otherwise
|
||||
*/
|
||||
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}`);
|
||||
const query: string = `INSERT INTO book_world (world_id, name, hashed_name, author_id, book_id, history, politics, economy, religion, languages, last_update) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)`;
|
||||
const params: SQLiteValue[] = [worldId, name, hashedName, authorId, bookId, history, politics, economy, religion, languages, lastUpdate];
|
||||
const insertResult: RunResult = db.run(query, params);
|
||||
return insertResult.changes > 0;
|
||||
} catch (error: unknown) {
|
||||
if (error instanceof Error) {
|
||||
console.error(`DB Error: ${error.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.");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Inserts a synced world element into the database.
|
||||
* @param elementId - The unique identifier for the element
|
||||
* @param worldId - The unique identifier of the parent world
|
||||
* @param userId - The unique identifier of the user
|
||||
* @param elementType - The type of the element
|
||||
* @param name - The encrypted name of the element
|
||||
* @param originalName - The original hashed name
|
||||
* @param description - The encrypted description (optional)
|
||||
* @param lastUpdate - The timestamp of the last update
|
||||
* @param lang - The language for error messages ('fr' or 'en')
|
||||
* @returns True if the insertion was successful, false otherwise
|
||||
*/
|
||||
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}`);
|
||||
const query: string = `INSERT INTO book_world_elements (element_id, world_id, user_id, element_type, name, original_name, description, last_update) VALUES (?, ?, ?, ?, ?, ?, ?, ?)`;
|
||||
const params: SQLiteValue[] = [elementId, worldId, userId, elementType, name, originalName, description, lastUpdate];
|
||||
const insertResult: RunResult = db.run(query, params);
|
||||
return insertResult.changes > 0;
|
||||
} catch (error: unknown) {
|
||||
if (error instanceof Error) {
|
||||
console.error(`DB Error: ${error.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[]> {
|
||||
|
||||
/**
|
||||
* Fetches a complete world by its ID.
|
||||
* @param id - The unique identifier of the world
|
||||
* @param lang - The language for error messages ('fr' or 'en')
|
||||
* @returns A promise resolving to an array of book world table records
|
||||
*/
|
||||
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) {
|
||||
const query: string = `SELECT world_id, name, hashed_name, author_id, book_id, history, politics, economy, religion, languages, last_update FROM book_world WHERE world_id = ?`;
|
||||
const params: SQLiteValue[] = [id];
|
||||
const worlds: BookWorldTable[] = db.all(query, params) as BookWorldTable[];
|
||||
return worlds;
|
||||
} catch (error: unknown) {
|
||||
if (error 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[]> {
|
||||
|
||||
/**
|
||||
* Fetches a complete world element by its ID.
|
||||
* @param id - The unique identifier of the element
|
||||
* @param lang - The language for error messages ('fr' or 'en')
|
||||
* @returns A promise resolving to an array of book world elements table records
|
||||
*/
|
||||
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) {
|
||||
const query: string = `SELECT element_id, world_id, user_id, element_type, name, original_name, description, last_update FROM book_world_elements WHERE element_id = ?`;
|
||||
const params: SQLiteValue[] = [id];
|
||||
const elements: BookWorldElementsTable[] = db.all(query, params) as BookWorldElementsTable[];
|
||||
return elements;
|
||||
} catch (error: unknown) {
|
||||
if (error 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 {
|
||||
|
||||
/**
|
||||
* Updates a single world element's name and description.
|
||||
* @param userId - The unique identifier of the user
|
||||
* @param elementId - The unique identifier of the element to update
|
||||
* @param name - The new encrypted name
|
||||
* @param description - The new encrypted description
|
||||
* @param lastUpdate - The timestamp of the last update
|
||||
* @param lang - The language for error messages ('fr' or 'en')
|
||||
* @returns True if the update was successful, false otherwise
|
||||
*/
|
||||
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}`);
|
||||
const query: string = `UPDATE book_world_elements SET name = ?, description = ?, last_update = ? WHERE element_id = ? AND user_id = ?`;
|
||||
const params: SQLiteValue[] = [name, description, lastUpdate, elementId, userId];
|
||||
const updateResult: RunResult = db.run(query, params);
|
||||
return updateResult.changes > 0;
|
||||
} catch (error: unknown) {
|
||||
if (error instanceof Error) {
|
||||
console.error(`DB Error: ${error.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.");
|
||||
@@ -349,34 +521,54 @@ export default class WorldRepository {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
static worldExist(userId: string, bookId: string, world_id: string,lang: "fr" | "en"): boolean {
|
||||
|
||||
/**
|
||||
* Checks if a world exists for a specific user and book.
|
||||
* @param userId - The unique identifier of the user
|
||||
* @param bookId - The unique identifier of the book
|
||||
* @param worldId - The unique identifier of the world
|
||||
* @param lang - The language for error messages ('fr' or 'en')
|
||||
* @returns True if the world exists, false otherwise
|
||||
*/
|
||||
static worldExist(userId: string, bookId: string, worldId: 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}`);
|
||||
const query: string = 'SELECT 1 FROM book_world WHERE world_id=? AND author_id=? AND book_id=?';
|
||||
const params: SQLiteValue[] = [worldId, userId, bookId];
|
||||
const world: QueryResult | null = db.get(query, params) || null;
|
||||
return world !== null;
|
||||
} catch (error: unknown) {
|
||||
if (error instanceof Error) {
|
||||
console.error(`DB Error: ${error.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 {
|
||||
|
||||
/**
|
||||
* Checks if a world element exists for a specific user and world.
|
||||
* @param userId - The unique identifier of the user
|
||||
* @param worldId - The unique identifier of the world
|
||||
* @param elementId - The unique identifier of the element
|
||||
* @param lang - The language for error messages ('fr' or 'en')
|
||||
* @returns True if the element exists, false otherwise
|
||||
*/
|
||||
static worldElementExist(userId: string, worldId: string, elementId: 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}`);
|
||||
const query: string = 'SELECT 1 FROM book_world_elements WHERE element_id=? AND world_id=? AND user_id=?';
|
||||
const params: SQLiteValue[] = [elementId, worldId, userId];
|
||||
const element: QueryResult | null = db.get(query, params) || null;
|
||||
return element !== null;
|
||||
} catch (error: unknown) {
|
||||
if (error instanceof Error) {
|
||||
console.error(`DB Error: ${error.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.");
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user