- 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.
185 lines
9.4 KiB
TypeScript
185 lines
9.4 KiB
TypeScript
import {Database, QueryResult, RunResult, SQLiteValue} from "node-sqlite3-wasm";
|
|
import System from "@/electron/database/System";
|
|
|
|
export interface BookIssuesTable extends Record<string, SQLiteValue> {
|
|
issue_id: string;
|
|
author_id: string;
|
|
book_id: string;
|
|
name: string;
|
|
hashed_issue_name: string;
|
|
last_update: number;
|
|
}
|
|
|
|
export interface SyncedIssueResult extends Record<string, SQLiteValue> {
|
|
issue_id: string;
|
|
book_id: string;
|
|
name: string;
|
|
last_update: number;
|
|
}
|
|
|
|
export interface IssueQuery extends Record<string, SQLiteValue> {
|
|
issue_id: string;
|
|
name: string;
|
|
}
|
|
|
|
export default class IssueRepository {
|
|
public static fetchIssuesFromBook(userId:string,bookId:string, lang: 'fr' | 'en'):IssueQuery[]{
|
|
try {
|
|
const db: Database = System.getDb();
|
|
return db.all('SELECT issue_id, name FROM book_issues WHERE author_id=? AND book_id=?', [userId, bookId]) as IssueQuery[];
|
|
} catch (e: unknown) {
|
|
if (e instanceof Error) {
|
|
console.error(`DB Error: ${e.message}`);
|
|
throw new Error(lang === 'fr' ? `Impossible de récupérer les problématiques.` : `Unable to retrieve issues.`);
|
|
} else {
|
|
console.error("An unknown error occurred.");
|
|
throw new Error(lang === 'fr' ? "Une erreur inconnue s'est produite." : "An unknown error occurred.");
|
|
}
|
|
}
|
|
}
|
|
public static insertNewIssue(issueId: string, userId: string, bookId: string, encryptedName: string, hashedName: string, lang: 'fr' | 'en'): string {
|
|
let existingResult: QueryResult | null;
|
|
let insertResult: RunResult;
|
|
try {
|
|
const db: Database = System.getDb();
|
|
existingResult = db.get('SELECT issue_id FROM book_issues WHERE hashed_issue_name=? AND book_id=? AND author_id=?', [hashedName, bookId, 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 la problématique.` : `Unable to verify issue existence.`);
|
|
} else {
|
|
console.error("An unknown error occurred.");
|
|
throw new Error(lang === 'fr' ? "Une erreur inconnue s'est produite." : "An unknown error occurred.");
|
|
}
|
|
}
|
|
if (existingResult !== null) {
|
|
throw new Error(lang === 'fr' ? `La problématique existe déjà.` : `This issue already exists.`);
|
|
}
|
|
try {
|
|
const db: Database = System.getDb();
|
|
insertResult = db.run('INSERT INTO book_issues (issue_id,author_id, book_id, name, hashed_issue_name, last_update) VALUES (?,?,?,?,?,?)', [issueId, 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 la problématique.` : `Unable to add issue.`);
|
|
} else {
|
|
console.error("An unknown error occurred.");
|
|
throw new Error(lang === 'fr' ? "Une erreur inconnue s'est produite." : "An unknown error occurred.");
|
|
}
|
|
}
|
|
if (!insertResult || insertResult.changes === 0) {
|
|
throw new Error(lang === 'fr' ? `Erreur pendant l'ajout de la problématique.` : `Error adding issue.`);
|
|
}
|
|
return issueId;
|
|
}
|
|
|
|
public static deleteIssue(userId: string, issueId: string, lang: 'fr' | 'en'): boolean {
|
|
try {
|
|
const db: Database = System.getDb();
|
|
const result: RunResult = db.run('DELETE FROM book_issues WHERE author_id=? AND issue_id=?', [userId, issueId]);
|
|
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 la problématique.` : `Unable to delete issue.`);
|
|
} else {
|
|
console.error("An unknown error occurred.");
|
|
throw new Error(lang === 'fr' ? "Une erreur inconnue s'est produite." : "An unknown error occurred.");
|
|
}
|
|
}
|
|
}
|
|
static async fetchBookIssues(userId: string, bookId: string, lang: 'fr' | 'en'): Promise<BookIssuesTable[]> {
|
|
try {
|
|
const db: Database = System.getDb();
|
|
return db.all('SELECT issue_id, author_id, book_id, name, hashed_issue_name, last_update FROM book_issues WHERE author_id=? AND book_id=?', [userId, bookId]) as BookIssuesTable[];
|
|
} catch (e: unknown) {
|
|
if (e instanceof Error) {
|
|
console.error(`DB Error: ${e.message}`);
|
|
throw new Error(lang === 'fr' ? `Impossible de récupérer les problématiques.` : `Unable to retrieve issues.`);
|
|
} else {
|
|
throw new Error(lang === 'fr' ? "Une erreur inconnue s'est produite." : "An unknown error occurred.");
|
|
}
|
|
}
|
|
}
|
|
static fetchSyncedIssues(userId: string, lang: 'fr' | 'en'): SyncedIssueResult[] {
|
|
try {
|
|
const db: Database = System.getDb();
|
|
return db.all('SELECT issue_id, book_id, name, last_update FROM book_issues WHERE author_id = ?', [userId]) as SyncedIssueResult[];
|
|
} catch (e: unknown) {
|
|
if (e instanceof Error) {
|
|
console.error(`DB Error: ${e.message}`);
|
|
throw new Error(lang === 'fr' ? `Impossible de récupérer les problématiques synchronisées.` : `Unable to retrieve synced issues.`);
|
|
} else {
|
|
console.error("An unknown error occurred.");
|
|
throw new Error(lang === 'fr' ? "Une erreur inconnue s'est produite." : "An unknown error occurred.");
|
|
}
|
|
}
|
|
}
|
|
|
|
static insertSyncIssue(issueId: string, authorId: string, bookId: string, name: string, hashedIssueName: string, lastUpdate: number, lang: 'fr' | 'en'): boolean {
|
|
try {
|
|
const db: Database = System.getDb();
|
|
const result: RunResult = db.run(
|
|
`INSERT INTO book_issues (issue_id, author_id, book_id, name, hashed_issue_name, last_update)
|
|
VALUES (?, ?, ?, ?, ?, ?)`,
|
|
[issueId, authorId, bookId, name, hashedIssueName, 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 la problématique.` : `Unable to insert issue.`);
|
|
} else {
|
|
throw new Error(lang === 'fr' ? "Une erreur inconnue s'est produite." : "An unknown error occurred.");
|
|
}
|
|
}
|
|
}
|
|
static async fetchCompleteIssueById(id: string, lang: "fr" | "en"):Promise<BookIssuesTable[]> {
|
|
try {
|
|
const db: Database = System.getDb();
|
|
return db.all(
|
|
`SELECT issue_id, author_id, book_id, name, hashed_issue_name, last_update
|
|
FROM book_issues
|
|
WHERE issue_id = ?`,
|
|
[id]
|
|
) as BookIssuesTable[];
|
|
} catch (e:unknown){
|
|
if (e instanceof Error) {
|
|
throw new Error(lang === 'fr' ? `Impossible de récupérer le problème complet.` : `Unable to retrieve complete issue.`);
|
|
} else {
|
|
throw new Error(lang === 'fr' ? "Une erreur inconnue s'est produite." : "An unknown error occurred.");
|
|
}
|
|
}
|
|
}
|
|
static updateIssue(userId: string, bookId: string, issueId: string, name: string, hashedName: string, lastUpdate: number, lang: "fr" | "en"):boolean {
|
|
try {
|
|
const db: Database = System.getDb();
|
|
const query:string = `UPDATE book_issues SET name = ?, hashed_issue_name = ?, last_update = FROM_UNIXTIME(?) WHERE issue_id = UUID_TO_BIN(?) AND author_id = UUID_TO_BIN(?) AND book_id = UUID_TO_BIN(?)`;
|
|
const params:(string|number)[] = [name, hashedName, lastUpdate, issueId, userId, bookId];
|
|
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 la problématique.` : `Unable to update issue.`);
|
|
} else {
|
|
console.error("An unknown error occurred.");
|
|
throw new Error(lang === 'fr' ? "Une erreur inconnue s'est produite." : "An unknown error occurred.");
|
|
}
|
|
}
|
|
}
|
|
static issueExist(userId: string, bookId: string, issue_id: string,lang: "fr" | "en"): boolean {
|
|
try {
|
|
const db: Database = System.getDb();
|
|
const result: QueryResult | null = db.get('SELECT 1 FROM `book_issues` WHERE `issue_id`=? AND `author_id`=? AND `book_id`=?', [issue_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 problème.` : `Unable to check issue existence.`);
|
|
} else {
|
|
throw new Error(lang === 'fr' ? "Une erreur inconnue s'est produite." : "An unknown error occurred.");
|
|
}
|
|
}
|
|
}
|
|
} |