import {Database, QueryResult, RunResult, SQLiteValue} from 'node-sqlite3-wasm'; import System from "../System.js"; export interface BookQuery extends Record { book_id: string; type: string; author_id: string; title: string; hashed_title: string; sub_title: string | null; hashed_sub_title: string | null; summary: string | null; serie_id: number | null; desired_release_date: string | null; desired_word_count: number | null; words_count: number | null; cover_image: string | null; } export interface EritBooksTable extends Record { book_id: string; type: string; author_id: string; title: string; hashed_title: string; sub_title: string | null; hashed_sub_title: string | null; summary: string | null; serie_id: number | null; desired_release_date: string | null; desired_word_count: number | null; words_count: number | null; last_update: number; cover_image: string | null; } export interface SyncedBookResult extends Record { book_id: string; type: string; title: string; sub_title: string | null; last_update: number; } export interface BookCoverQuery extends Record { cover_image: string; } export default class BookRepo { public static fetchBooks(userId: string, lang: 'fr' | 'en'): BookQuery[] { try { const db: Database = System.getDb(); return db.all('SELECT book_id, type, author_id, title, sub_title, summary, serie_id, desired_release_date, desired_word_count, words_count, cover_image FROM erit_books WHERE author_id = ? ORDER BY book_id DESC', [userId]) as BookQuery[]; } catch (error: unknown) { if (error instanceof Error) { console.error(error.message); throw new Error(lang === 'fr' ? 'Impossible de récupérer la liste des livres.' : 'Unable to retrieve book list.'); } else { console.error(error); throw new Error(lang === 'fr' ? 'Une erreur inconnue est survenue.' : 'An unknown error occurred.'); } } } public static updateBookCover(bookId:string,coverImageName:string,userId:string, lang: 'fr' | 'en'):boolean{ try { const db: Database = System.getDb(); const result:RunResult = db.run('UPDATE `erit_books` SET cover_image=?, last_update=? WHERE `book_id`=? AND author_id=?', [coverImageName, System.timeStampInSeconds(), bookId, userId]); 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 la couverture du livre.` : `Unable to update book cover.`); } else { console.error("An unknown error occurred."); throw new Error(lang === 'fr' ? "Une erreur inconnue s'est produite." : "An unknown error occurred."); } } } public static fetchBook(bookId: string, userId: string, lang: 'fr' | 'en'): BookQuery { let result: BookQuery; try { const db: Database = System.getDb(); result = db.get('SELECT book_id, author_id, title, summary, sub_title, cover_image, desired_release_date, desired_word_count, words_count FROM erit_books WHERE book_id=? AND author_id=?', [bookId, userId]) as BookQuery; } catch (error: unknown) { if (error instanceof Error) { console.error(`DB Error: ${error.message}`); throw new Error(lang === 'fr' ? `Impossible de récupérer les informations du livre.` : `Unable to retrieve book information.`); } else { console.error("An unknown error occurred."); throw new Error(lang === 'fr' ? "Une erreur inconnue s'est produite." : "An unknown error occurred."); } } if (!result) { throw new Error(lang === 'fr' ? `Livre non trouvé.` : `Book not found.`); } return result; } public static verifyBookExist(hashedTitle:string,hashedSubTitle:string,userId:string, lang: 'fr' | 'en'):boolean{ try { const db: Database = System.getDb(); const result:QueryResult|null = db.get('SELECT book_id FROM erit_books WHERE hashed_title=? AND author_id=? AND erit_books.hashed_sub_title=?', [hashedTitle,userId,hashedSubTitle]); return result!==null; } catch (err: unknown) { if (err instanceof Error) { console.error(`DB Error: ${err.message}`); throw new Error(lang === 'fr' ? `Impossible de vérifier l'existence du livre.` : `Unable to verify book 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 insertBook(bookId: string, userId: string, encryptedTitle: string, hashedTitle: string, encryptedSubTitle: string, hashedSubTitle: string, encryptedSummary: string, type: string, serie: number, publicationDate: string, desiredWordCount: number, lang: 'fr' | 'en'): string { let result: RunResult; try { const db: Database = System.getDb(); result = db.run('INSERT INTO erit_books (book_id, type, author_id, title, hashed_title, sub_title, hashed_sub_title, summary, serie_id, desired_release_date, desired_word_count, last_update) VALUES (?,?,?,?,?,?,?,?,?,?,?,?)', [bookId, type, userId, encryptedTitle, hashedTitle, encryptedSubTitle, hashedSubTitle, encryptedSummary, serie, publicationDate ? publicationDate : null, desiredWordCount, System.timeStampInSeconds()]); } catch (err: unknown) { if (err instanceof Error) { console.error(`DB Error: ${err.message}`); throw new Error(lang === 'fr' ? `Impossible d'ajouter le livre.` : `Unable to add book.`); } 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 livre.` : `Error adding book.`); } return bookId; } public static fetchBookCover(userId:string,bookId:string, lang: 'fr' | 'en'):BookCoverQuery{ try { const db: Database = System.getDb(); return db.get('SELECT cover_image FROM erit_books WHERE author_id=? AND book_id=?', [userId, bookId]) as BookCoverQuery; } catch (e: unknown) { if (e instanceof Error) { console.error(`DB Error: ${e.message}`); throw new Error(lang === 'fr' ? `Impossible de récupérer la couverture du livre.` : `Unable to retrieve book cover.`); } else { console.error("An unknown error occurred."); throw new Error(lang === 'fr' ? "Une erreur inconnue s'est produite." : "An unknown error occurred."); } } } static updateBookBasicInformation(userId: string, title: string, hashedTitle: string, subTitle: string, hashedSubTitle: string, summary: string, publicationDate: string, wordCount: number, bookId: string, lang: 'fr' | 'en'): boolean { try { const db: Database = System.getDb(); const result: RunResult = db.run('UPDATE erit_books SET title=?, hashed_title=?, sub_title=?, hashed_sub_title=?, summary=?, serie_id=?, desired_release_date=?, desired_word_count=?, last_update=? WHERE author_id=? AND book_id=?', [title, hashedTitle, subTitle, hashedSubTitle, summary, 0, publicationDate ? System.dateToMySqlDate(publicationDate) : null, wordCount, System.timeStampInSeconds(), userId, bookId]); 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 les informations du livre.` : `Unable to update book information.`); } else { console.error("An unknown error occurred."); throw new Error(lang === 'fr' ? "Une erreur inconnue s'est produite." : "An unknown error occurred."); } } } public static deleteBook(userId: string, bookId: string, lang: 'fr' | 'en'): boolean { try { const db: Database = System.getDb(); const result: RunResult = db.run('DELETE FROM erit_books WHERE author_id=? AND book_id=?', [userId,bookId]); 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 le livre.` : `Unable to delete book.`); } else { console.error("An unknown error occurred."); throw new Error(lang === 'fr' ? "Une erreur inconnue s'est produite." : "An unknown error occurred."); } } } static async fetchEritBooksTable(userId:string,bookId:string, lang: 'fr' | 'en'):Promise{ try { const db: Database = System.getDb(); return db.all('SELECT book_id, type, author_id, title, hashed_title, sub_title, hashed_sub_title, summary, serie_id, desired_release_date, desired_word_count, words_count, cover_image, last_update FROM erit_books WHERE book_id=? AND author_id=?', [bookId, userId]) as EritBooksTable[]; } catch (e:unknown) { if (e instanceof Error) { console.error(`DB Error: ${e.message}`); throw new Error(lang === 'fr' ? `Impossible de récupérer les informations du livre.` : `Unable to retrieve book information.`); } else { console.error("An unknown error occurred."); throw new Error(lang === 'fr' ? "Une erreur inconnue s'est produite." : "An unknown error occurred."); } } } static fetchSyncedBooks(userId: string, lang: 'fr' | 'en'): SyncedBookResult[] { try { const db: Database = System.getDb(); return db.all('SELECT book_id, type, title, sub_title, last_update FROM erit_books WHERE author_id = ?', [userId]) as SyncedBookResult[]; } catch (e: unknown) { if (e instanceof Error) { console.error(`DB Error: ${e.message}`); throw new Error(lang === 'fr' ? `Impossible de récupérer les livres synchronisés.` : `Unable to retrieve synced books.`); } else { console.error("An unknown error occurred."); throw new Error(lang === 'fr' ? "Une erreur inconnue s'est produite." : "An unknown error occurred."); } } } static insertSyncBook(bookId: string, userId: string, type: string, title: string, hashedTitle: string, subTitle: string | null, hashedSubTitle: string | null, summary: string | null, serieId: number | null, desiredReleaseDate: string | null, desiredWordCount: number | null, wordsCount: number | null, coverImage: string | null, lastUpdate: number, lang: 'fr' | 'en'): boolean { try { const db: Database = System.getDb(); const result: RunResult = db.run( `INSERT INTO erit_books (book_id, author_id, type, title, hashed_title, sub_title, hashed_sub_title, summary, serie_id, desired_release_date, desired_word_count, words_count, cover_image, last_update) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)`, [bookId, userId, type, title, hashedTitle, subTitle, hashedSubTitle, summary, serieId, desiredReleaseDate, desiredWordCount, wordsCount, coverImage, 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 livre synchronisé.` : `Unable to insert synced book.`); } else { throw new Error(lang === 'fr' ? "Une erreur inconnue s'est produite." : "An unknown error occurred."); } } } static async fetchCompleteBookById(bookId: string, lang: "fr" | "en") { try { const db: Database = System.getDb(); return db.all(`SELECT * FROM erit_books WHERE book_id = ?`, [bookId]) as EritBooksTable[]; } catch (e: unknown) { if (e instanceof Error) { throw new Error(lang === 'fr' ? `Impossible de récupérer le livre complet.` : `Unable to retrieve complete book.`); } else { throw new Error(lang === 'fr' ? "Une erreur inconnue s'est produite." : "An unknown error occurred."); } } } }