- 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.
496 lines
26 KiB
TypeScript
496 lines
26 KiB
TypeScript
import {Database, QueryResult, RunResult, SQLiteValue} from 'node-sqlite3-wasm';
|
|
import System from "../System.js";
|
|
import {BookQuery, EritBooksTable, SyncedBookResult} from "@/electron/database/repositories/book.repository";
|
|
|
|
export interface ChapterQueryResult extends Record<string, SQLiteValue>{
|
|
chapter_id: string;
|
|
title: string;
|
|
chapter_order: number;
|
|
}
|
|
|
|
export interface ActChapterQuery extends Record<string, SQLiteValue>{
|
|
chapter_info_id: number;
|
|
chapter_id: string;
|
|
title: string;
|
|
chapter_order: number;
|
|
act_id: number;
|
|
incident_id: string | null;
|
|
plot_point_id: string | null;
|
|
summary: string;
|
|
goal: string;
|
|
}
|
|
|
|
export interface ChapterStoryQueryResult extends Record<string, SQLiteValue>{
|
|
chapter_info_id: number;
|
|
act_id: number;
|
|
summary: string;
|
|
chapter_summary: string;
|
|
chapter_goal: string;
|
|
incident_id: number;
|
|
incident_title: string;
|
|
incident_summary: string;
|
|
plot_point_id: number;
|
|
plot_title: string;
|
|
plot_summary: string;
|
|
}
|
|
|
|
export interface LastChapterResult extends Record<string, SQLiteValue>{
|
|
chapter_id: string;
|
|
version: number;
|
|
}
|
|
|
|
export interface BookChaptersTable extends Record<string, SQLiteValue> {
|
|
chapter_id: string;
|
|
book_id: string;
|
|
author_id: string;
|
|
title: string;
|
|
hashed_title: string;
|
|
words_count: number | null;
|
|
chapter_order: number;
|
|
last_update: number;
|
|
}
|
|
|
|
export interface BookChapterInfosTable extends Record<string, SQLiteValue> {
|
|
chapter_info_id: string;
|
|
chapter_id: string;
|
|
act_id: number;
|
|
incident_id: string | null;
|
|
plot_point_id: string | null;
|
|
book_id: string;
|
|
author_id: string;
|
|
summary: string | null;
|
|
goal: string | null;
|
|
last_update: number;
|
|
}
|
|
|
|
export interface SyncedChapterResult extends Record<string, SQLiteValue> {
|
|
chapter_id: string;
|
|
book_id: string;
|
|
title: string;
|
|
last_update: number;
|
|
}
|
|
|
|
export interface SyncedChapterInfoResult extends Record<string, SQLiteValue> {
|
|
chapter_info_id: string;
|
|
chapter_id: string | null;
|
|
book_id: string;
|
|
last_update: number;
|
|
}
|
|
|
|
export interface ChapterBookResult extends Record<string, SQLiteValue> {
|
|
title: string;
|
|
chapter_order: number;
|
|
content: string | null;
|
|
}
|
|
|
|
export default class ChapterRepo{
|
|
public static checkNameDuplication(userId:string,bookId:string,hashedTitle:string, lang: 'fr' | 'en' = 'fr'):boolean{
|
|
try {
|
|
const db: Database = System.getDb();
|
|
const result = db.get('SELECT chapter_id FROM book_chapters WHERE author_id=? AND book_id=? AND hashed_title=?', [userId,bookId,hashedTitle]);
|
|
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 la duplication du nom.` : `Unable to verify name duplication.`);
|
|
} else {
|
|
console.error("An unknown error occurred.");
|
|
throw new Error(lang === 'fr' ? "Une erreur inconnue s'est produite." : "An unknown error occurred.");
|
|
}
|
|
}
|
|
}
|
|
|
|
public static insertChapter(chapterId: string, userId: string, bookId: string, title: string, hashedTitle: string, wordsCount: number, chapterOrder: number, lang: 'fr' | 'en' = 'fr'): string {
|
|
let result: RunResult;
|
|
try {
|
|
const db: Database = System.getDb();
|
|
result = db.run('INSERT INTO book_chapters (chapter_id, author_id, book_id, title, hashed_title, words_count, chapter_order, last_update) VALUES (?,?,?,?,?,?,?,?)', [chapterId, userId, bookId, title, hashedTitle, wordsCount, chapterOrder, System.timeStampInSeconds()]);
|
|
} catch (e: unknown) {
|
|
if (e instanceof Error) {
|
|
console.error(`DB Error: ${e.message}`);
|
|
throw new Error(lang === 'fr' ? `Impossible d'ajouter le chapitre.` : `Unable to add chapter.`);
|
|
} 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' ? `Une erreur s'est passé lors de l'ajout du chapitre.` : `Error adding chapter.`);
|
|
}
|
|
return chapterId;
|
|
}
|
|
public static fetchAllChapterForActs(userId:string,bookId:string, lang: 'fr' | 'en' = 'fr'):ActChapterQuery[]{
|
|
try {
|
|
const db: Database = System.getDb();
|
|
return db.all('SELECT ci.chapter_info_id AS chapter_info_id, ci.chapter_id AS chapter_id, chapter.title, chapter.chapter_order, ci.act_id, ci.incident_id AS incident_id, ci.plot_point_id AS plot_point_id, ci.summary, ci.goal FROM book_chapter_infos AS ci INNER JOIN book_chapters AS chapter ON chapter.chapter_id = ci.chapter_id WHERE ci.book_id = ? AND ci.author_id = ?', [bookId, userId]) as ActChapterQuery[];
|
|
} catch (e: unknown) {
|
|
if (e instanceof Error) {
|
|
console.error(`DB Error: ${e.message}`);
|
|
throw new Error(lang === 'fr' ? `Impossible de récupérer les chapitres pour les actes.` : `Unable to retrieve chapters for acts.`);
|
|
} else {
|
|
console.error("An unknown error occurred.");
|
|
throw new Error(lang === 'fr' ? "Une erreur inconnue s'est produite." : "An unknown error occurred.");
|
|
}
|
|
}
|
|
}
|
|
|
|
public static fetchAllChapterFromABook(userId: string, bookId: string, lang: 'fr' | 'en' = 'fr'): ChapterQueryResult[] {
|
|
try {
|
|
const db: Database = System.getDb();
|
|
return db.all('SELECT chapter_id, title, chapter_order FROM book_chapters WHERE book_id=? AND author_id=? ORDER BY chapter_order', [bookId, userId]) as ChapterQueryResult[];
|
|
} catch (e: unknown) {
|
|
if (e instanceof Error) {
|
|
console.error(`DB Error: ${e.message}`);
|
|
throw new Error(lang === 'fr' ? `Impossible de récupérer les chapitres.` : `Unable to retrieve chapters.`);
|
|
} else {
|
|
console.error("An unknown error occurred.");
|
|
throw new Error(lang === 'fr' ? "Une erreur inconnue s'est produite." : "An unknown error occurred.");
|
|
}
|
|
}
|
|
}
|
|
|
|
public static deleteChapter(userId: string, chapterId: string, lang: 'fr' | 'en' = 'fr'): boolean {
|
|
try {
|
|
const db: Database = System.getDb();
|
|
const result: RunResult = db.run('DELETE FROM book_chapters WHERE author_id=? AND chapter_id=?', [userId, chapterId]);
|
|
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 chapitre.` : `Unable to delete chapter.`);
|
|
} else {
|
|
console.error("An unknown error occurred.");
|
|
throw new Error(lang === 'fr' ? "Une erreur inconnue s'est produite." : "An unknown error occurred.");
|
|
}
|
|
}
|
|
}
|
|
|
|
static insertChapterInformation(chapterInfoId: string, userId: string, chapterId: string, actId: number, bookId: string, plotId: string | null, incidentId: string | null, lang: 'fr' | 'en' = 'fr'): string {
|
|
let existResult;
|
|
let result: RunResult;
|
|
try {
|
|
const db: Database = System.getDb();
|
|
existResult = db.get('SELECT chapter_info_id FROM book_chapter_infos WHERE chapter_id=? AND act_id=? AND book_id=? AND plot_point_id=? AND incident_id=? AND author_id=?', [chapterId, actId, bookId, plotId, incidentId, userId]);
|
|
} 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'information du chapitre.` : `Unable to verify chapter information existence.`);
|
|
} else {
|
|
console.error("An unknown error occurred.");
|
|
throw new Error(lang === 'fr' ? "Une erreur inconnue s'est produite." : "An unknown error occurred.");
|
|
}
|
|
}
|
|
if (existResult !== null) {
|
|
throw new Error(lang === 'fr' ? `Le chapitre est déjà lié.` : `Chapter is already linked.`);
|
|
}
|
|
try {
|
|
const db: Database = System.getDb();
|
|
result = db.run('INSERT INTO book_chapter_infos (chapter_info_id, chapter_id, act_id, book_id, author_id, incident_id, plot_point_id, summary, goal, last_update) VALUES (?,?,?,?,?,?,?,?,?,?)', [chapterInfoId, chapterId, actId, bookId, userId, incidentId, plotId, '', '', System.timeStampInSeconds()]);
|
|
} catch (e: unknown) {
|
|
if (e instanceof Error) {
|
|
console.error(`DB Error: ${e.message}`);
|
|
throw new Error(lang === 'fr' ? `Impossible d'ajouter l'information du chapitre.` : `Unable to add chapter 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 || result.changes === 0) {
|
|
throw new Error(lang === 'fr' ? `Une erreur s'est produite pendant la liaison du chapitre.` : `Error linking chapter.`);
|
|
}
|
|
return chapterInfoId;
|
|
}
|
|
|
|
public static updateChapter(userId: string, chapterId: string, encryptedTitle: string, hashTitle: string, chapterOrder: number, lastUpdate:number, lang: 'fr' | 'en' = 'fr'): boolean {
|
|
try {
|
|
const db: Database = System.getDb();
|
|
const result: RunResult = db.run('UPDATE book_chapters SET title=?, hashed_title=?, chapter_order=?, last_update=? WHERE author_id=? AND chapter_id=?', [encryptedTitle, hashTitle, chapterOrder, lastUpdate, userId, chapterId]);
|
|
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 chapitre.` : `Unable to update chapter.`);
|
|
} else {
|
|
console.error("An unknown error occurred.");
|
|
throw new Error(lang === 'fr' ? "Une erreur inconnue s'est produite." : "An unknown error occurred.");
|
|
}
|
|
}
|
|
}
|
|
|
|
public static updateChapterInfos(userId: string, chapterId: string, actId: number, bookId: string, incidentId: string | null, plotId: string | null, summary: string, goal: string | null, lastUpdate: number, lang: 'fr' | 'en' = 'fr'): boolean {
|
|
try {
|
|
const db: Database = System.getDb();
|
|
let sql: string = `UPDATE book_chapter_infos SET summary=?,goal=?,last_update=? WHERE chapter_id = ? AND act_id = ? AND book_id = ?`;
|
|
const params: (string|null|number)[] = [summary, goal, lastUpdate, chapterId, actId, bookId];
|
|
if (incidentId) {
|
|
sql += ` AND incident_id=?`;
|
|
params.push(incidentId);
|
|
} else {
|
|
sql += ` AND incident_id IS NULL`;
|
|
}
|
|
if (plotId) {
|
|
sql += ` AND plot_point_id=?`;
|
|
params.push(plotId);
|
|
} else {
|
|
sql += ` AND plot_point_id IS NULL`;
|
|
}
|
|
sql += ` AND author_id=?`;
|
|
params.push(userId);
|
|
const result: RunResult = db.run(sql, 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 les informations du chapitre.` : `Unable to update chapter 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 fetchLastChapter(userId: string, bookId: string, lang: 'fr' | 'en' = 'fr'): LastChapterResult | null {
|
|
try {
|
|
const db: Database = System.getDb();
|
|
const result = db.get('SELECT chapter_id as chapter_id,version FROM user_last_chapter WHERE user_id=? AND book_id=?', [userId, bookId]) as LastChapterResult | null;
|
|
return result;
|
|
} catch (e: unknown) {
|
|
if (e instanceof Error) {
|
|
console.error(`DB Error: ${e.message}`);
|
|
throw new Error(lang === 'fr' ? `Impossible de récupérer le dernier chapitre ouvert.` : `Unable to retrieve last opened chapter.`);
|
|
} else {
|
|
console.error("An unknown error occurred.");
|
|
throw new Error(lang === 'fr' ? "Une erreur inconnue s'est produite." : "An unknown error occurred.");
|
|
}
|
|
}
|
|
}
|
|
|
|
public static updateLastChapterRecord(userId: string, bookId: string, chapterId: string, version: number, lang: 'fr' | 'en' = 'fr'): boolean {
|
|
try {
|
|
const db: Database = System.getDb();
|
|
const result: RunResult = db.run('UPDATE user_last_chapter SET chapter_id=?, version=? WHERE user_id=? AND book_id=?', [chapterId, version, userId, bookId]);
|
|
if (result.changes > 0) {
|
|
return true;
|
|
} else {
|
|
const insertResult: RunResult = db.run('INSERT INTO user_last_chapter (user_id, book_id, chapter_id, version) VALUES (?,?,?,?)', [userId, bookId, chapterId, version]);
|
|
return insertResult.changes > 0;
|
|
}
|
|
} catch (e: unknown) {
|
|
if (e instanceof Error) {
|
|
console.error(`DB Error: ${e.message}`);
|
|
throw new Error(lang === 'fr' ? `Impossible d'enregistrer le dernier chapitre.` : `Unable to save last chapter.`);
|
|
} else {
|
|
console.error("An unknown error occurred.");
|
|
throw new Error(lang === 'fr' ? "Une erreur inconnue s'est produite." : "An unknown error occurred.");
|
|
}
|
|
}
|
|
}
|
|
|
|
public static fetchChapterStory(userId: string, chapterId: string, lang: 'fr' | 'en' = 'fr'):ChapterStoryQueryResult[] {
|
|
try {
|
|
const db: Database = System.getDb();
|
|
return db.all('SELECT chapter_info_id, chapter.act_id, act_sum.summary, chapter.summary AS chapter_summary, chapter.goal AS chapter_goal, chapter.incident_id, incident.title AS incident_title, incident.summary AS incident_summary, chapter.plot_point_id, plot.title AS plot_title, plot.summary AS plot_summary FROM book_chapter_infos AS chapter LEFT JOIN book_incidents AS incident ON chapter.incident_id=incident.incident_id LEFT JOIN book_plot_points AS plot ON chapter.plot_point_id=plot.plot_point_id LEFT JOIN book_act_summaries AS act_sum ON chapter.act_id=act_sum.act_sum_id AND chapter.book_id=act_sum.book_id WHERE chapter.chapter_id=? AND chapter.author_id=?', [chapterId, userId]) as ChapterStoryQueryResult[];
|
|
} catch (e: unknown) {
|
|
if (e instanceof Error) {
|
|
console.error(`DB Error: ${e.message}`);
|
|
throw new Error(lang === 'fr' ? `Impossible de récupérer l'histoire du chapitre.` : `Unable to retrieve chapter story.`);
|
|
} else {
|
|
console.error("An unknown error occurred.");
|
|
throw new Error(lang === 'fr' ? "Une erreur inconnue s'est produite." : "An unknown error occurred.");
|
|
}
|
|
}
|
|
}
|
|
|
|
static deleteChapterInformation(userId: string, chapterInfoId: string, lang: 'fr' | 'en' = 'fr'): boolean {
|
|
try {
|
|
const db: Database = System.getDb();
|
|
const result: RunResult = db.run('DELETE FROM book_chapter_infos WHERE chapter_info_id=? AND author_id=?', [chapterInfoId, 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 supprimer les informations du chapitre.` : `Unable to delete chapter information.`);
|
|
} else {
|
|
console.error("An unknown error occurred.");
|
|
throw new Error(lang === 'fr' ? "Une erreur inconnue s'est produite." : "An unknown error occurred.");
|
|
}
|
|
}
|
|
}
|
|
static isChapterExist(userId: string, chapter_id: string, lang: "fr" | "en"): boolean {
|
|
try {
|
|
const db: Database = System.getDb();
|
|
const result: QueryResult | null = db.get('SELECT 1 FROM book_chapters WHERE chapter_id=? AND author_id=?', [chapter_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 du chapitre.` : `Unable to check chapter existence.`);
|
|
} else {
|
|
console.error("An unknown error occurred.");
|
|
throw new Error(lang === 'fr' ? "Une erreur inconnue s'est produite." : "An unknown error occurred.");
|
|
}
|
|
}
|
|
}
|
|
|
|
static isChapterInfoExist(userId: string, chapter_id: string, lang: "fr" | "en"): boolean {
|
|
try {
|
|
const db: Database = System.getDb();
|
|
const result: QueryResult | null = db.get('SELECT 1 FROM `book_chapter_infos` WHERE `chapter_id`=? AND `author_id`=?', [chapter_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 des informations du chapitre.` : `Unable to check chapter info existence.`);
|
|
} else {
|
|
console.error("An unknown error occurred.");
|
|
throw new Error(lang === 'fr' ? "Une erreur inconnue s'est produite." : "An unknown error occurred.");
|
|
}
|
|
}
|
|
}
|
|
|
|
static fetchCompleteBookChapters(id: string, lang: 'fr' | 'en'): ChapterBookResult[] {
|
|
let result: ChapterBookResult[];
|
|
try {
|
|
const db: Database = System.getDb();
|
|
result = db.all('SELECT title, chapter_order, content.content FROM book_chapters AS chapter LEFT JOIN book_chapter_content AS content ON chapter.chapter_id = content.chapter_id AND content.version = (SELECT MAX(version) FROM book_chapter_content WHERE chapter_id = chapter.chapter_id AND version > 1) WHERE chapter.book_id = ? ORDER BY chapter.chapter_order', [id]) as ChapterBookResult[];
|
|
} catch (e: unknown) {
|
|
if (e instanceof Error) {
|
|
console.error(`DB Error: ${e.message}`);
|
|
throw new Error(lang === 'fr' ? `Impossible de récupérer les chapitres.` : `Unable to retrieve chapters.`);
|
|
} else {
|
|
console.error("An unknown error occurred.");
|
|
throw new Error(lang === 'fr' ? "Une erreur inconnue s'est produite." : "An unknown error occurred.");
|
|
}
|
|
}
|
|
if (result.length === 0) {
|
|
throw new Error(lang === 'fr' ? `Aucun chapitre trouvé.` : `No chapters found.`);
|
|
}
|
|
return result;
|
|
}
|
|
static async fetchBookChapters(userId: string, bookId: string, lang: 'fr' | 'en'): Promise<BookChaptersTable[]> {
|
|
try {
|
|
const db: Database = System.getDb();
|
|
return db.all('SELECT chapter_id, book_id, author_id, title, hashed_title, words_count, chapter_order, last_update FROM book_chapters WHERE author_id=? AND book_id=?', [userId, bookId]) as BookChaptersTable[];
|
|
} catch (e: unknown) {
|
|
if (e instanceof Error) {
|
|
console.error(`DB Error: ${e.message}`);
|
|
throw new Error(lang === 'fr' ? `Impossible de récupérer les chapitres.` : `Unable to retrieve chapters.`);
|
|
} else {
|
|
throw new Error(lang === 'fr' ? "Une erreur inconnue s'est produite." : "An unknown error occurred.");
|
|
}
|
|
}
|
|
}
|
|
|
|
static async fetchBookChapterInfos(userId: string, chapterId: string, lang: 'fr' | 'en'): Promise<BookChapterInfosTable[]> {
|
|
try {
|
|
const db: Database = System.getDb();
|
|
return db.all('SELECT chapter_info_id, chapter_id, act_id, incident_id, plot_point_id, book_id, author_id, summary, goal, last_update FROM book_chapter_infos WHERE author_id=? AND chapter_id=?', [userId, chapterId]) as BookChapterInfosTable[];
|
|
} catch (e: unknown) {
|
|
if (e instanceof Error) {
|
|
console.error(`DB Error: ${e.message}`);
|
|
throw new Error(lang === 'fr' ? `Impossible de récupérer les infos des chapitres.` : `Unable to retrieve chapter infos.`);
|
|
} else {
|
|
throw new Error(lang === 'fr' ? "Une erreur inconnue s'est produite." : "An unknown error occurred.");
|
|
}
|
|
}
|
|
}
|
|
static fetchSyncedChapters(userId: string, lang: 'fr' | 'en'): SyncedChapterResult[] {
|
|
try {
|
|
const db: Database = System.getDb();
|
|
return db.all('SELECT chapter_id, book_id, title, last_update FROM book_chapters WHERE author_id = ?', [userId]) as SyncedChapterResult[];
|
|
} catch (e: unknown) {
|
|
if (e instanceof Error) {
|
|
console.error(`DB Error: ${e.message}`);
|
|
throw new Error(lang === 'fr' ? `Impossible de récupérer les chapitres synchronisés.` : `Unable to retrieve synced chapters.`);
|
|
} else {
|
|
console.error("An unknown error occurred.");
|
|
throw new Error(lang === 'fr' ? "Une erreur inconnue s'est produite." : "An unknown error occurred.");
|
|
}
|
|
}
|
|
}
|
|
|
|
static fetchSyncedChapterInfos(userId: string, lang: 'fr' | 'en'): SyncedChapterInfoResult[] {
|
|
try {
|
|
const db: Database = System.getDb();
|
|
return db.all('SELECT chapter_info_id, chapter_id, book_id, last_update FROM book_chapter_infos WHERE author_id = ?', [userId]) as SyncedChapterInfoResult[];
|
|
} catch (e: unknown) {
|
|
if (e instanceof Error) {
|
|
console.error(`DB Error: ${e.message}`);
|
|
throw new Error(lang === 'fr' ? `Impossible de récupérer les infos des chapitres synchronisés.` : `Unable to retrieve synced chapter infos.`);
|
|
} else {
|
|
console.error("An unknown error occurred.");
|
|
throw new Error(lang === 'fr' ? "Une erreur inconnue s'est produite." : "An unknown error occurred.");
|
|
}
|
|
}
|
|
}
|
|
static insertSyncChapter(chapterId: string, bookId: string, authorId: string, title: string, hashedTitle: string | null, wordsCount: number | null, chapterOrder: number | null, lastUpdate: number, lang: 'fr' | 'en'): boolean {
|
|
try {
|
|
const db: Database = System.getDb();
|
|
const result: RunResult = db.run(
|
|
`INSERT INTO book_chapters (chapter_id, book_id, author_id, title, hashed_title, words_count, chapter_order, last_update)
|
|
VALUES (?, ?, ?, ?, ?, ?, ?, ?)`,
|
|
[chapterId, bookId, authorId, title, hashedTitle, wordsCount, chapterOrder, 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 chapitre.` : `Unable to insert chapter.`);
|
|
} else {
|
|
throw new Error(lang === 'fr' ? "Une erreur inconnue s'est produite." : "An unknown error occurred.");
|
|
}
|
|
}
|
|
}
|
|
|
|
static insertSyncChapterInfo(chapterInfoId: string, chapterId: string, actId: number | null, incidentId: string | null, plotPointId: string | null, bookId: string, authorId: string, summary: string | null, goal: string | null, lastUpdate: number, lang: 'fr' | 'en'): boolean {
|
|
try {
|
|
const db: Database = System.getDb();
|
|
const result: RunResult = db.run(`INSERT INTO book_chapter_infos (chapter_info_id, chapter_id, act_id, incident_id, plot_point_id, book_id, author_id, summary, goal, last_update) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?)`, [chapterInfoId, chapterId, actId, incidentId, plotPointId, bookId, authorId, summary, goal, 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 les infos du chapitre.` : `Unable to insert chapter info.`);
|
|
} else {
|
|
throw new Error(lang === 'fr' ? "Une erreur inconnue s'est produite." : "An unknown error occurred.");
|
|
}
|
|
}
|
|
}
|
|
|
|
static async fetchCompleteChapterById(id: string, lang: "fr" | "en"):Promise<BookChaptersTable[]> {
|
|
try {
|
|
const db: Database = System.getDb();
|
|
return db.all(
|
|
`SELECT chapter_id, book_id, author_id, title, hashed_title, words_count, chapter_order, last_update
|
|
FROM book_chapters
|
|
WHERE chapter_id = ?`,
|
|
[id]
|
|
) as BookChaptersTable[];
|
|
} catch (e:unknown){
|
|
if (e instanceof Error) {
|
|
throw new Error(lang === 'fr' ? `Impossible de récupérer le chapitre complet.` : `Unable to retrieve complete chapter.`);
|
|
} else {
|
|
throw new Error(lang === 'fr' ? "Une erreur inconnue s'est produite." : "An unknown error occurred.");
|
|
}
|
|
}
|
|
}
|
|
|
|
static async fetchCompleteChapterInfoById(id: string, lang: "fr" | "en"):Promise<BookChapterInfosTable[]> {
|
|
try {
|
|
const db: Database = System.getDb();
|
|
return db.all(
|
|
`SELECT chapter_info_id, chapter_id, act_id, incident_id, plot_point_id, book_id, author_id, summary, goal, last_update
|
|
FROM book_chapter_infos
|
|
WHERE chapter_info_id = ?`,
|
|
[id]
|
|
) as BookChapterInfosTable[];
|
|
} catch (e:unknown){
|
|
if (e instanceof Error) {
|
|
throw new Error(lang === 'fr' ? `Impossible de récupérer les informations de chapitre complètes.` : `Unable to retrieve complete chapter info.`);
|
|
} else {
|
|
throw new Error(lang === 'fr' ? "Une erreur inconnue s'est produite." : "An unknown error occurred.");
|
|
}
|
|
}
|
|
}
|
|
}
|