Add ipc/chapter.ipc.ts to handle chapter-related database interactions

- Introduce IPC handlers in `ipc/chapter.ipc.ts` for chapter CRUD, content management, and related operations.
- Register chapter IPC module in `main.ts` for application-wide access.
- Enhance type definitions for chapter functionalities, improving code consistency.
- Refactor `ipc/book.ipc.ts` to include new `UpdateStoryData` interface and additional story update handler.
This commit is contained in:
natreex
2025-11-18 22:42:18 -05:00
parent 361a1b0b7c
commit 75e5d71c74
3 changed files with 196 additions and 15 deletions

View File

@@ -31,6 +31,12 @@ interface StoryData {
issues: Issue[]; issues: Issue[];
} }
interface UpdateStoryData {
bookId: string;
acts: Act[];
mainChapters: any[]; // ChapterProps[] from your API
}
interface CreateBookData { interface CreateBookData {
title: string; title: string;
subTitle: string | null; subTitle: string | null;
@@ -72,10 +78,10 @@ interface SetAIGuideLineData {
bookId: string; bookId: string;
narrativeType: number; narrativeType: number;
dialogueType: number; dialogueType: number;
globalResume: string; plotSummary: string;
atmosphere: string; toneAtmosphere: string;
verbeTense: number; verbTense: number;
langue: number; language: number;
themes: string; themes: string;
} }
@@ -147,6 +153,14 @@ ipcMain.handle('db:book:story:get', createHandler<string, StoryData>(
) )
); );
// POST /book/story - Update story (acts + mainChapters)
ipcMain.handle('db:book:story:update', createHandler<UpdateStoryData, boolean>(
function(userId: string, data: UpdateStoryData, lang: 'fr' | 'en'):boolean {
return Book.updateStory(userId, data.bookId, data.acts, data.mainChapters, lang);
}
)
);
// POST /book/add - Create new book // POST /book/add - Create new book
ipcMain.handle('db:book:create', createHandler<CreateBookData, string>( ipcMain.handle('db:book:create', createHandler<CreateBookData, string>(
function(userId: string, data: CreateBookData, lang: 'fr' | 'en') { function(userId: string, data: CreateBookData, lang: 'fr' | 'en') {
@@ -278,7 +292,7 @@ ipcMain.handle('db:book:delete', createHandler<string, boolean>(
// GET /book/ai/guideline - Get AI guideline // GET /book/ai/guideline - Get AI guideline
ipcMain.handle('db:book:guideline:ai:get', createHandler<string, GuideLineAI>( ipcMain.handle('db:book:guideline:ai:get', createHandler<string, GuideLineAI>(
function(userId: string, bookId: string, lang: 'fr' | 'en') { function(userId: string, bookId: string, lang: 'fr' | 'en') {
return Book.getGuideLineAI(bookId, userId, lang); return Book.getGuideLineAI(userId, bookId, lang);
} }
) )
); );
@@ -287,17 +301,30 @@ ipcMain.handle('db:book:guideline:ai:get', createHandler<string, GuideLineAI>(
ipcMain.handle('db:book:guideline:ai:set', createHandler<SetAIGuideLineData, boolean>( ipcMain.handle('db:book:guideline:ai:set', createHandler<SetAIGuideLineData, boolean>(
function(userId: string, data: SetAIGuideLineData, lang: 'fr' | 'en') { function(userId: string, data: SetAIGuideLineData, lang: 'fr' | 'en') {
return Book.setAIGuideLine( return Book.setAIGuideLine(
data.bookId,
userId, userId,
data.bookId,
data.narrativeType, data.narrativeType,
data.dialogueType, data.dialogueType,
data.globalResume, data.plotSummary,
data.atmosphere, data.toneAtmosphere,
data.verbeTense, data.verbTense,
data.langue, data.language,
data.themes, data.themes,
lang lang
); );
} }
) )
); );
// PUT /book/world/update - Update world
interface UpdateWorldData {
bookId: string;
world: WorldProps;
}
ipcMain.handle('db:book:world:update', createHandler<UpdateWorldData, boolean>(
function(userId: string, data: UpdateWorldData, lang: 'fr' | 'en') {
return Book.updateWorld(userId, data.world, lang);
}
)
);

157
electron/ipc/chapter.ipc.ts Normal file
View File

@@ -0,0 +1,157 @@
import { ipcMain } from 'electron';
import { createHandler } from '../database/LocalSystem.js';
import Chapter from '../database/models/Chapter.js';
import type { ChapterProps, CompanionContent, ActStory } from '../database/models/Chapter.js';
interface GetWholeChapterData {
chapterId: string;
version: number;
bookId?: string;
}
interface SaveChapterContentData {
chapterId: string;
version: number;
content: JSON;
totalWordCount: number;
currentTime: number;
}
interface AddChapterData {
bookId: string;
title: string;
chapterOrder: number;
}
interface UpdateChapterData {
chapterId: string;
title: string;
chapterOrder: number;
}
interface AddChapterInformationData {
chapterId: string;
actId: number;
bookId: string;
plotId: string | null;
incidentId: string | null;
}
interface GetChapterContentData {
chapterId: string;
version: number;
}
// GET /book/chapters - Get all chapters from a book
ipcMain.handle('db:book:chapters', createHandler<string, ChapterProps[]>(
function(userId: string, bookId: string, lang: 'fr' | 'en'): ChapterProps[] {
return Chapter.getAllChaptersFromABook(userId, bookId, lang);
}
)
);
// GET /chapter/whole - Get whole chapter
ipcMain.handle('db:chapter:whole', createHandler<GetWholeChapterData, ChapterProps>(
function(userId: string, data: GetWholeChapterData, lang: 'fr' | 'en'): ChapterProps {
return Chapter.getWholeChapter(userId, data.chapterId, data.version, data.bookId, lang);
}
)
);
// GET /chapter/:id/story - Get chapter story
ipcMain.handle('db:chapter:story', createHandler<string, ActStory[]>(
function(userId: string, chapterId: string, lang: 'fr' | 'en'): ActStory[] {
return Chapter.getChapterStory(userId, chapterId, lang);
}
)
);
// GET /chapter/content/companion - Get companion content
ipcMain.handle('db:chapter:content:companion', createHandler<GetChapterContentData, CompanionContent>(
function(userId: string, data: GetChapterContentData, lang: 'fr' | 'en'): CompanionContent {
return Chapter.getCompanionContent(userId, data.chapterId, data.version, lang);
}
)
);
// GET /chapter/content - Get chapter content by version
ipcMain.handle('db:chapter:content:get', createHandler<GetChapterContentData, string>(
function(userId: string, data: GetChapterContentData, lang: 'fr' | 'en'): string {
return Chapter.getChapterContentByVersion(userId, data.chapterId, data.version, lang);
}
)
);
// POST /chapter/content - Save chapter content
ipcMain.handle('db:chapter:content:save', createHandler<SaveChapterContentData, boolean>(
function(userId: string, data: SaveChapterContentData, lang: 'fr' | 'en'): boolean {
return Chapter.saveChapterContent(
userId,
data.chapterId,
data.version,
data.content,
data.totalWordCount,
data.currentTime,
lang
);
}
)
);
// GET /chapter/last-chapter - Get last chapter
ipcMain.handle('db:chapter:last', createHandler<string, ChapterProps | null>(
function(userId: string, bookId: string, lang: 'fr' | 'en'): ChapterProps | null {
return Chapter.getLastChapter(userId, bookId, lang);
}
)
);
// POST /chapter/add - Add new chapter
ipcMain.handle('db:chapter:create', createHandler<AddChapterData, string>(
function(userId: string, data: AddChapterData, lang: 'fr' | 'en'): string {
return Chapter.addChapter(userId, data.bookId, data.title, 0, data.chapterOrder, lang);
}
)
);
// DELETE /chapter/remove - Remove chapter
ipcMain.handle('db:chapter:delete', createHandler<string, boolean>(
function(userId: string, chapterId: string, lang: 'fr' | 'en'): boolean {
return Chapter.removeChapter(userId, chapterId, lang);
}
)
);
// POST /chapter/update - Update chapter
ipcMain.handle('db:chapter:update', createHandler<UpdateChapterData, boolean>(
function(userId: string, data: UpdateChapterData, lang: 'fr' | 'en'): boolean {
return Chapter.updateChapter(userId, data.chapterId, data.title, data.chapterOrder, lang);
}
)
);
// POST /chapter/resume/add - Add chapter information
ipcMain.handle('db:chapter:information:add', createHandler<AddChapterInformationData, string>(
function(userId: string, data: AddChapterInformationData, lang: 'fr' | 'en'): string {
return Chapter.addChapterInformation(
userId,
data.chapterId,
data.actId,
data.bookId,
data.plotId,
data.incidentId,
lang
);
}
)
);
// DELETE /chapter/resume/remove - Remove chapter information
ipcMain.handle('db:chapter:information:remove', createHandler<string, any>(
function(userId: string, chapterInfoId: string, lang: 'fr' | 'en') {
return Chapter.removeChapterInformation(userId, chapterInfoId, lang);
}
)
);
console.log('[IPC] Chapter handlers registered');

View File

@@ -9,6 +9,7 @@ import { getDatabaseService } from './database/database.service.js';
// Import IPC handlers // Import IPC handlers
import './ipc/book.ipc.js'; import './ipc/book.ipc.js';
import './ipc/user.ipc.js'; import './ipc/user.ipc.js';
import './ipc/chapter.ipc.js';
// Fix pour __dirname en ES modules // Fix pour __dirname en ES modules
const __filename = fileURLToPath(import.meta.url); const __filename = fileURLToPath(import.meta.url);
@@ -204,7 +205,7 @@ ipcMain.handle('db:user:sync', async (_event, data: SyncUserData): Promise<boole
data.lastName, data.lastName,
data.username, data.username,
data.email, data.email,
'', // Password not needed for local DB '',
lang lang
); );
console.log(`[DB] User ${data.userId} synced successfully`); console.log(`[DB] User ${data.userId} synced successfully`);
@@ -273,10 +274,6 @@ ipcMain.handle('db-initialize', (_event, userId: string, encryptionKey: string)
// and use the new createHandler() pattern with auto userId/lang injection // and use the new createHandler() pattern with auto userId/lang injection
app.whenReady().then(() => { app.whenReady().then(() => {
console.log('App ready, isDev:', isDev);
console.log('resourcesPath:', process.resourcesPath);
console.log('isPackaged:', app.isPackaged);
// Enregistrer le protocole custom app:// pour servir les fichiers depuis out/ // Enregistrer le protocole custom app:// pour servir les fichiers depuis out/
if (!isDev) { if (!isDev) {
const outPath = path.join(process.resourcesPath, 'app.asar.unpacked/out'); const outPath = path.join(process.resourcesPath, 'app.asar.unpacked/out');